Class: APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/allegro_api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(http_client: HTTParty) ⇒ APIClient

Returns a new instance of APIClient.



6
7
8
9
10
# File 'lib/allegro_api_client.rb', line 6

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



17
18
19
# File 'lib/allegro_api_client.rb', line 17

def delete(path:path)
  @http_client.delete("#{@base_uri}#{encoded path}", {basic_auth:@auth})
end

#encoded(path) ⇒ Object



31
32
33
# File 'lib/allegro_api_client.rb', line 31

def encoded path
  URI.escape(path)
end

#get(path: path) ⇒ Object



12
13
14
15
# File 'lib/allegro_api_client.rb', line 12

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



21
22
23
24
# File 'lib/allegro_api_client.rb', line 21

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



26
27
28
29
# File 'lib/allegro_api_client.rb', line 26

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