vfnetapis 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +48 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +63 -0
- data/Rakefile +2 -0
- data/lib/vfnetapis/version.rb +3 -0
- data/lib/vfnetapis.rb +80 -0
- data/vfnetapis.gemspec +20 -0
- metadata +55 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p125@vfnetapis"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.10.3" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
else
|
29
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
30
|
+
rvm --create "$environment_id" || {
|
31
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
32
|
+
return 1
|
33
|
+
}
|
34
|
+
fi
|
35
|
+
|
36
|
+
# If you use bundler, this might be useful to you:
|
37
|
+
# if [[ -s Gemfile ]] && {
|
38
|
+
# ! builtin command -v bundle >/dev/null ||
|
39
|
+
# builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
|
40
|
+
# }
|
41
|
+
# then
|
42
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
43
|
+
# gem install bundler
|
44
|
+
# fi
|
45
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
46
|
+
# then
|
47
|
+
# bundle install | grep -vE '^Using|Your bundle is complete'
|
48
|
+
# fi
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 OneAPI
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Vfnetapis
|
2
|
+
|
3
|
+
Simple OAUTH and OneAPI calls against Vodafone's OneAPI endpoint
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'vfnetapis'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install vfnetapis
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
USAGE: Initialize with Vfnetapis::Location.new(application key, callbackURI) , then location(MSISDN, requested accuracy in metres)
|
22
|
+
|
23
|
+
The gem returns lat/long based on mobile network location, incorporating and greatly simplifying the OAUTH flow to gain user consent.
|
24
|
+
|
25
|
+
The result is a JSON location based on the OneAPI location API, http://oneapi.gsma.com/reference-location-restful-api/
|
26
|
+
|
27
|
+
For convenience the JSON is pre-parsed into several instance variables, see example below.
|
28
|
+
|
29
|
+
For this version the callbackURI must points to a web service with specific routes,
|
30
|
+
to trap the authorization and access token flows. See https://github.com/OneAPI/NetworksAPIs/ruby/auth_trap.rb
|
31
|
+
for a Sinatra implementation.
|
32
|
+
|
33
|
+
EXAMPLE:
|
34
|
+
require 'vfnetapis'
|
35
|
+
|
36
|
+
# 1. run auth_trap.rb from https://github.com/OneAPI/NetworksAPIs/ruby
|
37
|
+
|
38
|
+
# 2. initialise with your application key and use the auth_trap.rb as the callback URI
|
39
|
+
loc=Vfnetapis::Location.new({my_app_key},'http://localhost:4567/tokenStore')
|
40
|
+
|
41
|
+
# 3. make the location request, params are MSISDN in tel:441234567890 format, and requested accuracy in metres
|
42
|
+
loc.location({MSISDN},{metres})
|
43
|
+
|
44
|
+
# 4. Do something with the results!
|
45
|
+
puts 'Results for address: ' + loc.address
|
46
|
+
puts 'longitude is ' + loc.longitude
|
47
|
+
puts 'latitude is ' + loc.latitude
|
48
|
+
puts 'altitude is ' + loc.altitude
|
49
|
+
puts 'accuracy is ' + loc.accuracy
|
50
|
+
puts 'timestamp is ' + loc.timestamp
|
51
|
+
|
52
|
+
puts 'the full response is: ' + loc.response.to_s # => the raw JSON if you want it...
|
53
|
+
|
54
|
+
puts 'the access token is: ' + loc.access_token
|
55
|
+
puts 'the access token expiry is: ' + loc.expires_in"
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/vfnetapis.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require "vfnetapis/version"
|
2
|
+
require 'rest-client'
|
3
|
+
require 'JSON'
|
4
|
+
require 'launchy'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
module Vfnetapis
|
8
|
+
class Location
|
9
|
+
attr_accessor :key
|
10
|
+
attr_accessor :redirectURI
|
11
|
+
attr_accessor :requestId
|
12
|
+
attr_accessor :auth_token
|
13
|
+
attr_accessor :access_token
|
14
|
+
attr_accessor :expires_in
|
15
|
+
attr_reader :longitude
|
16
|
+
attr_reader :latitude
|
17
|
+
attr_reader :altitude
|
18
|
+
attr_reader :address
|
19
|
+
attr_reader :accuracy
|
20
|
+
attr_reader :timestamp
|
21
|
+
attr_reader :response
|
22
|
+
@@loc_api="http://79.125.107.189/v2/location/queries/location"
|
23
|
+
@@auth_server = "http://79.125.107.189/2/oauth/authorize"
|
24
|
+
@@acc_server = "http://176.34.213.154/2/oauth/access_token?client_id="
|
25
|
+
|
26
|
+
def initialize(key,redirectURI) # => set up tokens ready for request
|
27
|
+
@scope = "GET-/location/queries/location" # "POST-/payment/acr:Authorization/transactions/amount"
|
28
|
+
@key = key
|
29
|
+
@redirectURI = redirectURI
|
30
|
+
@requestId = generateActivationCode()
|
31
|
+
end
|
32
|
+
|
33
|
+
def location(address, requested_accuracy)
|
34
|
+
@auth_token = get_auth_token(@redirectURI,@scope)
|
35
|
+
puts 'Auth Token is:' + @auth_token
|
36
|
+
@access_token = get_access_token(@auth_token)
|
37
|
+
puts 'Access Token is:' + @access_token
|
38
|
+
|
39
|
+
@response = RestClient.get @@loc_api, :authorization=> 'Oauth ' + @access_token, :params => {:address => URI.escape(address,':'), :requestedAccuracy => requested_accuracy}
|
40
|
+
|
41
|
+
resJson = JSON.parse(@response)
|
42
|
+
@longitude = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["longitude"]
|
43
|
+
@latitude = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["latitude"]
|
44
|
+
@altitude = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["altitude"]
|
45
|
+
@accuracy = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["accuracy"]
|
46
|
+
@timestamp = resJson["terminalLocationList"]["terminalLocation"]["currentLocation"]["timestamp"]
|
47
|
+
@address = resJson["terminalLocationList"]["terminalLocation"]["address"]
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_auth_token(redirectURI,scope) # => start consent flow to send code to redirectURI
|
51
|
+
@uri = @@auth_server + '?client_id=' + @key + '&redirect_uri=' + URI.escape(redirectURI + '/' + @requestId, '/:&\?\.@') + '&scope=' + URI.escape(scope,'/:')
|
52
|
+
Launchy.open(@uri) # => opens a browser to acquire consent then redirects to the callback
|
53
|
+
while true # => poll to check user consent received by external listener
|
54
|
+
begin
|
55
|
+
@auth_token = RestClient.get redirectURI + '/authToken/' + requestId
|
56
|
+
break if !@auth_token.empty?
|
57
|
+
rescue
|
58
|
+
$stderr.puts "failed to get access token"
|
59
|
+
retry
|
60
|
+
end
|
61
|
+
sleep 1
|
62
|
+
end
|
63
|
+
@auth_token
|
64
|
+
end
|
65
|
+
|
66
|
+
def get_access_token(authorizationCode)
|
67
|
+
uri = @@acc_server + @key
|
68
|
+
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
|
69
|
+
# this needs to change in order to get the JSON back
|
70
|
+
resJson = JSON.parse(accessResponse)
|
71
|
+
@expires_in = resJson["expires_in"]
|
72
|
+
@accessToken = resJson["access_token"]
|
73
|
+
end
|
74
|
+
|
75
|
+
def generateActivationCode(size= 6)
|
76
|
+
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}
|
77
|
+
(0...size).map{ charset.to_a[rand(charset.size)] }.join
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/vfnetapis.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/vfnetapis/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["OneAPI"]
|
6
|
+
gem.email = ["kevin.smith@vodafone.com"]
|
7
|
+
gem.description = "OAUTH with OneAPI calls against Vodafone's OneAPI endpoint."
|
8
|
+
gem.summary = "Network API helpers for reading a user's location and brokering the OAUTH dance."
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "vfnetapis"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Vfnetapis::VERSION
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vfnetapis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- OneAPI
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-30 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: OAUTH with OneAPI calls against Vodafone's OneAPI endpoint.
|
15
|
+
email:
|
16
|
+
- kevin.smith@vodafone.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rvmrc
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/vfnetapis.rb
|
28
|
+
- lib/vfnetapis/version.rb
|
29
|
+
- vfnetapis.gemspec
|
30
|
+
homepage: ''
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.18
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Network API helpers for reading a user's location and brokering the OAUTH
|
54
|
+
dance.
|
55
|
+
test_files: []
|