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
71
# 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(strict: false)).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!(error, message = nil) ⇒ Object



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

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

#id_paramObject

Class Methods



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

def id_param
  self.class.id_param
end

#method_params_for(method_name) ⇒ Object



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

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

#mountsObject



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

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.



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

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