class Patchboard
Attributes
api[R]
context[R]
http[R]
resources[R]
schema_manager[R]
Public Class Methods
discover(url, options={}, &block)
click to toggle source
# File lib/patchboard.rb, line 53 def self.discover(url, options={}, &block) begin response = self.http.request "GET", url, :response => :object, :headers => { "Accept" => "application/json" } data = JSON.parse(response.body, :symbolize_names => true) self.new(data, options, &block) rescue JSON::ParserError => error raise "Unparseable API description: #{error}" rescue Errno::ECONNREFUSED => error raise "Problem discovering API: #{error}" end end
http()
click to toggle source
# File lib/patchboard.rb, line 49 def self.http @http ||= HTTP.with_headers "User-Agent" => "patchboard-rb v0.1.0" end
new(api, options={}, &block)
click to toggle source
# File lib/patchboard.rb, line 71 def initialize(api, options={}, &block) @api = API.new(api) @options = options @context_creator = block if options[:namespace] if options[:namespace].is_a? Module @namespace = options[:namespace] else raise "Namespace must be a Module" end end @endpoint_classes = {} @schema_manager = SchemaManager.new(@api.schemas) @http = self.class.http self.create_classes() client = self.spawn({}) @resources = client.resources @context = client.context end
Public Instance Methods
create_class(resource_name, definition, schema, mapping)
click to toggle source
# File lib/patchboard.rb, line 129 def create_class(resource_name, definition, schema, mapping) patchboard = self mapping.klass = klass = Class.new(self.class::Resource) do |klass| self.assemble(patchboard, definition, schema, mapping) end if @namespace @namespace.const_set Util.camel_case(resource_name).to_sym, klass else Patchboard::Resources.const_set Util.camel_case(resource_name).to_sym, klass end klass end
create_classes()
click to toggle source
# File lib/patchboard.rb, line 115 def create_classes klasses = {} @api.mappings.each do |name, mapping| resource_name = mapping.resource.name.to_sym schema = @schema_manager.find :name => resource_name klass = klasses[name] ||= begin resource_def = mapping.resource self.create_class(name, resource_def, schema, mapping) end @endpoint_classes[name] = klass end end
spawn(context=nil)
click to toggle source
# File lib/patchboard.rb, line 95 def spawn(context=nil) context ||= @context_creator.call self.class::Client.new(self, context, @api, @endpoint_classes) end