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
-
#app(name = nil) ⇒ Symbol
Set or get the app for this model belongs to.
-
#uri(path = nil) ⇒ String
Set or get the base uri for this model.
Instance Method Summary collapse
-
#before_request(uri, http_verb = :get, options = {}) ⇒ Object
Gets called before a request.
-
#expand(path) ⇒ Object
Remove the leading slash because Faraday considers it to be absolute path and ignores any prefixes.
-
#get(path, options = {}) ⇒ Remotely::Collection, ...
GET request.
-
#http_delete(path) ⇒ Boolean
DELETE request.
-
#parse_response(response, klass = nil, parent = nil) ⇒ Remotely::Collection, ...
Parses the response depending on what was returned.
-
#post(path, options = {}) ⇒ Remotely::Collection, ...
POST request.
-
#put(path, options = {}) ⇒ Boolean
PUT request.
- #raise_if_html(response) ⇒ Object
Instance Attribute Details
#app ⇒ Symbol #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.
7 8 9 |
# File 'lib/remotely/http_methods.rb', line 7 def app @app end |
#uri ⇒ String #uri(path) ⇒ String
Set or get the base uri for this model. If name is passed, it’s a setter, otherwise, a getter.
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.
138 139 140 141 142 143 |
# File 'lib/remotely/http_methods.rb', line 138 def before_request(uri, http_verb = :get, = {}) 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)
132 133 134 |
# File 'lib/remotely/http_methods.rb', line 132 def (path) path.gsub(%r(^/), "") end |
#get(path, options = {}) ⇒ Remotely::Collection, ...
GET request.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/remotely/http_methods.rb', line 57 def get(path, ={}) path = (path) klass = .delete(:class) parent = .delete(:parent) before_request(path, :get, ) response = app.connection.get { |req| req.url(path, ) } parse_response(raise_if_html(response), klass, parent) end |
#http_delete(path) ⇒ Boolean
DELETE request.
114 115 116 117 118 119 |
# File 'lib/remotely/http_methods.rb', line 114 def http_delete(path) path = (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
————------------------————–
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/remotely/http_methods.rb', line 173 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.
81 82 83 84 85 86 87 88 89 |
# File 'lib/remotely/http_methods.rb', line 81 def post(path, ={}) path = (path) klass = .delete(:class) parent = .delete(:parent) body = .delete(:body) || MultiJson.dump() before_request(path, :post, body) raise_if_html(app.connection.post(path, body)) end |
#put(path, options = {}) ⇒ Boolean
PUT request.
99 100 101 102 103 104 105 |
# File 'lib/remotely/http_methods.rb', line 99 def put(path, ={}) path = (path) body = .delete(:body) || MultiJson.dump() before_request(path, :put, body) raise_if_html(app.connection.put(path, body)) end |
#raise_if_html(response) ⇒ Object
145 146 147 148 149 150 |
# File 'lib/remotely/http_methods.rb', line 145 def raise_if_html(response) if response.body =~ %r(<html>) raise Remotely::NonJsonResponseError.new(response.body) end response end |