Class: TWM::API
- Inherits:
-
Object
- Object
- TWM::API
- Defined in:
- lib/twm-ruby.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
-
#create_session(url) ⇒ Object
:nodoc:.
-
#delete(path, params = {}) ⇒ Object
Make a DELETE request with the session.
-
#get(path, params = {}) ⇒ Object
Make a GET request with the session.
- #handle_response(response) ⇒ Object
-
#initialize(debug = false) ⇒ API
constructor
A new instance of API.
-
#post(path, params = {}) ⇒ Object
Make a POST request with the session.
-
#put(path, params = {}) ⇒ Object
Make a PUT request with the session.
-
#set_headers ⇒ Object
Set the request headers.
-
#sightings ⇒ Object
API nodes.
Constructor Details
#initialize(debug = false) ⇒ API
12 13 14 |
# File 'lib/twm-ruby.rb', line 12 def initialize(debug=false) @debug = debug end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
10 11 12 |
# File 'lib/twm-ruby.rb', line 10 def debug @debug end |
#session ⇒ Object
Returns the value of attribute session.
10 11 12 |
# File 'lib/twm-ruby.rb', line 10 def session @session end |
Instance Method Details
#create_session(url) ⇒ Object
:nodoc:
16 17 18 |
# File 'lib/twm-ruby.rb', line 16 def create_session(url) # :nodoc: @session = Faraday.new(url: url, headers: set_headers) end |
#delete(path, params = {}) ⇒ Object
Make a DELETE request with the session
40 41 42 |
# File 'lib/twm-ruby.rb', line 40 def delete(path, params={}) # :nodoc: handle_response(@session.delete(path, MultiJson.dump(params))) end |
#get(path, params = {}) ⇒ Object
Make a GET request with the session
22 23 24 |
# File 'lib/twm-ruby.rb', line 22 def get(path, params={}) # :nodoc: handle_response(@session.get(path, params)) end |
#handle_response(response) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/twm-ruby.rb', line 44 def handle_response(response) case response.status # :nodoc: when 400 raise BadRequest.new response.body when 401 raise .new when 404 raise NotFound.new when 400...500 raise ClientError.new response.body when 500...600 raise ServerError.new else case response.body when '' true when is_a?(Integer) response.body else MultiJson.load(response.body) end end end |
#post(path, params = {}) ⇒ Object
Make a POST request with the session
28 29 30 |
# File 'lib/twm-ruby.rb', line 28 def post(path, params={}) # :nodoc: handle_response(@session.post(path, MultiJson.dump(params))) end |
#put(path, params = {}) ⇒ Object
Make a PUT request with the session
34 35 36 |
# File 'lib/twm-ruby.rb', line 34 def put(path, params={}) # :nodoc: handle_response(@session.put(path, MultiJson.dump(params))) end |
#set_headers ⇒ Object
Set the request headers
69 70 71 72 73 74 75 |
# File 'lib/twm-ruby.rb', line 69 def set_headers # :nodoc: { 'User-Agent' => "twm-ruby-#{VERSION}", 'Content-Type' => 'application/json; charset=utf-8', 'Accept' => 'application/json' } end |
#sightings ⇒ Object
API nodes
78 79 80 |
# File 'lib/twm-ruby.rb', line 78 def sightings # :nodoc: Sightings.new self end |