Class: Sequencescape::Api

Inherits:
Object
  • Object
show all
Extended by:
ConnectionFactory::Helpers
Includes:
BasicErrorHandling
Defined in:
lib/sequencescape-api/core.rb,
lib/sequencescape-api/errors.rb,
lib/sequencescape-api/version.rb

Defined Under Namespace

Modules: Actions, Associations, BasicErrorHandling, Composition, FinderMethods, GeneralError, Rails Classes: ConnectionFactory, JsonError, ModifyingHandler, PageOfResults, Resource, ResourceInvalid, ResourceModelProxy, Version1, Version2

Constant Summary

Error =
Class.new(StandardError)
VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConnectionFactory::Helpers

connection_factory, connection_factory=

Methods included from BasicErrorHandling

#missing, #redirection, #unauthenticated

Constructor Details

#initialize(options = {}) ⇒ Api



12
13
14
15
16
17
# File 'lib/sequencescape-api/core.rb', line 12

def initialize(options = {})
  @models, @model_namespace = {}, options.delete(:namespace) || Sequencescape
  @connection = self.class.connection_factory.create(options).tap do |connection|
    connection.root(self)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



30
31
32
33
# File 'lib/sequencescape-api/core.rb', line 30

def method_missing(name, *args, &block)
  return super unless @models.keys.include?(name.to_s)
  ResourceModelProxy.new(self, model(name), @models[name.to_s])
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities



19
20
21
# File 'lib/sequencescape-api/core.rb', line 19

def capabilities
  @capabilities
end

Instance Method Details

#inspectObject



56
57
58
# File 'lib/sequencescape-api/core.rb', line 56

def inspect
  "#<Sequencescape::Api @connection=#{@connection.inspect}>"
end

#model(name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sequencescape-api/core.rb', line 36

def model(name)
  parts = name.to_s.split('::').map(&:classify)
  raise StandardError, "#{name.inspect} is rooted and that is not supported" if parts.first.blank?
  parts.inject(@model_namespace) { |context, part| context.const_get(part) }
rescue NameError => missing_constant_in_user_specified_namespace_fallback
  raise if @model_namespace == ::Sequencescape

  parts.inject([ ::Sequencescape, @model_namespace ]) do |(source, dest), part|
    const_from_source = source.const_get(part)
    [ const_from_source, dest.const_set(part, const_from_source) ]
  end.last
end

#read_uuid(uuid, handler) ⇒ Object



22
23
24
# File 'lib/sequencescape-api/core.rb', line 22

def read_uuid(uuid, handler)
  read(@connection.url_for_uuid(uuid), handler)
end

#respond_to?(name, include_private = false) ⇒ Boolean



26
27
28
# File 'lib/sequencescape-api/core.rb', line 26

def respond_to?(name, include_private = false)
  super || @models.keys.include?(name.to_s)
end

#success(json) ⇒ Object



51
52
53
54
# File 'lib/sequencescape-api/core.rb', line 51

def success(json)
  @capabilities = Sequencescape::Api.const_get("Version#{json.delete('revision') || 1}").new
  @models       = Hash[json.map { |k,v| [ k.to_s.singularize, v['actions'] ] }]
end