From afbf30f1c08461d3c4fed6b4ebcbe1e3afd90430 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Mon, 19 Jan 2015 13:29:26 +0100 Subject: [PATCH] work on router --- Gemfile.lock | 4 +- lib/travis/api/v3/result.rb | 21 ++++++++-- lib/travis/api/v3/router.rb | 27 +++++++++++-- lib/travis/api/v3/routes.rb | 1 + lib/travis/api/v3/routes/dsl.rb | 35 ++++++++++++++++- lib/travis/api/v3/routes/resource.rb | 15 ++++++++ lib/travis/api/v3/services/find_repository.rb | 2 +- travis-api.gemspec | 38 ++++++++++++++----- 8 files changed, 123 insertions(+), 20 deletions(-) create mode 100644 lib/travis/api/v3/routes/resource.rb diff --git a/Gemfile.lock b/Gemfile.lock index df988b45..933e755e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,7 +36,7 @@ GIT GIT remote: git://github.com/travis-ci/travis-core.git - revision: 8fa9680a47ab457187ddf3a88461a756a6f4c2a6 + revision: cbfb6da8a1c6f4cc80c40b260d4d2708e3acec03 specs: travis-core (0.0.1) actionmailer (~> 3.2.19) @@ -74,7 +74,7 @@ GIT GIT remote: git://github.com/travis-ci/travis-yaml.git - revision: f3aa306016a08b66a487f966eb8aa3a60ee9b319 + revision: 1630d576d221aea2340615e87f402df7889b5176 specs: travis-yaml (0.2.0) diff --git a/lib/travis/api/v3/result.rb b/lib/travis/api/v3/result.rb index 9aff2b05..44cd799d 100644 --- a/lib/travis/api/v3/result.rb +++ b/lib/travis/api/v3/result.rb @@ -1,9 +1,24 @@ module Travis::API::V3 class Result - attr_accessor :resource + attr_accessor :type, :resource - def initialize(resource = nil) - @resource = resource + def initialize(type, resource = []) + @type, @resource = type, resource + end + + def respond_to_missing?(method, *) + super or method.to_sym == type.to_sym + end + + def <<(value) + resource << value + self + end + + def method_missing(method, *args) + return super unless method.to_sym == type.to_sym + raise ArgumentError, 'wrong number of arguments (1 for 0)'.freeze if args.any? + resource end end end diff --git a/lib/travis/api/v3/router.rb b/lib/travis/api/v3/router.rb index ee243ad5..5b51d5e0 100644 --- a/lib/travis/api/v3/router.rb +++ b/lib/travis/api/v3/router.rb @@ -12,9 +12,30 @@ module Travis::API::V3 end def call(env) - access_control = AccessControl.new(env) - p access_control - not_found + access_control = AccessControl.new(env) + factory, params = @routes.factory_for(env['REQUEST_METHOD'.freeze], env['PATH_INFO'.freeze]) + env_params = params(env) + if factory + service = factory.new(access_control, env_params.merge(params)) + result = service.run + render(result, env_params) + else + not_found + end + end + + def params(env) + request = Rack::Request.new(env) + params = request.params + media_type = request.media_type + + if media_type == 'application/json'.freeze or media_type == 'text/json'.freeze + request.body.rewind + json_params = env['travis.input.json'.freeze] ||= JSON.load(request.body) + params.merge! json_params if json_params.is_a? Hash + end + + params end end end diff --git a/lib/travis/api/v3/routes.rb b/lib/travis/api/v3/routes.rb index 12b98654..519bffaa 100644 --- a/lib/travis/api/v3/routes.rb +++ b/lib/travis/api/v3/routes.rb @@ -4,6 +4,7 @@ module Travis::API::V3 extend DSL resource :repository do + route '/repo/:id' get :find_repository end end diff --git a/lib/travis/api/v3/routes/dsl.rb b/lib/travis/api/v3/routes/dsl.rb index 544efbd7..ca594929 100644 --- a/lib/travis/api/v3/routes/dsl.rb +++ b/lib/travis/api/v3/routes/dsl.rb @@ -1,5 +1,38 @@ +require 'travis/api/v3/routes/resource' +require 'mustermann' + module Travis::API::V3 module Routes::DSL - def resource(*) end + def resources + @resources ||= [] + end + + def current_resource + @current_resource ||= nil + end + + def resource(type, &block) + resource = Routes::Resource.new(type) + with_resource(resource, &block) + resources << resource + end + + def with_resource(resource) + resource_was, @current_resource = current_resource, resource + yield + ensure + @current_resource = resource_was + end + + def route(value) + current_resource.route = Mustermann.new(value) + end + + def get(*args) + current_resource.add_service('GET'.freeze, *args) + end + + def factory_for(method, path) + end end end diff --git a/lib/travis/api/v3/routes/resource.rb b/lib/travis/api/v3/routes/resource.rb new file mode 100644 index 00000000..45b26841 --- /dev/null +++ b/lib/travis/api/v3/routes/resource.rb @@ -0,0 +1,15 @@ +module Travis::API::V3 + class Routes::Resource + attr_accessor :identifier, :route, :services + + def initialize(identifier) + @identifier = identifier + @services = {} + end + + def add_service(request_method, service, sub_route = '') + services[request_method] ||= {} + services[request_method][sub_route] = service + end + end +end diff --git a/lib/travis/api/v3/services/find_repository.rb b/lib/travis/api/v3/services/find_repository.rb index f749a173..73d3a68a 100644 --- a/lib/travis/api/v3/services/find_repository.rb +++ b/lib/travis/api/v3/services/find_repository.rb @@ -4,7 +4,7 @@ module Travis::API::V3 def run raise NotFound unless repository and access_control.visible? repository - Result.new(repository) + Result.new(:repository, repository) end def repository diff --git a/travis-api.gemspec b/travis-api.gemspec index d98c31da..f4ffa7d3 100644 --- a/travis-api.gemspec +++ b/travis-api.gemspec @@ -12,15 +12,16 @@ Gem::Specification.new do |s| "Piotr Sarnacki", "Konstantin Haase", "Sven Fuchs", - "Hiro Asari", "Mathias Meyer", + "Hiro Asari", "Josh Kalderimis", "Henrik Hodne", "Andre Arko", - "Erik Michaels-Ober", "Dan Buch", + "Erik Michaels-Ober", "Steve Richert", "Brian Ford", + "Patrick Williams", "Bryan Goldstein", "Puneeth Chaganti", "Thais Camilo and Konstantin Haase", @@ -29,37 +30,37 @@ Gem::Specification.new do |s| "James Dennes", "rainsun", "Dan Rice", - "Nick Schonning", - "Patrick Williams" + "Nick Schonning" ] s.email = [ "drogus@gmail.com", "konstantin.mailinglists@googlemail.com", "me@svenfuchs.com", - "asari.ruby@gmail.com", "meyer@paperplanes.de", + "asari.ruby@gmail.com", "josh.kalderimis@gmail.com", "me@henrikhodne.com", "henrik@hodne.io", "konstantin.haase@gmail.com", "andre@arko.net", "svenfuchs@artweb-design.de", - "sferik@gmail.com", "dan@travis-ci.org", + "sferik@gmail.com", "steve.richert@gmail.com", "bford@engineyard.com", "henrik@travis-ci.com", - "punchagan@muse-amuse.in", + "brysgo@gmail.com", + "jdennes@gmail.com", "rainsuner@gmail.com", "dev+narwen+rkh@rkh.im", "tim@spork.in", "e@zzak.io", - "jdennes@gmail.com", + "punchagan@muse-amuse.in", "dan@zoombody.com", + "dan@meatballhat.com", "nschonni@gmail.com", - "patrick@bittorrent.com", - "brysgo@gmail.com" + "patrick@bittorrent.com" ] s.files = [ @@ -154,6 +155,23 @@ Gem::Specification.new do |s| "lib/travis/api/v2/http/ssl_key.rb", "lib/travis/api/v2/http/user.rb", "lib/travis/api/v2/http/validation_error.rb", + "lib/travis/api/v3.rb", + "lib/travis/api/v3/access_control.rb", + "lib/travis/api/v3/access_control/anonymous.rb", + "lib/travis/api/v3/access_control/application.rb", + "lib/travis/api/v3/access_control/generic.rb", + "lib/travis/api/v3/access_control/legacy_token.rb", + "lib/travis/api/v3/access_control/scoped.rb", + "lib/travis/api/v3/access_control/signature.rb", + "lib/travis/api/v3/access_control/user.rb", + "lib/travis/api/v3/opt_in.rb", + "lib/travis/api/v3/result.rb", + "lib/travis/api/v3/router.rb", + "lib/travis/api/v3/routes.rb", + "lib/travis/api/v3/routes/dsl.rb", + "lib/travis/api/v3/service.rb", + "lib/travis/api/v3/services.rb", + "lib/travis/api/v3/services/find_repository.rb", "lib/travis/private_key.rb", "public/favicon.ico", "public/images/result/canceled.png",