Module: ElvantoAPI::Resource::ClassMethods

Defined in:
lib/elvanto/resources/resource.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/elvanto/resources/resource.rb', line 105

def method_missing(method, *args, &block)
  if member_method? method
    query_member(resource_class::MEMBER_METHODS[method], *args)
  elsif collection_method? method
    query_collection(resource_class::COLLECTION_METHODS[method], *args)
  else
    super method, *args, &block
  end
end

Instance Method Details

#collection_method?(method) ⇒ Boolean

Returns True if API method returns a set of objects, otherwise false.

Parameters:

  • method (Symbol)

    The name of the method to call

Returns:

  • (Boolean)

    True if API method returns a set of objects, otherwise false



100
101
102
103
# File 'lib/elvanto/resources/resource.rb', line 100

def collection_method? method
  return unless defined? resource_class::COLLECTION_METHODS
  resource_class::COLLECTION_METHODS.keys.include? method
end

#collection_nameObject



51
52
53
# File 'lib/elvanto/resources/resource.rb', line 51

def collection_name
  Utils.pluralize Utils.underscore(resource_name)
end

#collection_pathObject Also known as: href



55
56
57
# File 'lib/elvanto/resources/resource.rb', line 55

def collection_path
  collection_name
end

#construct_from_response(payload) ⇒ Object

Returns Instance of the class specified in body.

Parameters:

  • payload (Symbol)

    Body of the API response

Returns:

  • (Object)

    Instance of the class specified in body



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/elvanto/resources/resource.rb', line 67

def construct_from_response(payload)

  payload = ElvantoAPI::Utils.indifferent_read_access payload
  # the remaining keys here are just hypermedia resources
  payload.slice!(member_name)

  instance = nil
  
  payload.each do |key, value|
    if value.class == Hash
      resource_body = value
    else
      resource_body = value.first
    end
    # > Singular resources are represented as JSON objects. However,
    # they are still wrapped inside an array:
    #resource_body = value.first
    cls = ("ElvantoAPI::" + ElvantoAPI::Utils.classify(key)).constantize
    instance = cls.new resource_body
  end
  instance
  
end

#def_instance_methods(instance_methods = {}) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/elvanto/resources/resource.rb', line 115

def def_instance_methods instance_methods={}
  instance_methods.each do |key, value|
    define_method(key) do |options={}|
      self.query_member(value, options)
    end
  end
end

#member_method?(method) ⇒ Boolean

Returns True if API method returns a single object, otherwise false.

Parameters:

  • method (Symbol)

    The name of the method to call

Returns:

  • (Boolean)

    True if API method returns a single object, otherwise false



93
94
95
96
# File 'lib/elvanto/resources/resource.rb', line 93

def member_method? method
  return unless defined? resource_class::MEMBER_METHODS
  resource_class::MEMBER_METHODS.keys.include? method
end

#member_nameObject



61
62
63
# File 'lib/elvanto/resources/resource.rb', line 61

def member_name
  Utils.underscore resource_name
end

#query_collection(method, options = {}) ⇒ Array

Returns The response from the API method.

Returns:

  • (Array)

    The response from the API method.



135
136
137
138
139
# File 'lib/elvanto/resources/resource.rb', line 135

def query_collection(method, options={})
  uri = href + "/" + method.to_s
  pager = ElvantoAPI::Pager.new uri, options
  pager.to_a
end

#query_member(method, options = {}) ⇒ Object

Returns The response from the API method.

Returns:

  • (Object)

    The response from the API method.



126
127
128
129
130
# File 'lib/elvanto/resources/resource.rb', line 126

def query_member(method, options={})
  uri = href + "/" + method.to_s
  response = ElvantoAPI.post uri, options
  construct_from_response response.body         
end

#resource_classObject



47
48
49
# File 'lib/elvanto/resources/resource.rb', line 47

def resource_class
  name.constantize
end

#resource_nameObject



43
44
45
# File 'lib/elvanto/resources/resource.rb', line 43

def resource_name
  Utils.demodulize name
end