Class: Vfnetapis::Location

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

Constant Summary collapse

@@loc_api =
"http://api.developer.vodafone.com/v2/location/queries/location"
@@auth_server =
"http://79.125.107.189/2/oauth/authorize"
@@acc_server =
"http://176.34.213.154/2/oauth/access_token?client_id="

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, redirectURI) ⇒ Location

> set up tokens ready for request



28
29
30
31
32
33
# File 'lib/vfnetapis.rb', line 28

def initialize(key,redirectURI) # => set up tokens ready for request
 @scope = "GET-/location/queries/location" # "POST-/payment/acr:Authorization/transactions/amount"    
 @key          = key
 @redirectURI  = redirectURI
 @requestId    = generateActivationCode()
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



14
15
16
# File 'lib/vfnetapis.rb', line 14

def access_token
  @access_token
end

#accuracyObject (readonly)

Returns the value of attribute accuracy.



20
21
22
# File 'lib/vfnetapis.rb', line 20

def accuracy
  @accuracy
end

#addressObject (readonly)

Returns the value of attribute address.



19
20
21
# File 'lib/vfnetapis.rb', line 19

def address
  @address
end

#altitudeObject (readonly)

Returns the value of attribute altitude.



18
19
20
# File 'lib/vfnetapis.rb', line 18

def altitude
  @altitude
end

#auth_tokenObject

Returns the value of attribute auth_token.



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

def auth_token
  @auth_token
end

#expires_inObject

Returns the value of attribute expires_in.



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

def expires_in
  @expires_in
end

#keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/vfnetapis.rb', line 10

def key
  @key
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



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

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



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

def longitude
  @longitude
end

#redirectURIObject

Returns the value of attribute redirectURI.



11
12
13
# File 'lib/vfnetapis.rb', line 11

def redirectURI
  @redirectURI
end

#requestIdObject

Returns the value of attribute requestId.



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

def requestId
  @requestId
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

Instance Method Details

#generateActivationCode(size = 6) ⇒ Object



81
82
83
84
# File 'lib/vfnetapis.rb', line 81

def generateActivationCode(size= 6)
    charset = %w{ 2 3 4 6 7 9 A C D E F G H J K M N P Q R T V W X Y Z}
 	 (0...size).map{ charset.to_a[rand(charset.size)] }.join
end

#get_access_token(authorizationCode) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/vfnetapis.rb', line 72

def get_access_token(authorizationCode)
uri = @@acc_server + @key
accessResponse = RestClient.post uri, :redirect_uri => URI.escape(redirectURI, '/:&\?\.@') , :grant_type => 'authorization_code', :response_type => 'client_credentials', :code => authorizationCode, :content_type => 'application/x-www-form-urlencoded', :accept => :json
# this needs to change in order to get the JSON back
resJson = JSON.parse(accessResponse)
@expires_in = resJson["expires_in"]
 @accessToken = resJson["access_token"]
end

#get_auth_token(redirectURI, scope) ⇒ Object

> start consent flow to send code to redirectURI



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vfnetapis.rb', line 56

def get_auth_token(redirectURI,scope) # => start consent flow to send code to redirectURI
 @uri = @@auth_server + '?client_id=' + @key + '&redirect_uri=' + URI.escape(redirectURI + '/' + @requestId, '/:&\?\.@') + '&scope=' + URI.escape(scope,'/:')
 Launchy.open(@uri) # => opens a browser to acquire consent then redirects to the callback
 while true # => poll to check user consent received by external listener
begin
@auth_token = RestClient.get redirectURI + '/authToken/' + requestId
break if !@auth_token.empty?
rescue
$stderr.puts "failed to get access token"
retry
end
sleep 1
	 end
	 @auth_token
end

#location(address, requested_accuracy) ⇒ Object



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

def location(address, requested_accuracy)
 @auth_token   = get_auth_token(@redirectURI,@scope)
 puts 'Auth Token is:' + @auth_token
 @access_token = get_access_token(@auth_token)
 puts 'Access Token is:' + @access_token
 
 @response = RestClient.get @@loc_api, :authorization=> 'Oauth ' + @access_token, :params => {:address => URI.escape(address,':'), :requestedAccuracy => requested_accuracy}
 resJson = JSON.parse(@response)
 
 if resJson["terminalLocationList"]["terminalLocation"]["locationRetrievalStatus"] == "NotRetrieved"    
		 @longitude = @latitude = @altitude = "Not Found"
	 else
     @longitude = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["longitude"][0..8]
		 @latitude  = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["latitude"][0..8]
		 @altitude  = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["altitude"]
		 @accuracy  = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["accuracy"]
		 @timestamp = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["timestamp"]
 end 
		 @address = resJson["terminalLocationList"]["terminalLocation"]["address"]
end