Class: Rester::Service::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rester/service/resource.rb,
lib/rester/service/resource/params.rb

Defined Under Namespace

Classes: Params

Constant Summary collapse

REQUEST_METHOD_TO_IDENTIFIED_METHOD =
{
  'GET'    => :get,
  'PUT'    => :update,
  'DELETE' => :delete
}.freeze
REQUEST_METHOD_TO_UNIDENTIFIED_METHOD =
{
  'GET'    => :search,
  'POST'   => :create
}.freeze
RESOURCE_METHODS =
[:search, :create, :get, :update, :delete].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.id(name) ⇒ Object

Specify the name of your identifier (Default: ‘id’)



28
29
30
# File 'lib/rester/service/resource.rb', line 28

def id(name)
  @id_name = name.to_sym
end

.id_nameObject



49
50
51
# File 'lib/rester/service/resource.rb', line 49

def id_name
  @id_name ||= :id
end

.id_paramObject



53
54
55
# File 'lib/rester/service/resource.rb', line 53

def id_param
  "#{self.name.split('::').last.underscore}_#{id_name}"
end

.method_added(method_name) ⇒ Object



65
66
67
68
69
70
# File 'lib/rester/service/resource.rb', line 65

def method_added(method_name)
  if RESOURCE_METHODS.include?(method_name.to_sym)
    method_params[method_name.to_sym] = (@_next_params || Params.new).freeze
  end
  @_next_params = nil
end

.method_paramsObject



61
62
63
# File 'lib/rester/service/resource.rb', line 61

def method_params
  @_method_params ||= {}
end

.mount(klass) ⇒ Object

Mount another Service Resource



34
35
36
37
38
# File 'lib/rester/service/resource.rb', line 34

def mount(klass)
  raise "Only other Service Resources can be mounted." unless klass < Resource
  start = self.name.split('::')[0..-2].join('::').length + 2
  mounts[klass.name[start..-1].pluralize.underscore] = klass
end

.mountsObject



57
58
59
# File 'lib/rester/service/resource.rb', line 57

def mounts
  (@__mounts ||= {})
end

.params(opts = {}, &block) ⇒ Object



40
41
42
# File 'lib/rester/service/resource.rb', line 40

def params(opts={}, &block)
  @_next_params = Params.new(opts, &block)
end

Instance Method Details

#error!(message = nil) ⇒ Object



99
100
101
# File 'lib/rester/service/resource.rb', line 99

def error!(message=nil)
  Errors.throw_error!(Errors::RequestError, message)
end

#id_paramObject

Class Methods



73
74
75
# File 'lib/rester/service/resource.rb', line 73

def id_param
  self.class.id_param
end

#method_params_for(method_name) ⇒ Object



95
96
97
# File 'lib/rester/service/resource.rb', line 95

def method_params_for(method_name)
  self.class.method_params[method_name.to_sym]
end

#mountsObject



91
92
93
# File 'lib/rester/service/resource.rb', line 91

def mounts
  self.class.mounts
end

#process(request_method, id_provided, params = {}) ⇒ Object

Given an HTTP request method, calls the appropriate calls the appropriate instance method. ‘id_provided` specifies whether on not the ID for the object is included in the params hash. This will be used when determining which instance method to call. For example, if the request method is GET: the ID being specified will call the `get` method and if it’s not specified then it will call the ‘search` method.



84
85
86
87
88
89
# File 'lib/rester/service/resource.rb', line 84

def process(request_method, id_provided, params={})
  meth = (id_provided ? REQUEST_METHOD_TO_IDENTIFIED_METHOD
    : REQUEST_METHOD_TO_UNIDENTIFIED_METHOD)[request_method]

  _process(meth, params).to_h
end