Class: Rod::Rest::Client

Inherits:
Object
  • Object
show all
Includes:
Naming
Defined in:
lib/rod/rest/client.rb

Instance Method Summary collapse

Methods included from Naming

#plural_resource_name, #singular_resource_name

Constructor Details

#initialize(options = {}) ⇒ Client

Options:

  • http_client - library used to talk via HTTP (e.g. Faraday)

  • parser - parser used to parse the incoming data (JSON by default)

  • factory - factory class used to build the proxy objects

  • url_encoder - encoder used to encode URL strings (CGI by default)

  • metadata - metadata describing the remote database (optional - it is retrieved via the API if not given; in that case metadata_factory must be provided).

  • metadata_factory - factory used to build the metadata (used only if metadata was not provided).

  • proxy_cache - used to cache proxied objects. By default it is ProxyCache. Might be disabled by passing nil.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rod/rest/client.rb', line 23

def initialize(options={})
  @web_client = options.fetch(:http_client)
  @parser = options[:parser] || JSON
  @proxy_factory_class = options[:factory] || ProxyFactory
  @url_encoder = options[:url_encoder] || CGI
  if options.has_key?(:proxy_cache)
    @proxy_cache = options[:proxy_cache]
  else
    @proxy_cache = ProxyCache.new
  end

  @metadata = options[:metadata]
  if @metadata
    (@metadata)
  else
    @metadata_factory = options[:metadata_factory] || Metadata
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object

Overrided in order to fetch the metadata if it was not provided in the constructor.



78
79
80
81
82
83
84
85
# File 'lib/rod/rest/client.rb', line 78

def method_missing(*args)
  unless @metadata.nil?
    super
  end
  @metadata = 
  (@metadata)
  self.send(*args)
end

Instance Method Details

#fetch_object(object_stub) ⇒ Object

Fetch the object from the remote API. The method requires the stub of the object to be proviede, i.e. a hash containing its rod_id and type, e.g. 1, type: “Car”.



53
54
55
56
57
# File 'lib/rod/rest/client.rb', line 53

def fetch_object(object_stub)
  check_stub(object_stub)
  check_method(object_stub)
  __send__(primary_finder_method(object_stub[:type]),object_stub[:rod_id])
end

Fetch object related via the association to the subject. The association name is association_name and the object returned is the index-th element in the collection.



62
63
64
65
# File 'lib/rod/rest/client.rb', line 62

def fetch_related_object(subject,association_name,index)
  check_subject_and_association(subject,association_name)
  __send__(association_method(subject.type,association_name),subject.rod_id,index)
end

Fetch objects related via the association to the subject. The association name is association_name and the objects are the objects indicated by the idices. This might be a range or a comma separated list of indices.



71
72
73
74
# File 'lib/rod/rest/client.rb', line 71

def fetch_related_objects(subject,association_name,*indices)
  check_subject_and_association(subject,association_name)
  __send__(plural_association_method(subject.type,association_name),subject.rod_id,*indices)
end

#inspectObject

Detailed description of the client.



88
89
90
# File 'lib/rod/rest/client.rb', line 88

def inspect
  "Rod::Rest::Client<port: #{@web_client.port}, host: #{@web_client.host}>"
end

#metadataObject

Returns the Database metadata.



43
44
45
46
47
48
# File 'lib/rod/rest/client.rb', line 43

def 
  return @metadata unless @metadata.nil?
  @metadata = 
  (@metadata)
  @metadata
end

#to_sObject

Short description of the client.



93
94
95
# File 'lib/rod/rest/client.rb', line 93

def to_s
  "ROD REST API client"
end