Module: Served::Resource::Requestable::ClassMethods

Defined in:
lib/served/resource/requestable.rb

Instance Method Summary collapse

Instance Method Details

#all(params = {}) ⇒ Object



124
125
126
# File 'lib/served/resource/requestable.rb', line 124

def all(params = {})
  get(nil, params).map { |resource| new(resource) }
end

#clientServed::HTTPClient

Returns the HTTPClient using the configured backend.

Returns:



129
130
131
# File 'lib/served/resource/requestable.rb', line 129

def client
  @client ||= Served::HTTPClient.new(self)
end

#find(id, params = {}) ⇒ Resource::Base

Looks up a resource on the service by id. For example, ‘SomeResource.find(5)` would call `/some_resources/5`

Parameters:

  • id (Integer)

    the id of the resource

Returns:



119
120
121
122
# File 'lib/served/resource/requestable.rb', line 119

def find(id, params = {})
  instance = new(id: id)
  instance.reload(params)
end

#get(id, params = {}) ⇒ Object



133
134
135
# File 'lib/served/resource/requestable.rb', line 133

def get(id, params = {})
  handle_response(client.get(resource_name, id, params))
end

#handle(code_or_range, symbol_or_proc = nil, &block) ⇒ Object

Sets individual handlers for response codes, accepts a proc or a symbol representing a method

Parameters:

  • code (Integer|Range)

    the response code(s) the handler is to be assigned to

  • symbol_or_proc (Symbol|Proc) (defaults to: nil)

    a symbol representing the method to call, or a proc to be called when the specific response code has been called. The method or proc should return a hash of attributes, if an error class is returned it will be raised

Yield Returns:

  • (Hash)

    a hash of attributes, if an error class is returned it will be raised

Raises:



93
94
95
96
97
98
99
100
101
102
# File 'lib/served/resource/requestable.rb', line 93

def handle(code_or_range, symbol_or_proc = nil, &block)
  raise HandlerRequired unless symbol_or_proc || block_given?
  if code_or_range.is_a?(Range) || code_or_range.is_a?(Array)
    code_or_range.each do |c|
      handlers[c] = symbol_or_proc || block unless handlers.key?(c)
    end
  else
    handlers[code_or_range] = symbol_or_proc || block
  end
end

#handle_response(response) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/served/resource/requestable.rb', line 68

def handle_response(response)
  if raise_on_exceptions
    handler = handlers.fetch(response.code) { proc { HttpError } }
    if handler.is_a? Proc
      result = handler.call(response)
    else
      result = send(handler, response.body)
    end

    if result.respond_to?(:ancestors) && result.ancestors.include?(HttpError)
      raise result.new(response.code, self, response)
    end
    result
  else
    serializer.load(self, response)
  end
end

#headers(h = {}) ⇒ Object

Defines the default headers that should be used for the request.

Parameters:

  • headers (Hash)

    the headers to send with each requesat



108
109
110
111
112
# File 'lib/served/resource/requestable.rb', line 108

def headers(h = {})
  headers ||= _headers
  _headers(headers.merge!(h)) unless h.empty?
  _headers
end