Class: Patchboard

Inherits:
Object
  • Object
show all
Defined in:
lib/patchboard.rb,
lib/patchboard/api.rb,
lib/patchboard/util.rb,
lib/patchboard/action.rb,
lib/patchboard/resource.rb,
lib/patchboard/response.rb,
lib/patchboard/endpoints.rb,
lib/patchboard/schema_manager.rb

Defined Under Namespace

Modules: Resources, Util Classes: API, Action, Client, Endpoints, Mapping, Resource, Response, SchemaManager

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, options = {}, &block) ⇒ Patchboard

Returns a new instance of Patchboard.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



69
70
71
# File 'lib/patchboard.rb', line 69

def api
  @api
end

#contextObject (readonly)

Returns the value of attribute context.



69
70
71
# File 'lib/patchboard.rb', line 69

def context
  @context
end

#httpObject (readonly)

Returns the value of attribute http.



69
70
71
# File 'lib/patchboard.rb', line 69

def http
  @http
end

#resourcesObject (readonly)

Returns the value of attribute resources.



69
70
71
# File 'lib/patchboard.rb', line 69

def resources
  @resources
end

#schema_managerObject (readonly)

Returns the value of attribute schema_manager.



69
70
71
# File 'lib/patchboard.rb', line 69

def schema_manager
  @schema_manager
end

Class Method Details

.discover(url, options = {}, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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

.httpObject



49
50
51
# File 'lib/patchboard.rb', line 49

def self.http
  @http ||= HTTP.with_headers "User-Agent" => "patchboard-rb v0.1.0"
end

Instance Method Details

#create_class(resource_name, definition, schema, mapping) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/patchboard.rb', line 124

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_classesObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/patchboard.rb', line 110

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) ⇒ Object



95
96
97
98
# File 'lib/patchboard.rb', line 95

def spawn(context=nil)
  context ||= @context_creator.call
  self.class::Client.new(context, @api, @endpoint_classes)
end