Class: APIClient
- Inherits:
-
Object
- Object
- APIClient
- Defined in:
- lib/allegro_api_client.rb
Instance Method Summary collapse
- #delete(path: path) ⇒ Object
- #encoded(path) ⇒ Object
- #get(path: path) ⇒ Object
-
#initialize(http_client: HTTParty) ⇒ APIClient
constructor
A new instance of APIClient.
- #patch(path: path, body: body) ⇒ Object
- #post(path: path, body: body) ⇒ Object
Constructor Details
#initialize(http_client: HTTParty) ⇒ APIClient
Returns a new instance of APIClient.
5 6 7 8 9 |
# File 'lib/allegro_api_client.rb', line 5 def initialize(http_client:HTTParty) @base_uri = ENV['API_URI'] @auth = {username:'snap', password:ENV['AUTH_TOKEN']} @http_client = http_client end |
Instance Method Details
#delete(path: path) ⇒ Object
16 17 18 |
# File 'lib/allegro_api_client.rb', line 16 def delete(path:path) @http_client.delete("#{@base_uri}#{encoded path}", {basic_auth:@auth}) end |
#encoded(path) ⇒ Object
30 31 32 |
# File 'lib/allegro_api_client.rb', line 30 def encoded path path.gsub ' ', '%20' end |
#get(path: path) ⇒ Object
11 12 13 14 |
# File 'lib/allegro_api_client.rb', line 11 def get(path:path) body = @http_client.get("#{@base_uri}#{encoded path}", {basic_auth:@auth}).body JSON.parse(body, quirks_mode:true) end |
#patch(path: path, body: body) ⇒ Object
20 21 22 23 |
# File 'lib/allegro_api_client.rb', line 20 def patch(path:path, body:body) @http_client.patch("#{@base_uri}#{encoded path}", :basic_auth =>@auth, :body => body.to_json, headers:{ 'Content-Type' => 'application/json' }).body end |
#post(path: path, body: body) ⇒ Object
25 26 27 28 |
# File 'lib/allegro_api_client.rb', line 25 def post(path:path, body:body) @http_client.post("#{@base_uri}#{encoded path}", :basic_auth =>@auth, :body => body.to_json, headers:{ 'Content-Type' => 'application/json' }).body end |