Class: AlexaToolbox::Address

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

Overview

Handles the retrieving address information about a device.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consent_token = nil, device_id = nil, app_location = "US", full_address = false) ⇒ Address

Returns a new instance of Address.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/alexa_toolbox/address.rb', line 8

def initialize (consent_token = nil, device_id = nil, app_location = "US", full_address = false)
  @consent_token = consent_token
  @device_id = device_id
  @country_code = nil
  @postal_code = nil
  @city = nil
  @state_or_region = nil
  @address_line_1 = nil
  @address_line_2 = nil
  @address_line_3 = nil
  @district_or_county = nil
  @status = nil
  @full_address = full_address
  @app_location = app_location
  @json = nil
end

Instance Attribute Details

#app_locationObject

Returns the value of attribute app_location.



6
7
8
# File 'lib/alexa_toolbox/address.rb', line 6

def app_location
  @app_location
end

Returns the value of attribute consent_token.



6
7
8
# File 'lib/alexa_toolbox/address.rb', line 6

def consent_token
  @consent_token
end

#device_idObject

Returns the value of attribute device_id.



6
7
8
# File 'lib/alexa_toolbox/address.rb', line 6

def device_id
  @device_id
end

#full_addressObject

Returns the value of attribute full_address.



6
7
8
# File 'lib/alexa_toolbox/address.rb', line 6

def full_address
  @full_address
end

#jsonObject (readonly)

Returns the value of attribute json.



5
6
7
# File 'lib/alexa_toolbox/address.rb', line 5

def json
  @json
end

Instance Method Details

#add_device(device) ⇒ Object



34
35
36
# File 'lib/alexa_toolbox/address.rb', line 34

def add_device(device)
  @device_id = device
end

#add_request(request) ⇒ Object



25
26
27
28
# File 'lib/alexa_toolbox/address.rb', line 25

def add_request(request)
  @consent_token = request.session.consent_token
  @device_id = request.session.device_id
end

#add_token(token) ⇒ Object



30
31
32
# File 'lib/alexa_toolbox/address.rb', line 30

def add_token(token)
  @consent_token = token
end

#base_uriObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/alexa_toolbox/address.rb', line 93

def base_uri
  case self.app_location
  when "US"
    return "https://api.amazonalexa.com"
  when "UK"
    return "https://api.eu.amazonalexa.com"
  when "DE"
    return "https://api.eu.amazonalexa.com"
  else
    return nil
  end
end

#execute_request(endpoint, token) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/alexa_toolbox/address.rb', line 68

def execute_request(endpoint,token)
  uri = URI(endpoint)

  Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    request = Net::HTTP::Get.new(uri)
    request['Authorization'] = token

    response = http.request(request)
    self.status = response.code
    self.json = response.body
    if response.code == 200
      res_body = response.body
      @country_code = res_body.key?("countryCode") ? res_body["countryCode"] : self.country_code
      @postal_code = res_body.key?("postalCode") ? res_body["postalCode"] : self.postal_code
      @city = res_body.key?("city") ? res_body["city"] : self.city
      @state_or_region = res_body.key?("stateOrRegion") ? res_body["stateOrRegion"] : self.state_or_region
      @address_line_1 = res_body.key?("addressLine1") ? res_body["addressLine1"] : self.address_line_1
      @address_line_2 = res_body.key?("addressLine2") ? res_body["addressLine2"] : self.address_line_2
      @address_line_3 = res_body.key?("addressLine3") ? res_body["addressLine3"] : self.address_line_3
      @district_or_county = res_body.key?("districtOrCounty") ? res_body["districtOrCounty"] : self.district_or_county
    else
    end
  end
end

#retrieve_full_address(consent_token = nil, device_id = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/alexa_toolbox/address.rb', line 38

def retrieve_full_address(consent_token = nil, device_id = nil)
  token = consent_token.nil? ? @consent_token : consent_token
  device = device_id.nil? ? @device_id : device_id
  if !token.nil? && !device.nil?
    if self.base_uri.nil?
      raise ArgumentError, 'Invalid Request, Invalid app_location set'
    else
      full_uri = self.base_uri + "/v1/devices/" + device + "/settings/address"
      self.execute_request(full_uri,token)
    end
  else
    raise ArgumentError, 'Invalid Request, No Token or Device'
  end
end

#retrieve_partial_address(consent_token = nil, device_id = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/alexa_toolbox/address.rb', line 53

def retrieve_partial_address(consent_token = nil, device_id = nil)
  token = consent_token.nil? ? @consent_token : consent_token
  device = device_id.nil? ? @device_id : device_id
  if !token.nil? && !device.nil?
    if self.base_uri.nil?
      raise ArgumentError, 'Invalid Request, Invalid app_location set'
    else
      full_uri = self.base_uri + "/v1/devices/" + device + "/settings/address/countryAndPostalCode"
      self.execute_request(full_uri,token)
    end
  else
    raise ArgumentError, 'Invalid Request, No Token or Device'
  end
end