Module: Remotely::HTTPMethods

Included in:
Model
Defined in:
lib/remotely/http_methods.rb

Constant Summary collapse

SUCCESS_STATUSES =

HTTP status codes that are represent successful requests

(200..299)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appSymbol #app(name) ⇒ Symbol

Set or get the app for this model belongs to. If name is passed, it’s a setter, otherwise, a getter.

In getter form, if a model didn’t declare which app it is associated with and there is only one registered app, it will default to that app.

Overloads:

  • #appSymbol

    Gets the current ‘app` value.

  • #app(name) ⇒ Symbol

    Sets the value of ‘app`.

    Parameters:

    • name (Symbol)

      Name corresponding to an app defined via Remotely.app.

Returns:

  • (Symbol)

    New app symbol or current value.



7
8
9
# File 'lib/remotely/http_methods.rb', line 7

def app
  @app
end

#uriString #uri(path) ⇒ String

Set or get the base uri for this model. If name is passed, it’s a setter, otherwise, a getter.

Overloads:

  • #uriString

    Gets the current ‘uri` value.

  • #uri(path) ⇒ String

    Sets the value of ‘uri`.

    Parameters:

    • path (Symbol)

      Relative path to this type of resource.

Returns:

  • (String)

    New uri or current value.



10
11
12
# File 'lib/remotely/http_methods.rb', line 10

def uri
  @uri
end

Instance Method Details

#before_request(uri, http_verb = :get, options = {}) ⇒ Object

Gets called before a request. Override to add logging, etc.



142
143
144
145
146
147
# File 'lib/remotely/http_methods.rb', line 142

def before_request(uri, http_verb = :get, options = {})
  if ENV['REMOTELY_DEBUG']
    puts "-> #{http_verb.to_s.upcase} #{uri}"
    puts "   #{options.inspect}"
  end
end

#expand(path) ⇒ Object

Remove the leading slash because Faraday considers it to be absolute path and ignores any prefixes. eg:

c = Faraday::Connection.new("http://foo.com/api")
c.get("users")  # => /api/users (Good)
c.get("/users") # => /users     (Bad)

Examples:

Remotely.configure { app :thingapp, "http://example.com/api" }
Model.expand("/members") # => "members"


136
137
138
# File 'lib/remotely/http_methods.rb', line 136

def expand(path)
  path.gsub(%r(^/), "")
end

#get(path, options = {}) ⇒ Remotely::Collection, ...

GET request.

Parameters:

  • uri (String)

    Relative path of request.

  • params (Hash)

    Query string, in key-value Hash form.

Returns:



57
58
59
60
61
62
63
64
65
66
# File 'lib/remotely/http_methods.rb', line 57

def get(path, options={})
  path     = expand(path)
  klass    = options.delete(:class)
  parent   = options.delete(:parent)

  before_request(path, :get, options)

  response = app.connection.get { |req| req.url(path, options) }
  parse_response(raise_if_html(response), klass, parent)
end

#http_delete(path) ⇒ Boolean

DELETE request.

Parameters:

  • uri (String)

    Relative path of request.

Returns:

  • (Boolean)

    Was the resource deleted? (Resulted in a 200-299 response code)



118
119
120
121
122
123
# File 'lib/remotely/http_methods.rb', line 118

def http_delete(path)
  path = expand(path)
  before_request(path, :delete)
  response = raise_if_html(app.connection.delete(path))
  SUCCESS_STATUSES.include?(response.status)
end

#parse_response(response, klass = nil, parent = nil) ⇒ Remotely::Collection, ...

Parses the response depending on what was returned. The following table described what gets return in what situations.

————------------------————– Status Code | Return Body Type | Return Value ————------------------————–

>= 400    |       N/A        |    false

————------------------————–

200-299   |      Array       |  Collection

————------------------————–

200-299   |      Hash        |     Model

————------------------————–

200-299   |      Other       | Parsed JSON

————------------------————–

Parameters:

  • response (Faraday::Response)

    Response object

Returns:



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/remotely/http_methods.rb', line 177

def parse_response(response, klass=nil, parent=nil)
  return false if response.status >= 400

  body  = MultiJson.load(response.body) rescue nil
  klass = (klass || self)

  case body
  when Array
    Collection.new(parent, klass, body.map { |o| klass.new(o) })
  when Hash
    klass.new(body)
  else
    body
  end
end

#post(path, options = {}) ⇒ Remotely::Collection, ...

POST request.

Used mainly to create new resources. Remotely assumes that the remote API will return the newly created object, in JSON form, with the ‘id` assigned to it.

Parameters:

  • uri (String)

    Relative path of request.

  • params (Hash)

    Request payload. Gets JSON-encoded.

Returns:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/remotely/http_methods.rb', line 81

def post(path, options={})
  path      = expand(path)
  klass     = options.delete(:class)
  parent    = options.delete(:parent)
  body      = options.delete(:body) || MultiJson.dump(options)
  headers   = options.delete(:headers) || {}
  headers['Content-Type'] ||= 'application/json'

  before_request(path, :post, body)
  raise_if_html(app.connection.post(path, body, headers))
end

#put(path, options = {}) ⇒ Boolean

PUT request.

Parameters:

  • uri (String)

    Relative path of request.

  • params (Hash)

    Request payload. Gets JSON-encoded.

Returns:

  • (Boolean)

    Was the request successful? (Resulted in a 200-299 response code)



101
102
103
104
105
106
107
108
109
# File 'lib/remotely/http_methods.rb', line 101

def put(path, options={})
  path = expand(path)
  body = options.delete(:body) || MultiJson.dump(options)
  headers   = options.delete(:headers) || {}
  headers['Content-Type'] ||= 'application/json'

  before_request(path, :put, body)
  raise_if_html(app.connection.put(path, body, headers))
end

#raise_if_html(response) ⇒ Object



149
150
151
152
153
154
# File 'lib/remotely/http_methods.rb', line 149

def raise_if_html(response)
  if response.body =~ %r(<html.*?>)i
    raise Remotely::NonJsonResponseError.new(response.body)
  end
  response
end