Class: Adash::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_model) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:

  • _self (Adash::Client)

    the object that the method was called on



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/adash/client.rb', line 19

def initialize(device_model)
  @drs_host = 'dash-replenishment-service-na.amazon.com'
  @amazon_host = 'api.amazon.com'
  @device_model = device_model
  @serial = nil
  @authorization_code = nil
  @redirect_uri = nil
  @access_token = nil
  @refresh_token = nil
  @on_new_token = nil
  yield(self) if block_given?
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



15
16
17
# File 'lib/adash/client.rb', line 15

def access_token
  @access_token
end

#authorization_codeObject

Returns the value of attribute authorization_code.



15
16
17
# File 'lib/adash/client.rb', line 15

def authorization_code
  @authorization_code
end

#device_modelObject

Returns the value of attribute device_model.



15
16
17
# File 'lib/adash/client.rb', line 15

def device_model
  @device_model
end

#on_new_tokenObject

Returns the value of attribute on_new_token.



16
17
18
# File 'lib/adash/client.rb', line 16

def on_new_token
  @on_new_token
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



15
16
17
# File 'lib/adash/client.rb', line 15

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



15
16
17
# File 'lib/adash/client.rb', line 15

def refresh_token
  @refresh_token
end

#serialObject

Returns the value of attribute serial.



15
16
17
# File 'lib/adash/client.rb', line 15

def serial
  @serial
end

#user_agentObject



32
33
34
# File 'lib/adash/client.rb', line 32

def user_agent
  @user_agent ||= "AdashRubyGem/#{Adash::VERSION}/#{RUBY_DESCRIPTION}"
end

Instance Method Details

#deregistrate_deviceObject



36
37
38
39
40
41
42
43
# File 'lib/adash/client.rb', line 36

def deregistrate_device
  headers = {
    'x-amzn-accept-type': '[email protected]',
    'x-amzn-type-version': '[email protected]'
  }
  path = "/deviceModels/#{@device_model}/devices/#{@serial}/registration"
  request_drs(:delete, path, headers: headers)
end

#device_status(most_recently_active_date) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/adash/client.rb', line 45

def device_status(most_recently_active_date)
  headers = {
    'x-amzn-accept-type': '[email protected]',
    'x-amzn-type-version': '[email protected]'
  }
  path = '/deviceStatus'
  request_drs(:post, path, headers: headers, params: { 'mostRecentlyActiveDate' =>  convert_to_iso8601(most_recently_active_date) })
end

#get_tokenObject



94
95
96
97
98
99
100
101
# File 'lib/adash/client.rb', line 94

def get_token
  if @access_token
    @access_token
  else
    resp = request_token
    process_token_response(resp)
  end
end

#replenish(slot_id) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/adash/client.rb', line 79

def replenish(slot_id)
  headers = {
    'x-amzn-accept-type': '[email protected]',
    'x-amzn-type-version': '[email protected]'
  }
  path = "/replenish/#{slot_id}"
  response = request_drs(:post, path, headers: headers)
  # TODO: imprement response processing
  # https://developer.amazon.com/public/solutions/devices/dash-replenishment-service/docs/dash-replenish-endpoint
  # response examples:
  #   {"message":"Unable to place order - slot : <01234567-89ab-cdef-0123-456789abcdef> is unsubscribed."}
  #   {"detailCode":"STANDARD_ORDER_PLACED","eventInstanceId":"amzn1.dash.v2.o.xxxxxx......"}
  #   {"detailCode":"ORDER_INPROGRESS","eventInstanceId":"amzn1.dash.v2.o.xxxxxx......"}
end

#slot_status(slot_id, expected_replenishment_date, remaining_quantity_in_unit, original_quantity_in_unit, total_quantity_on_hand, last_use_date) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/adash/client.rb', line 63

def slot_status(slot_id, expected_replenishment_date, remaining_quantity_in_unit, original_quantity_in_unit, total_quantity_on_hand, last_use_date)
  headers = {
    'x-amzn-accept-type': '[email protected]',
    'x-amzn-type-version': '[email protected]'
  }
  path = "/slotStatus/#{slot_id}"
  params = {
    'expectedReplenishmentDate' => convert_to_iso8601(expected_replenishment_date),
    'remainingQuantityInUnit' => remaining_quantity_in_unit,
    'originalQuantityInUnit' => original_quantity_in_unit,
    'totalQuantityOnHand' => total_quantity_on_hand,
    'lastUseDate' => convert_to_iso8601(last_use_date)
  }
  request_drs(:post, path, headers: headers, params: params)
end

#subscription_infoObject



54
55
56
57
58
59
60
61
# File 'lib/adash/client.rb', line 54

def subscription_info
  headers = {
    'x-amzn-accept-type': '[email protected]',
    'x-amzn-type-version': '[email protected]'
  }
  path = '/subscriptionInfo'
  request_drs(:get, path, headers: headers)
end