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.
142 143 144 145 146 147 |
# File 'lib/remotely/http_methods.rb', line 142 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)
136 137 138 |
# File 'lib/remotely/http_methods.rb', line 136 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.
118 119 120 121 122 123 |
# File 'lib/remotely/http_methods.rb', line 118 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
————------------------————–
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.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/remotely/http_methods.rb', line 81 def post(path, ={}) path = (path) klass = .delete(:class) parent = .delete(:parent) body = .delete(:body) || MultiJson.dump() headers = .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.
101 102 103 104 105 106 107 108 109 |
# File 'lib/remotely/http_methods.rb', line 101 def put(path, ={}) path = (path) body = .delete(:body) || MultiJson.dump() headers = .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 |