Class: Putson::Client
- Inherits:
-
Object
- Object
- Putson::Client
- Defined in:
- lib/putson/client.rb
Constant Summary collapse
- URL =
'https://api.myjson.com/'.freeze
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Instance Method Summary collapse
- #get(newid = nil) ⇒ Object
-
#initialize(id: nil) ⇒ Client
constructor
A new instance of Client.
- #post(data) ⇒ Object
- #put(data) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(id: nil) ⇒ Client
Returns a new instance of Client.
7 8 9 |
# File 'lib/putson/client.rb', line 7 def initialize(id: nil) @id = id end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/putson/client.rb', line 5 def id @id end |
Instance Method Details
#get(newid = nil) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/putson/client.rb', line 31 def get(newid = nil) @id = newid unless newid.nil? response = client.get do |req| req.url "/bins/#{id}" end response.body end |
#post(data) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/putson/client.rb', line 11 def post(data) data = data.to_json unless data.is_a? String response = client.post do |req| req.url '/bins' req.headers['Content-Type'] = 'application/json' req.body = data end parse_post_response(response) end |
#put(data) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/putson/client.rb', line 21 def put(data) data = data.to_json unless data.is_a? String response = client.put do |req| req.url "/bins/#{id}" req.headers['Content-Type'] = 'application/json' req.body = data end parse_post_response(response) end |
#url ⇒ Object
39 40 41 42 |
# File 'lib/putson/client.rb', line 39 def url return if id.nil? "#{URL}bins/#{id}" end |