tam 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  pkg
2
2
  Gemfile.lock
3
+ coverage
3
4
 
data/README.mkd CHANGED
@@ -56,3 +56,11 @@ If you are using rails then:
56
56
 
57
57
  end
58
58
  ```
59
+
60
+ 5. The routes exposed by the gem in your rails application are:
61
+
62
+ ```ruby
63
+ /tamapi/authorize
64
+ /tamapi/receive_sms # use this one (complement to form absolute path) as SMS URL when registering your application in telcoassetmarketplace.com
65
+ /tamapi/oauth_callback # use this one (complement to form absolute path) as OAUTH callback URL when registering your application in telcoassetmarketplace.com
66
+ ```
data/Rakefile CHANGED
@@ -28,4 +28,13 @@ end
28
28
  desc "Clean automatically generated files"
29
29
  task :clean do
30
30
  FileUtils.rm_rf "pkg"
31
- end
31
+ end
32
+
33
+ require 'spec/rake/spectask'
34
+
35
+ Spec::Rake::SpecTask.new(:spec) do |t|
36
+ t.spec_files = Dir.glob('spec/**/*_spec.rb')
37
+ end
38
+
39
+ desc "Run tests"
40
+ task :default => :spec
data/lib/tam/api/sms.rb CHANGED
@@ -22,8 +22,10 @@ module TAM
22
22
  from_user = User.new(access_token, token_secret)
23
23
  to_app = data["to"]
24
24
  body = data["body"]
25
+ transaction_id = data["transaction_id"]
26
+
25
27
  begin
26
- dispatch_to_handler('receive_sms', from_user, to_app, body)
28
+ dispatch_to_handler('receive_sms', from_user, to_app, body, transaction_id)
27
29
  response.status = 200
28
30
  return ''
29
31
  rescue Error => error
@@ -33,9 +35,13 @@ module TAM
33
35
  end
34
36
 
35
37
  # Sends an SMS
36
- def self.send_sms(from_app, to_user, body)
37
- payload = JSON.generate({'body' => body, 'from' => from_app})
38
- dispatch_to_tam(:post, '/api/1/sms/send', to_user, payload)
38
+ def self.send_sms(from_app, to_user, body, transaction_id = nil)
39
+ payload = {'body' => body, 'from' => from_app}
40
+ if transaction_id
41
+ payload["transaction_id"] = transaction_id
42
+ end
43
+ response = dispatch_to_tam(:post, '/api/1/sms/send', to_user, JSON.generate(payload))
44
+ JSON.parse response
39
45
  end
40
46
  end
41
47
  end
data/lib/tam/user.rb CHANGED
@@ -8,5 +8,9 @@ module TAM
8
8
  @access_token = access_token
9
9
  @token_secret = token_secret
10
10
  end
11
+
12
+ def ==(another_user)
13
+ self.access_token == another_user.access_token and self.token_secret == another_user.token_secret
14
+ end
11
15
  end
12
16
  end
data/lib/tam/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module TAM
2
2
  # The version of the gem
3
- VERSION = '1.1.1'.freeze unless defined?(::TAM::VERSION)
3
+ VERSION = '1.2.0'.freeze unless defined?(::TAM::VERSION)
4
4
  end
@@ -0,0 +1,11 @@
1
+ {
2
+ "status": {
3
+ "code":0,
4
+ "message":"Message sent successfully"
5
+ }, "body": {
6
+ "latitude":10.0,
7
+ "longitude":-20.0,
8
+ "accuracy":10,
9
+ "timestamp":1288747339000
10
+ }
11
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "status": {
3
+ "code":0,
4
+ "message":"Message sent successfully"
5
+ }
6
+ }
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'tam/api'
3
+ require 'tam/user'
4
+ require 'rack/test'
5
+ require 'json'
6
+ require 'webmock/rspec'
7
+
8
+ describe TAM::API, "#getcoord" do
9
+ include WebMock::API
10
+ include OAuthSettings
11
+
12
+ it "sends location request to platform" do
13
+ #given
14
+ TAM.expects(:consumer_key).twice().returns(ConsumerKey)
15
+ TAM.expects(:consumer_secret).twice().returns(ConsumerSecret)
16
+ TAM.expects(:site).returns(Site)
17
+ TAM.expects(:request_token_path).returns('/api/1/oauth/request_token')
18
+ TAM.expects(:access_token_path).returns('/api/1/oauth/access_token')
19
+ TAM.expects(:authorize_path).returns('/web/authorize')
20
+ TAM.expects(:oauth_scheme).returns(:query_string)
21
+ TAM.expects(:oauth_http_method).returns(:get)
22
+ http_stub = stub_request(:get, /.*\/api\/1\/location\/getcoord.*/).with { |request| assert_query(request.uri, OAuthParams) }.to_return(:body => load_fixture("tam_getcoord_success.json"))
23
+
24
+ #when
25
+ body = TAM::API.getcoord(TAM::User.new(AccessToken, AccessSecret))
26
+
27
+ #then
28
+ http_stub.should have_been_requested.times(1)
29
+ body["status"]["code"].should == 0
30
+ body["status"]["message"].should == "Message sent successfully"
31
+ end
32
+
33
+ def load_fixture(fixture)
34
+ IO.read("spec/fixtures/#{fixture}")
35
+ end
36
+
37
+ def assert_query(uri, params)
38
+ uri_params = uri.query_values
39
+ params.each do |key, value|
40
+ if not value.match(uri_params[key])
41
+ return false
42
+ end
43
+ end
44
+ return true
45
+ end
46
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ require 'tam/api'
3
+ require 'tam/user'
4
+ require 'rack/test'
5
+ require 'mocha'
6
+ require 'json'
7
+
8
+ describe TAM::API, "when receiving sms" do
9
+ include Rack::Test::Methods
10
+
11
+ def app
12
+ TAM::API
13
+ end
14
+
15
+ before(:all) do
16
+ logger = stub('logger')
17
+ logger.stubs(:error)
18
+ TAM.const_set(:LOGGER, logger)
19
+ end
20
+
21
+ it "should pass request to tam sms handler and render success" do
22
+ #given
23
+ handler = stub('consumer_handler')
24
+ handler.expects(:respond_to?).with('receive_sms').returns(true)
25
+ handler.expects(:send).with('receive_sms', TAM::User.new('token', 'secret'), 'to_app', 'message', nil)
26
+ TAM.expects(:consumer_handler).times(3).returns(handler)
27
+ #when
28
+ post '/tamapi/receive_sms', JSON.generate({ 'body' => 'message', 'to' => 'to_app', 'from' => 'from_user' ,'access_token' => 'token', 'token_secret' => 'secret'})
29
+ #then
30
+ last_response.should be_ok
31
+ end
32
+
33
+ it "shpuld pass request with transaction to tam sms handler and render success" do
34
+ #given
35
+ handler = stub('consumer_handler')
36
+ handler.expects(:respond_to?).with('receive_sms').returns(true)
37
+ handler.expects(:send).with('receive_sms', TAM::User.new('token', 'secret'), 'to_app', 'message', 'tran_id')
38
+ TAM.expects(:consumer_handler).times(3).returns(handler)
39
+ #when
40
+ post '/tamapi/receive_sms', JSON.generate({ 'body' => 'message', 'to' => 'to_app', 'from' => 'from_user','transaction_id' => 'tran_id','access_token' => 'token', 'token_secret' => 'secret'})
41
+ #then
42
+ last_response.should be_ok
43
+ end
44
+
45
+ it "should pass request to tam sms handler and render error on unexpected error" do
46
+ #given
47
+ handler = stub('consumer_handler')
48
+ handler.expects(:respond_to?).with('receive_sms').returns(true)
49
+ handler.stubs(:send).raises(TAM::UnexpectedError.new("Cannot handle"))
50
+ TAM.expects(:consumer_handler).times(3).returns(handler)
51
+ #when
52
+ post '/tamapi/receive_sms', JSON.generate({ 'body' => 'message', 'to' => 'to_app', 'from' => 'from_user','transaction_id' => 'tran_id','access_token' => 'token', 'token_secret' => 'secret'})
53
+ #then
54
+ last_response.should be_server_error
55
+ last_response.body.should == "Cannot handle"
56
+ end
57
+
58
+ it "should pass request to tam sms handler and render error on missing consumer_handler" do
59
+ #given
60
+ handler = stub('consumer_handler')
61
+ TAM.expects(:consumer_handler).times(1).returns(nil)
62
+ #when
63
+ post '/tamapi/receive_sms', JSON.generate({ 'body' => 'message', 'to' => 'to_app', 'from' => 'from_user','transaction_id' => 'tran_id','access_token' => 'token', 'token_secret' => 'secret'})
64
+ #then
65
+ last_response.should be_server_error
66
+ last_response.body.should == "Application has not configured the telco asset marketplace consumer_handler"
67
+ end
68
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'tam/api'
3
+ require 'tam/user'
4
+ require 'rack/test'
5
+ require 'json'
6
+ require 'webmock/rspec'
7
+
8
+ describe TAM::API, "when sending sms" do
9
+ include WebMock::API
10
+ include OAuthSettings
11
+
12
+ it "should succelfully send the request to platform" do
13
+ #given
14
+ TAM.expects(:consumer_key).twice().returns(ConsumerKey)
15
+ TAM.expects(:consumer_secret).twice().returns(ConsumerSecret)
16
+ TAM.expects(:site).returns(Site)
17
+ TAM.expects(:request_token_path).returns('/api/1/oauth/request_token')
18
+ TAM.expects(:access_token_path).returns('/api/1/oauth/access_token')
19
+ TAM.expects(:authorize_path).returns('/web/authorize')
20
+ TAM.expects(:oauth_scheme).returns(:query_string)
21
+ TAM.expects(:oauth_http_method).returns(:get)
22
+ http_stub = stub_request(:post, /.*\/api\/1\/sms\/send.*/).with { |request| assert_request(request, { :query => OAuthParams, :body => "{\"body\":\"test message\",\"from\":\"to_app\"}"})}.to_return(:body => load_fixture("tam_send_sms_success.json"))
23
+
24
+ #when
25
+ body = TAM::API.send_sms("to_app", TAM::User.new(AccessToken, AccessSecret), 'test message')
26
+
27
+ #then
28
+ http_stub.should have_been_requested.times(1)
29
+ body["status"]["code"].should == 0
30
+ body["status"]["message"].should == "Message sent successfully"
31
+ end
32
+
33
+ it "sends sms request with transaction id to platform" do
34
+ #given
35
+ TAM.expects(:consumer_key).twice().returns(ConsumerKey)
36
+ TAM.expects(:consumer_secret).twice().returns(ConsumerSecret)
37
+ TAM.expects(:site).returns(Site)
38
+ TAM.expects(:request_token_path).returns('/api/1/oauth/request_token')
39
+ TAM.expects(:access_token_path).returns('/api/1/oauth/access_token')
40
+ TAM.expects(:authorize_path).returns('/web/authorize')
41
+ TAM.expects(:oauth_scheme).returns(:query_string)
42
+ TAM.expects(:oauth_http_method).returns(:get)
43
+ http_stub = stub_request(:post, /.*\/api\/1\/sms\/send.*/).with { |request| assert_request(request, { :query => OAuthParams, :body => "{\"body\":\"test message\",\"from\":\"to_app\",\"transaction_id\":\"tran_id\"}"})}.to_return(:body => load_fixture("tam_send_sms_success.json"))
44
+
45
+ #when
46
+ body = TAM::API.send_sms("to_app", TAM::User.new(AccessToken, AccessSecret), 'test message', 'tran_id')
47
+
48
+ #then
49
+ http_stub.should have_been_requested.times(1)
50
+ body["status"]["code"].should == 0
51
+ body["status"]["message"].should == "Message sent successfully"
52
+ end
53
+
54
+ def load_fixture(fixture)
55
+ IO.read("spec/fixtures/#{fixture}")
56
+ end
57
+
58
+ def assert_request(req, matchers)
59
+ return (assert_query(req.uri, matchers[:query]) and req.body == matchers[:body])
60
+ end
61
+
62
+ def assert_query(uri, params)
63
+ uri_params = uri.query_values
64
+ params.each do |key, value|
65
+ if not value.match(uri_params[key])
66
+ return false
67
+ end
68
+ end
69
+ return true
70
+ end
71
+ end
@@ -0,0 +1,14 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ module OAuthSettings
7
+ AccessToken = "token"
8
+ AccessSecret = "secret"
9
+ ConsumerKey = "consumer_key"
10
+ ConsumerSecret = "consumer_secret"
11
+ Site = "https://telcoassetmarketplace.com"
12
+
13
+ OAuthParams = {"oauth_consumer_key" => /^#{ConsumerKey}$/, "oauth_signature_method" => /^HMAC-SHA1$/, "oauth_nonce" => /.+/, "oauth_signature" => /.+/, "oauth_timestamp" => /^[0-9]+$/, "oauth_version" => /^1.0$/, "oauth_token" => /^#{AccessToken}$/}
14
+ end
data/tam.gemspec CHANGED
@@ -6,6 +6,13 @@ Gem::Specification.new do |s|
6
6
  s.add_runtime_dependency('json', '~> 1.5')
7
7
  s.add_runtime_dependency('oauth', '~> 0.4.3')
8
8
 
9
+ s.add_development_dependency('rake', '= 0.9.2')
10
+ s.add_development_dependency('rspec', '= 1.3.2')
11
+ s.add_development_dependency('rack-test', "~> 0.6.1")
12
+ s.add_development_dependency('mocha', '~> 0.9.0')
13
+ s.add_development_dependency('webmock', '~> 1.7.0')
14
+ s.add_development_dependency('simplecov', '~> 0.5.0')
15
+
9
16
  s.name = "tam"
10
17
  s.version = TAM::VERSION.dup
11
18
  s.platform = Gem::Platform::RUBY
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tam
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 1
9
- - 1
10
- version: 1.1.1
8
+ - 2
9
+ - 0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Carlos Manzanares
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-08 00:00:00 +03:00
18
+ date: 2011-10-19 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -65,6 +65,102 @@ dependencies:
65
65
  - 3
66
66
  version: 0.4.3
67
67
  requirement: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: rake
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - "="
76
+ - !ruby/object:Gem::Version
77
+ hash: 63
78
+ segments:
79
+ - 0
80
+ - 9
81
+ - 2
82
+ version: 0.9.2
83
+ requirement: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - "="
92
+ - !ruby/object:Gem::Version
93
+ hash: 31
94
+ segments:
95
+ - 1
96
+ - 3
97
+ - 2
98
+ version: 1.3.2
99
+ requirement: *id005
100
+ - !ruby/object:Gem::Dependency
101
+ name: rack-test
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: &id006 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ hash: 5
110
+ segments:
111
+ - 0
112
+ - 6
113
+ - 1
114
+ version: 0.6.1
115
+ requirement: *id006
116
+ - !ruby/object:Gem::Dependency
117
+ name: mocha
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: &id007 !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ hash: 59
126
+ segments:
127
+ - 0
128
+ - 9
129
+ - 0
130
+ version: 0.9.0
131
+ requirement: *id007
132
+ - !ruby/object:Gem::Dependency
133
+ name: webmock
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: &id008 !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ hash: 11
142
+ segments:
143
+ - 1
144
+ - 7
145
+ - 0
146
+ version: 1.7.0
147
+ requirement: *id008
148
+ - !ruby/object:Gem::Dependency
149
+ name: simplecov
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: &id009 !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ hash: 11
158
+ segments:
159
+ - 0
160
+ - 5
161
+ - 0
162
+ version: 0.5.0
163
+ requirement: *id009
68
164
  description: The Ruby gem for the telco asset marketplace REST APIs
69
165
  email:
70
166
  - developers@telcoassetmarketplace.com
@@ -91,6 +187,12 @@ files:
91
187
  - lib/tam/railtie.rb
92
188
  - lib/tam/user.rb
93
189
  - lib/tam/version.rb
190
+ - spec/fixtures/tam_getcoord_success.json
191
+ - spec/fixtures/tam_send_sms_success.json
192
+ - spec/location_get_spec.rb
193
+ - spec/sms_receive_spec.rb
194
+ - spec/sms_send_spec.rb
195
+ - spec/spec_helper.rb
94
196
  - tam.gemspec
95
197
  has_rdoc: true
96
198
  homepage: https://github.com/tamdeveloper/telco-asset-marketplace-gem
@@ -139,5 +241,10 @@ rubygems_version: 1.3.7
139
241
  signing_key:
140
242
  specification_version: 3
141
243
  summary: telco asset marketplace Ruby gem
142
- test_files: []
143
-
244
+ test_files:
245
+ - spec/fixtures/tam_getcoord_success.json
246
+ - spec/fixtures/tam_send_sms_success.json
247
+ - spec/location_get_spec.rb
248
+ - spec/sms_receive_spec.rb
249
+ - spec/sms_send_spec.rb
250
+ - spec/spec_helper.rb