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, ErrorHandling, FinderMethods, GeneralError, Rails Classes: ConnectionFactory, JsonError, ModifyingHandler, PageOfResults, Resource, ResourceInvalid, ResourceModelProxy, Version1, Version2

Constant Summary collapse

Error =
Class.new(StandardError)
VERSION =
'2.0.0'.freeze

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

Returns a new instance of Api.



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

def initialize(options = {})
  @models = {}
  @model_namespace = options.delete(:namespace) || Sequencescape
  @model_namespace = @model_namespace.constantize if @model_namespace.is_a?(String)
  @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)



33
34
35
36
37
# File 'lib/sequencescape-api/core.rb', line 33

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.



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

def capabilities
  @capabilities
end

Instance Method Details

#inspectObject



65
66
67
# File 'lib/sequencescape-api/core.rb', line 65

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

#model(name) ⇒ Object

rubocop:todo Metrics/MethodLength



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sequencescape-api/core.rb', line 40

def model(name) # rubocop:todo Metrics/MethodLength
  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 => e
  raise if @model_namespace == ::Sequencescape

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

#read_uuid(uuid, handler) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#success(json) ⇒ Object



60
61
62
63
# File 'lib/sequencescape-api/core.rb', line 60

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