tropo_rest 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +88 -0
- data/LICENSE +19 -0
- data/README.md +104 -0
- data/Rakefile +35 -0
- data/autotest/discover.rb +1 -0
- data/lib/faraday/request/serialize_json.rb +17 -0
- data/lib/faraday/response/raise_http_errors.rb +52 -0
- data/lib/faraday/response/resource.rb +26 -0
- data/lib/hashie/twash.rb +61 -0
- data/lib/tropo_rest/client/address.rb +76 -0
- data/lib/tropo_rest/client/application.rb +72 -0
- data/lib/tropo_rest/client/exchange.rb +18 -0
- data/lib/tropo_rest/client/session.rb +20 -0
- data/lib/tropo_rest/client/signal.rb +26 -0
- data/lib/tropo_rest/client.rb +27 -0
- data/lib/tropo_rest/configuration.rb +61 -0
- data/lib/tropo_rest/connection.rb +28 -0
- data/lib/tropo_rest/error.rb +33 -0
- data/lib/tropo_rest/request.rb +66 -0
- data/lib/tropo_rest/resource/address.rb +20 -0
- data/lib/tropo_rest/resource/application.rb +15 -0
- data/lib/tropo_rest/resource/exchange.rb +13 -0
- data/lib/tropo_rest/utils.rb +33 -0
- data/lib/tropo_rest/version.rb +3 -0
- data/lib/tropo_rest.rb +31 -0
- data/script/console +8 -0
- data/spec/spec_helper.rb +64 -0
- data/spec/tropo_rest/client/address_spec.rb +172 -0
- data/spec/tropo_rest/client/application_spec.rb +210 -0
- data/spec/tropo_rest/client/exchange_spec.rb +71 -0
- data/spec/tropo_rest/client/session_spec.rb +47 -0
- data/spec/tropo_rest/client/signal_spec.rb +34 -0
- data/spec/tropo_rest/client_spec.rb +246 -0
- data/spec/tropo_rest/resource/address_spec.rb +12 -0
- data/spec/tropo_rest/resource/application_spec.rb +12 -0
- data/spec/tropo_rest/resource/exchange_spec.rb +12 -0
- data/spec/tropo_rest_spec.rb +22 -0
- data/tropo_rest.gemspec +50 -0
- metadata +343 -0
@@ -0,0 +1,210 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe TropoRest::Client do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@client = TropoRest::Client.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#applications" do
|
10
|
+
|
11
|
+
before do
|
12
|
+
stub_get("applications").to_return(:body => <<-JSON
|
13
|
+
[
|
14
|
+
{
|
15
|
+
"id": "1",
|
16
|
+
"href": "https://api.tropo.com/v1/applications/1",
|
17
|
+
"name": "app1",
|
18
|
+
"platform": "scripting",
|
19
|
+
"voiceUrl": "http://example1.com/voice.rb",
|
20
|
+
"messagingUrl": "http://example1.com/messaging.rb",
|
21
|
+
"partition": "staging"
|
22
|
+
},
|
23
|
+
{
|
24
|
+
"id": "2",
|
25
|
+
"href": "https://api.tropo.com/v1/applications/2",
|
26
|
+
"name": "app2",
|
27
|
+
"platform": "webapi",
|
28
|
+
"voiceUrl": "http://example2.com/voice",
|
29
|
+
"messagingUrl": "http://example2.com/messaging",
|
30
|
+
"partition": "production"
|
31
|
+
}
|
32
|
+
]
|
33
|
+
JSON
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should make the request" do
|
38
|
+
@client.applications
|
39
|
+
a_get("applications").should have_been_made
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return a collection of applications" do
|
43
|
+
@client.applications.should have(2).items
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return the correct objects" do
|
47
|
+
apps = @client.applications
|
48
|
+
first = apps.first
|
49
|
+
last = apps.last
|
50
|
+
|
51
|
+
first.should be_instance_of(TropoRest::Resource::Application)
|
52
|
+
first.id.should == "1"
|
53
|
+
first.href.should == "https://api.tropo.com/v1/applications/1"
|
54
|
+
first.name.should == "app1"
|
55
|
+
first.platform.should == "scripting"
|
56
|
+
first.voice_url.should == "http://example1.com/voice.rb"
|
57
|
+
first.messaging_url.should == "http://example1.com/messaging.rb"
|
58
|
+
first.partition.should == "staging"
|
59
|
+
|
60
|
+
last.should be_instance_of(TropoRest::Resource::Application)
|
61
|
+
last.id.should == "2"
|
62
|
+
last.href.should == "https://api.tropo.com/v1/applications/2"
|
63
|
+
last.name.should == "app2"
|
64
|
+
last.platform.should == "webapi"
|
65
|
+
last.voice_url.should == "http://example2.com/voice"
|
66
|
+
last.messaging_url.should == "http://example2.com/messaging"
|
67
|
+
last.partition.should == "production"
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "#application" do
|
73
|
+
|
74
|
+
before do
|
75
|
+
stub_get("applications/1").to_return(:body => <<-JSON
|
76
|
+
{
|
77
|
+
"id": "1",
|
78
|
+
"href": "https://api.tropo.com/v1/applications/1",
|
79
|
+
"name": "app1",
|
80
|
+
"platform": "scripting",
|
81
|
+
"voiceUrl": "http://example1.com/voice.rb",
|
82
|
+
"messagingUrl": "http://example1.com/messaging.rb",
|
83
|
+
"partition": "staging"
|
84
|
+
}
|
85
|
+
JSON
|
86
|
+
)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should make the request with an application ID" do
|
90
|
+
@client.application(1)
|
91
|
+
a_get("applications/1").should have_been_made
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should make the request with an application HREF" do
|
95
|
+
@client.application("https://api.tropo.com/v1/applications/1")
|
96
|
+
a_get("applications/1").should have_been_made
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should return an application object" do
|
100
|
+
app = @client.application(1)
|
101
|
+
app.should be_instance_of(TropoRest::Resource::Application)
|
102
|
+
app.id.should == "1"
|
103
|
+
app.href.should == "https://api.tropo.com/v1/applications/1"
|
104
|
+
app.name.should == "app1"
|
105
|
+
app.platform.should == "scripting"
|
106
|
+
app.voice_url.should == "http://example1.com/voice.rb"
|
107
|
+
app.messaging_url.should == "http://example1.com/messaging.rb"
|
108
|
+
app.partition.should == "staging"
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "#create_application" do
|
114
|
+
|
115
|
+
before do
|
116
|
+
@underscore = {
|
117
|
+
:name => "new app",
|
118
|
+
:voice_url => "http://website.com",
|
119
|
+
:messaging_url => "http://website2.com",
|
120
|
+
:platform => "scripting",
|
121
|
+
:partition => "staging"
|
122
|
+
}
|
123
|
+
@camel_case = {
|
124
|
+
'name' => "new app",
|
125
|
+
'voiceUrl' => "http://website.com",
|
126
|
+
'messagingUrl' => "http://website2.com",
|
127
|
+
'platform' => "scripting",
|
128
|
+
'partition' => "staging"
|
129
|
+
}
|
130
|
+
stub_post("applications").
|
131
|
+
with(:body => @camel_case).
|
132
|
+
to_return(:body => %({"href":"https://api.tropo.com/v1/applications/123456"}))
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should make the request" do
|
136
|
+
@client.create_application(@underscore)
|
137
|
+
a_post("applications").with(:body => @camel_case).should have_been_made
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should return the href of the application" do
|
141
|
+
res = @client.create_application(@underscore)
|
142
|
+
res.href.should == "https://api.tropo.com/v1/applications/123456"
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "#delete_application" do
|
148
|
+
|
149
|
+
before do
|
150
|
+
stub_delete("applications/123456").
|
151
|
+
to_return(:body => %({"message": "delete successful"}))
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should make the request with an application ID" do
|
155
|
+
@client.delete_application(123456)
|
156
|
+
a_delete("applications/123456").should have_been_made
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should make the request with an application HREF" do
|
160
|
+
@client.delete_application("https://api.tropo.com/v1/applications/123456")
|
161
|
+
a_delete("applications/123456").should have_been_made
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should return a successful message" do
|
165
|
+
res = @client.delete_application(123456)
|
166
|
+
res.message.should == "delete successful"
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "#update_application" do
|
172
|
+
|
173
|
+
before do
|
174
|
+
@underscore = {
|
175
|
+
:name => "newer app",
|
176
|
+
:voice_url => "http://voice.com",
|
177
|
+
:messaging_url => "http://messaging.com",
|
178
|
+
:platform => "webapi",
|
179
|
+
:partition => "production"
|
180
|
+
}
|
181
|
+
@camel_case = {
|
182
|
+
'name' => "newer app",
|
183
|
+
'voiceUrl' => "http://voice.com",
|
184
|
+
'messagingUrl' => "http://messaging.com",
|
185
|
+
'platform' => "webapi",
|
186
|
+
'partition' => "production"
|
187
|
+
}
|
188
|
+
stub_put("applications/123456").
|
189
|
+
with(:body => @camel_case).
|
190
|
+
to_return(:body => %({"href":"https://api.tropo.com/v1/applications/123456"}))
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should make the request with an application ID" do
|
194
|
+
@client.update_application(123456, @underscore)
|
195
|
+
a_put("applications/123456").with(:body => @camel_case).should have_been_made
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should make the request with an application HREF" do
|
199
|
+
@client.update_application("https://api.tropo.com/v1/applications/123456", @underscore)
|
200
|
+
a_put("applications/123456").with(:body => @camel_case).should have_been_made
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should return the href of the application" do
|
204
|
+
res = @client.update_application(123456, @underscore)
|
205
|
+
res.href.should == "https://api.tropo.com/v1/applications/123456"
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe TropoRest::Client do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@client = TropoRest::Client.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#exchanges" do
|
10
|
+
|
11
|
+
before do
|
12
|
+
stub_get("exchanges").to_return(:body => <<-JSON
|
13
|
+
[
|
14
|
+
{
|
15
|
+
"prefix": "1321",
|
16
|
+
"city": "Orlando",
|
17
|
+
"state": "FL",
|
18
|
+
"country": "United States",
|
19
|
+
"description": "Phone Number w/ SMS"
|
20
|
+
},
|
21
|
+
{
|
22
|
+
"prefix": "1888",
|
23
|
+
"country": "United States",
|
24
|
+
"description": "Toll Free Phone Number"
|
25
|
+
},
|
26
|
+
{
|
27
|
+
"prefix": "31",
|
28
|
+
"country": "Netherlands",
|
29
|
+
"description": "International Phone Number"
|
30
|
+
}
|
31
|
+
]
|
32
|
+
JSON
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should make the request" do
|
37
|
+
@client.exchanges
|
38
|
+
a_get("exchanges").should have_been_made
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return a collection of exchanges" do
|
42
|
+
@client.exchanges.should have(3).items
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should return the correct objects" do
|
46
|
+
apps = @client.exchanges
|
47
|
+
first = apps[0]
|
48
|
+
second = apps[1]
|
49
|
+
third = apps[2]
|
50
|
+
|
51
|
+
first.should be_instance_of(TropoRest::Resource::Exchange)
|
52
|
+
first.prefix.should == "1321"
|
53
|
+
first.city.should == "Orlando"
|
54
|
+
first.state.should == "FL"
|
55
|
+
first.country.should == "United States"
|
56
|
+
first.description.should == "Phone Number w/ SMS"
|
57
|
+
|
58
|
+
second.should be_instance_of(TropoRest::Resource::Exchange)
|
59
|
+
second.prefix.should == "1888"
|
60
|
+
second.country.should == "United States"
|
61
|
+
second.description.should == "Toll Free Phone Number"
|
62
|
+
|
63
|
+
third.should be_instance_of(TropoRest::Resource::Exchange)
|
64
|
+
third.prefix.should == "31"
|
65
|
+
third.country.should == "Netherlands"
|
66
|
+
third.description.should == "International Phone Number"
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe TropoRest::Client do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@client = TropoRest::Client.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#create_session" do
|
10
|
+
|
11
|
+
before do
|
12
|
+
@params = {'token' => 'TOKEN', 'action' => 'create'}
|
13
|
+
stub_session_get("sessions").
|
14
|
+
with(:query => @params).
|
15
|
+
to_return(:body => <<-XML
|
16
|
+
<session>
|
17
|
+
<success>true</success>
|
18
|
+
<token>TOKEN</token>
|
19
|
+
</session>
|
20
|
+
XML
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should make the request" do
|
25
|
+
@client.create_session("TOKEN")
|
26
|
+
a_session_get("sessions").
|
27
|
+
with(:query => @params).should have_been_made
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should make the request with parameters" do
|
31
|
+
params = {'phone_number' => '+19995551234', 'name' => 'Billy Gnosis'}
|
32
|
+
query = params.merge('action' => 'create', 'token' => 'TOKEN')
|
33
|
+
stub_session_get("sessions").
|
34
|
+
with(:query => query)
|
35
|
+
@client.create_session("TOKEN", params)
|
36
|
+
a_session_get("sessions").
|
37
|
+
with(:query => query).should have_been_made
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should return the session object" do
|
41
|
+
res = @client.create_session("TOKEN")
|
42
|
+
res.session.should == {'success' => 'true', 'token' => 'TOKEN'}
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe TropoRest::Client do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@client = TropoRest::Client.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#create_signal" do
|
10
|
+
|
11
|
+
before do
|
12
|
+
@params = {'signal' => 'exit'}
|
13
|
+
stub_session_post("sessions/644a27c6da7e505ab432325056671535/signals").
|
14
|
+
with(:body => @params).
|
15
|
+
to_return(:body => <<-JSON
|
16
|
+
{"status": "QUEUED"}
|
17
|
+
JSON
|
18
|
+
)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should make the request" do
|
22
|
+
@client.create_signal("644a27c6da7e505ab432325056671535", "exit")
|
23
|
+
a_session_post("sessions/644a27c6da7e505ab432325056671535/signals").
|
24
|
+
with(:body => @params).should have_been_made
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return the signal object" do
|
28
|
+
res = @client.create_signal("644a27c6da7e505ab432325056671535", "exit")
|
29
|
+
res.status.should == "QUEUED"
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,246 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe TropoRest::Client do
|
4
|
+
|
5
|
+
describe "configuration" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
TropoRest.reset
|
9
|
+
end
|
10
|
+
|
11
|
+
it "uses defaults set on TropoRest" do
|
12
|
+
TropoRest.configure do |config|
|
13
|
+
config.username = "username"
|
14
|
+
config.password = "password"
|
15
|
+
config.adapter = :em_synchrony
|
16
|
+
config.endpoint = "http://api.tropo.com/v1/"
|
17
|
+
config.session_endpoint = "http://api.tropo.com/1.0/"
|
18
|
+
config.user_agent = "RSpec"
|
19
|
+
end
|
20
|
+
client = TropoRest::Client.new
|
21
|
+
client.username.should == "username"
|
22
|
+
client.password.should == "password"
|
23
|
+
client.adapter.should == :em_synchrony
|
24
|
+
client.endpoint.should == "http://api.tropo.com/v1/"
|
25
|
+
client.session_endpoint.should == "http://api.tropo.com/1.0/"
|
26
|
+
client.user_agent.should == "RSpec"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can be initialized with options hash" do
|
30
|
+
client = TropoRest::Client.new \
|
31
|
+
:username => "username",
|
32
|
+
:password => "password",
|
33
|
+
:adapter => :em_synchrony,
|
34
|
+
:endpoint => "http://api.tropo.com/v1/",
|
35
|
+
:session_endpoint => "http://api.tropo.com/1.0/",
|
36
|
+
:user_agent => "RSpec"
|
37
|
+
client.username.should == "username"
|
38
|
+
client.password.should == "password"
|
39
|
+
client.adapter.should == :em_synchrony
|
40
|
+
client.endpoint.should == "http://api.tropo.com/v1/"
|
41
|
+
client.session_endpoint.should == "http://api.tropo.com/1.0/"
|
42
|
+
client.user_agent.should == "RSpec"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should have sensible defaults" do
|
46
|
+
client = TropoRest::Client.new
|
47
|
+
client.username.should be_nil
|
48
|
+
client.password.should be_nil
|
49
|
+
client.adapter.should == Faraday.default_adapter
|
50
|
+
client.endpoint.should == "https://api.tropo.com/v1/"
|
51
|
+
client.session_endpoint.should == "https://api.tropo.com/1.0/"
|
52
|
+
client.user_agent.should == "TropoRest Ruby Gem #{TropoRest::VERSION}"
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "request" do
|
58
|
+
|
59
|
+
before do
|
60
|
+
@client = TropoRest::Client.new
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should respond to GET" do
|
64
|
+
@client.should respond_to(:get)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should perform GET" do
|
68
|
+
stub_get("somewhere")
|
69
|
+
@client.get("somewhere")
|
70
|
+
a_get("somewhere").should have_been_made
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should respond to POST" do
|
74
|
+
@client.should respond_to(:post)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should perform POST" do
|
78
|
+
stub_post("somewhere")
|
79
|
+
@client.post("somewhere", {:foo => "bar"})
|
80
|
+
a_post("somewhere").with(:body => {:foo => "bar"}).should have_been_made
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should respond to PUT" do
|
84
|
+
@client.should respond_to(:put)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should perform PUT" do
|
88
|
+
stub_put("somewhere")
|
89
|
+
@client.put("somewhere", {:foo => "bar"})
|
90
|
+
a_put("somewhere").with(:body => {:foo => "bar"}).should have_been_made
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should respond to DELETE" do
|
94
|
+
@client.should respond_to(:delete)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should perform DELETE" do
|
98
|
+
stub_delete("somewhere")
|
99
|
+
@client.delete("somewhere")
|
100
|
+
a_delete("somewhere").should have_been_made
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should use authenticated URL" do
|
104
|
+
stub_request(:get, "https://username:password@api.tropo.com/v1/somewhere")
|
105
|
+
client = TropoRest::Client.new :username => "username", :password => "password"
|
106
|
+
client.get("somewhere")
|
107
|
+
a_request(:get, "https://username:password@api.tropo.com/v1/somewhere").should have_been_made
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should send the correct User-Agent" do
|
111
|
+
stub_get("somewhere")
|
112
|
+
@client.get("somewhere")
|
113
|
+
a_get("somewhere").with(:headers => {"User-Agent" => @client.user_agent}).should have_been_made
|
114
|
+
end
|
115
|
+
|
116
|
+
context "sessions" do
|
117
|
+
|
118
|
+
it "should use the session URL if the path passed in starts with 'sessions'" do
|
119
|
+
stub_request(:get, "https://api.tropo.com/1.0/sessions")
|
120
|
+
@client.get("sessions")
|
121
|
+
a_request(:get, "https://api.tropo.com/1.0/sessions").should have_been_made
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should send XML Accept header for session requests" do
|
125
|
+
params = {'action' => 'create', 'token' => 'TOKEN'}
|
126
|
+
stub_session_get("sessions").with(:query => params)
|
127
|
+
@client.get("sessions", params)
|
128
|
+
a_session_get("sessions").with(:query => params, :headers => {"Accept" => "application/xml"}).should have_been_made
|
129
|
+
end
|
130
|
+
|
131
|
+
context "signals" do
|
132
|
+
|
133
|
+
it "should send JSON Accept header" do
|
134
|
+
params = {"signal" => "exit"}
|
135
|
+
stub_session_post("sessions/abcd/signals").with(:body => params)
|
136
|
+
@client.post("sessions/abcd/signals", params)
|
137
|
+
a_session_post("sessions/abcd/signals").with(:body => params, :headers => {"Accept" => "application/json"}).should have_been_made
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should send JSON Accept header" do
|
145
|
+
stub_get("somewhere")
|
146
|
+
@client.get("somewhere")
|
147
|
+
a_get("somewhere").with(:headers => {"Accept" => "application/json"}).should have_been_made
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should send JSON Content-Type header" do
|
151
|
+
stub_get("somewhere")
|
152
|
+
@client.get("somewhere")
|
153
|
+
a_get("somewhere").with(:headers => {"Content-Type" => "application/json"}).should have_been_made
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should use JSON encoded requests" do
|
157
|
+
params = {'hello' => 'world', 'array' => [1, 2, 3]}
|
158
|
+
stub_post("somewhere")
|
159
|
+
@client.post("somewhere", params)
|
160
|
+
a_post("somewhere").with(:body => params).should have_been_made
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should decode JSON responses" do
|
164
|
+
params = {'hello' => 'world', 'array' => [1, 2, 3]}
|
165
|
+
body = MultiJson.encode(params)
|
166
|
+
stub_get("somewhere").to_return(:body => body)
|
167
|
+
res = @client.get("somewhere")
|
168
|
+
res.should == params
|
169
|
+
end
|
170
|
+
|
171
|
+
context "underscore and camel case" do
|
172
|
+
|
173
|
+
before do
|
174
|
+
class TestResource < Hashie::Twash
|
175
|
+
property :voice_url, :from => :voiceUrl
|
176
|
+
property :messaging_url, :from => :messagingUrl
|
177
|
+
end
|
178
|
+
@underscore = {'voice_url' => 'http://example.com', 'messaging_url' => 'http://example2.com'}
|
179
|
+
@camel_case = {'voiceUrl' => 'http://example.com', 'messagingUrl' => 'http://example2.com'}
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should convert request params to camel case" do
|
183
|
+
stub_post("somewhere")
|
184
|
+
@client.post("somewhere", TestResource.new(@underscore))
|
185
|
+
a_post("somewhere").with(:body => @camel_case).should have_been_made
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should convert response params to underscore" do
|
189
|
+
stub_get("somewhere").to_return(:body => MultiJson.encode(@camel_case))
|
190
|
+
res = @client.get("somewhere", TestResource)
|
191
|
+
res.should == @underscore
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
|
198
|
+
describe "errors" do
|
199
|
+
|
200
|
+
before do
|
201
|
+
@client = TropoRest::Client.new
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should raise Bad Request on 400 status code" do
|
205
|
+
stub_get("error").to_return(:status => 400)
|
206
|
+
lambda { @client.get("error") }.should raise_error(TropoRest::BadRequest)
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should raise Not Authorized on 401 status code" do
|
210
|
+
stub_get("error").to_return(:status => 401)
|
211
|
+
lambda { @client.get("error") }.should raise_error(TropoRest::NotAuthorized)
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should raise Access Denied on 403 status code" do
|
215
|
+
stub_get("error").to_return(:status => 403)
|
216
|
+
lambda { @client.get("error") }.should raise_error(TropoRest::AccessDenied)
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should raise Not Found on 404 status code" do
|
220
|
+
stub_get("error").to_return(:status => 404)
|
221
|
+
lambda { @client.get("error") }.should raise_error(TropoRest::NotFound)
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should raise Method Not Allowed on 405 status code" do
|
225
|
+
stub_get("error").to_return(:status => 405)
|
226
|
+
lambda { @client.get("error") }.should raise_error(TropoRest::MethodNotAllowed)
|
227
|
+
end
|
228
|
+
|
229
|
+
it "should raise Unsupported Media Type on 415 status code" do
|
230
|
+
stub_get("error").to_return(:status => 415)
|
231
|
+
lambda { @client.get("error") }.should raise_error(TropoRest::UnsupportedMediaType)
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should raise Internal Server Error on 500 status code" do
|
235
|
+
stub_get("error").to_return(:status => 500)
|
236
|
+
lambda { @client.get("error") }.should raise_error(TropoRest::InternalServerError)
|
237
|
+
end
|
238
|
+
|
239
|
+
it "should raise Service Unavailable on 503 status code" do
|
240
|
+
stub_get("error").to_return(:status => 503)
|
241
|
+
lambda { @client.get("error") }.should raise_error(TropoRest::ServiceUnavailable)
|
242
|
+
end
|
243
|
+
|
244
|
+
end
|
245
|
+
|
246
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe TropoRest::Resource::Address do
|
4
|
+
|
5
|
+
it { should be_kind_of(Hashie::Twash) }
|
6
|
+
|
7
|
+
%w[href type prefix number address city state channel username password token application_id].each do |param|
|
8
|
+
it { should respond_to(param) }
|
9
|
+
it { should respond_to("#{param}=") }
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe TropoRest::Resource::Application do
|
4
|
+
|
5
|
+
it { should be_kind_of(Hashie::Twash) }
|
6
|
+
|
7
|
+
%w[id href name voice_url messaging_url platform partition].each do |param|
|
8
|
+
it { should respond_to(param) }
|
9
|
+
it { should respond_to("#{param}=") }
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe TropoRest::Resource::Exchange do
|
4
|
+
|
5
|
+
it { should be_kind_of(Hashie::Twash) }
|
6
|
+
|
7
|
+
%w[prefix city state country description].each do |param|
|
8
|
+
it { should respond_to(param) }
|
9
|
+
it { should respond_to("#{param}=") }
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe TropoRest do
|
4
|
+
|
5
|
+
describe ".client" do
|
6
|
+
|
7
|
+
it "should be a TropoRest::Client" do
|
8
|
+
TropoRest.client.should be_kind_of(TropoRest::Client)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should delegate methods to the client" do
|
14
|
+
client = double("TropoRest::Client")
|
15
|
+
client.stub(:client_method)
|
16
|
+
TropoRest.stub(:client).and_return(client)
|
17
|
+
|
18
|
+
client.should_receive(:client_method)
|
19
|
+
TropoRest.client_method
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|