Class: RakutenProductApi::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rakuten_product_api/client.rb

Constant Summary collapse

REFRESH_TOKEN_LEEWAY =

Ten minutes prior to expiry we should refresh token

60 * 10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sid: RakutenProductApi.sid, endpoint: RakutenProductApi.endpoint, access_token: nil, access_token_expires_at: nil) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rakuten_product_api/client.rb', line 15

def initialize(sid: RakutenProductApi.sid,
               endpoint: RakutenProductApi.endpoint,
               access_token: nil,
               access_token_expires_at: nil)
  
  @sid = sid
  @endpoint = endpoint

  @authenticate = Authenticate.new(sid: sid,
                                   access_token: access_token,
                                   access_token_expires_at: access_token_expires_at)

end

Instance Attribute Details

#authenticateObject

Returns the value of attribute authenticate.



13
14
15
# File 'lib/rakuten_product_api/client.rb', line 13

def authenticate
  @authenticate
end

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'lib/rakuten_product_api/client.rb', line 13

def password
  @password
end

#sidObject

Returns the value of attribute sid.



13
14
15
# File 'lib/rakuten_product_api/client.rb', line 13

def sid
  @sid
end

#usernameObject

Returns the value of attribute username.



13
14
15
# File 'lib/rakuten_product_api/client.rb', line 13

def username
  @username
end

Instance Method Details

#api_request(payload, path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rakuten_product_api/client.rb', line 35

def api_request(payload, path)
  params = payload.map { |k, v| "#{k}=#{v}" }.join("&")

  uri = URI("#{@endpoint}#{path}?#{params}")

  res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    req = Net::HTTP::Get.new(uri)
    req["Authorization"] = @authenticate.auth_header
    req["Accept"] = "application/xml"

    http.request(req)
  end

  puts "RESPONSE CODE #{res.code} received" if res.code != "200"

  res
end

#search(**options) ⇒ Object



29
30
31
32
33
# File 'lib/rakuten_product_api/client.rb', line 29

def search(**options)
  params = %i{ keyword exact one none cat language max pagenumber mid, sort, sorttype }
  allowed_options = options.select {|k, v| params.include?(k)}
  Response.new(api_request(options, '/productsearch/1.0'))
end