hashblue-api 0.0.8
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/README +21 -0
- data/Rakefile +98 -0
- data/hashblue-api.gemspec +58 -0
- data/lib/dependency/mime_type.rb +231 -0
- data/lib/dependency/mime_types.rb +23 -0
- data/lib/hashblue/api.rb +89 -0
- data/lib/hashblue/api/contact.rb +26 -0
- data/lib/hashblue/api/error.rb +39 -0
- data/lib/hashblue/api/message.rb +37 -0
- data/lib/hashblue/api/model.rb +55 -0
- data/lib/hashblue/api/request.rb +36 -0
- data/lib/hashblue/api/subscriber.rb +65 -0
- data/lib/hashblue/api/test_helper.rb +87 -0
- data/test/helpers/test_helper_test.rb +115 -0
- data/test/test_helper.rb +32 -0
- data/test/unit/api_test.rb +17 -0
- data/test/unit/contact_test.rb +27 -0
- data/test/unit/message_api_test.rb +135 -0
- data/test/unit/message_test.rb +42 -0
- data/test/unit/model_test.rb +27 -0
- data/test/unit/subscriber_test.rb +42 -0
- metadata +110 -0
data/test/test_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# This isn't great
|
2
|
+
|
3
|
+
begin
|
4
|
+
# Require the preresolved locked set of gems.
|
5
|
+
require File.expand_path('../../../.bundle/environment', __FILE__)
|
6
|
+
rescue LoadError
|
7
|
+
# Fallback on doing the resolve at runtime.
|
8
|
+
require "rubygems"
|
9
|
+
require "bundler"
|
10
|
+
Bundler.setup
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'test/unit'
|
14
|
+
require 'mocha'
|
15
|
+
require 'shoulda'
|
16
|
+
require 'webmock/test_unit'
|
17
|
+
|
18
|
+
# This dependency needs to be removed
|
19
|
+
|
20
|
+
require 'hashblue/api'
|
21
|
+
|
22
|
+
Hashblue::API.base_uri "http://www.example.com"
|
23
|
+
Hashblue::API.timeout 1
|
24
|
+
|
25
|
+
class Test::Unit::TestCase
|
26
|
+
def dont_respond_to_http_requests_for_10_seconds
|
27
|
+
stub_request(:any, Regexp.new(Regexp.escape("http://www.example.com/"))).to_return(lambda {|request|
|
28
|
+
sleep(10)
|
29
|
+
result = {:headers => {}, :body => {}, :status => 200 }
|
30
|
+
})
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class Hashblue::APITest < Test::Unit::TestCase
|
4
|
+
include WebMock
|
5
|
+
|
6
|
+
context "Hashblue::API" do
|
7
|
+
context "a request that times out" do
|
8
|
+
setup do
|
9
|
+
dont_respond_to_http_requests_for_10_seconds
|
10
|
+
end
|
11
|
+
|
12
|
+
should "raise a TimeoutError" do
|
13
|
+
assert_raises(Hashblue::API::NotRespondingError) { Hashblue::API.get '/any/path' }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class Hashblue::API::ContactTest < Test::Unit::TestCase
|
4
|
+
context "Hashblue::API::Contact" do
|
5
|
+
should "return Contact for model name" do
|
6
|
+
assert_equal 'Contact', Hashblue::API::Contact.model_name
|
7
|
+
end
|
8
|
+
|
9
|
+
should "return contact for model_name.singular" do
|
10
|
+
assert_equal 'contact', Hashblue::API::Contact.model_name.singular
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "A contact initialized with an subscriber_id and an id" do
|
15
|
+
setup do
|
16
|
+
@subscriber_id = 'ffddee554433'
|
17
|
+
@id = 'abcdef123456'
|
18
|
+
@contact = Hashblue::API::Contact.new(:id => @id, :subscriber_id => @subscriber_id)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "return messages found at /subscribers/:subscriber_id/contacts/:id/messages.json as messages" do
|
22
|
+
Hashblue::API.expects(:get).with("/subscribers/#{@subscriber_id}/contacts/#{@id}/messages.json", anything).returns(@json = :messages_json)
|
23
|
+
Hashblue::API::Message.expects(:from_json).with(@json).returns(:messages)
|
24
|
+
assert_equal :messages, @contact.messages
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class Hashblue::API::MessageAPITest < Test::Unit::TestCase
|
4
|
+
# include WebMock
|
5
|
+
#
|
6
|
+
# context "When loading a messages for an MSISDN" do
|
7
|
+
# context "and the API responds with a message" do
|
8
|
+
# setup do
|
9
|
+
# stub_request(:get, Hashblue::API.url_for("/subscribers/44123456789/messages.json")).to_return(
|
10
|
+
# :body => %{[{"sender":"44123456789","content":"Hello","recipient":"44987654321","timestamp":"2010/02/15 17:26:21 +0000"}]},
|
11
|
+
# :status => 200,
|
12
|
+
# :headers => {'Content-Type' => Mime::JSON.to_s}
|
13
|
+
# )
|
14
|
+
# @messages = Hashblue::API::Message.messages_for("44123456789")
|
15
|
+
# @message = @messages.first
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# should "return an instantiated Hashblue::API::Message" do
|
19
|
+
# assert_equal 1, @messages.length
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# should "parse the message content" do
|
23
|
+
# assert_equal "Hello", @message.content
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# should "parse the message sender MSISDN" do
|
27
|
+
# assert_equal "44123456789", @message.sender_msisdn
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# should "parse the message recipient MSISDN" do
|
31
|
+
# assert_equal "44987654321", @message.recipient_msisdn
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# should "parse the timestamp" do
|
35
|
+
# assert_equal Time.parse("2010/02/15 17:26:21 +0000"), @message.timestamp
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# context "and the API responds with several messages" do
|
40
|
+
# setup do
|
41
|
+
# stub_request(:get, Hashblue::API.url_for("/subscribers/44123456789/messages.json")).to_return(
|
42
|
+
# :body => %{[{"sender":"44123456789","content":"Hello","recipient":"44987654321","timestamp":"2010/02/15 17:26:21 +0000"},{"sender":"44234567890","content":"Goodbye","recipient":"44987654321","timestamp":"2010/02/15 17:36:41 +0000"}]},
|
43
|
+
# :status => 200,
|
44
|
+
# :headers => {'Content-Type' => Mime::JSON.to_s}
|
45
|
+
# )
|
46
|
+
# @messages = Hashblue::API::Message.messages_for("44123456789")
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# should "return each message" do
|
50
|
+
# assert_equal 2, @messages.length
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# should "parse all content" do
|
54
|
+
# assert_equal %w(Hello Goodbye), @messages.map(&:content)
|
55
|
+
# end
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# context "and the API has no messages for that MSISDN" do
|
59
|
+
# setup do
|
60
|
+
# stub_request(:get, Hashblue::API.url_for("/subscribers/44123456789/messages.json")).to_return(
|
61
|
+
# :body => %{[]},
|
62
|
+
# :status => 200,
|
63
|
+
# :headers => {'Content-Type' => Mime::JSON.to_s}
|
64
|
+
# )
|
65
|
+
# @messages = Hashblue::API::Message.messages_for("44123456789")
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# should "return an empty array" do
|
69
|
+
# assert @messages.empty?
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# context "and the API responds with something that isn't JSON" do
|
74
|
+
# setup do
|
75
|
+
# stub_request(:get, Hashblue::API.url_for("/subscribers/44123456789/messages.json")).to_return(
|
76
|
+
# :body => %{<html><body><h1>Oh hello, this is HTML, yes.</body></html>},
|
77
|
+
# :status => 200,
|
78
|
+
# :headers => {'Content-Type' => Mime::HTML.to_s}
|
79
|
+
# )
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# should "raise an exception" do
|
83
|
+
# assert_raises(Hashblue::API::BadResponseError) do
|
84
|
+
# Hashblue::API::Message.messages_for("44123456789")
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# context "and the API responds with a 500 status code" do
|
90
|
+
# setup do
|
91
|
+
# stub_request(:get, Hashblue::API.url_for("/subscribers/44123456789/messages.json")).to_return(
|
92
|
+
# :body => %{[]},
|
93
|
+
# :status => 500,
|
94
|
+
# :headers => {'Content-Type' => Mime::JSON.to_s}
|
95
|
+
# )
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# should "raise an exception" do
|
99
|
+
# assert_raises(Hashblue::API::BadResponseError) do
|
100
|
+
# Hashblue::API::Message.messages_for("44123456789")
|
101
|
+
# end
|
102
|
+
# end
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# context "and the API fails to respond within 2 seconds" do
|
106
|
+
# setup do
|
107
|
+
# dont_respond_to_http_requests_for_10_seconds
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
# should "raise a NotRespondingError" do
|
111
|
+
# assert_raises(Hashblue::API::NotRespondingError) do
|
112
|
+
# Hashblue::API::Message.messages_for("44123456789")
|
113
|
+
# end
|
114
|
+
# end
|
115
|
+
# end
|
116
|
+
# end
|
117
|
+
#
|
118
|
+
# context "Deleting an individual message" do
|
119
|
+
# context "when the api fails to respond" do
|
120
|
+
# setup do
|
121
|
+
# dont_respond_to_http_requests_for_10_seconds
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# should "raise a NotRespondingError" do
|
125
|
+
# assert_raises(Hashblue::API::NotRespondingError) do
|
126
|
+
# Hashblue::API::Message.delete("1234567", "ab44ff33")
|
127
|
+
# end
|
128
|
+
# end
|
129
|
+
# end
|
130
|
+
# end
|
131
|
+
|
132
|
+
# authentication?
|
133
|
+
# pagination?
|
134
|
+
|
135
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class Hashblue::API::MessageTest < Test::Unit::TestCase
|
4
|
+
context "Hashblue::API::Message" do
|
5
|
+
should "return Message for model name" do
|
6
|
+
assert_equal 'Message', Hashblue::API::Message.model_name
|
7
|
+
end
|
8
|
+
|
9
|
+
should "return message for model_name.singular" do
|
10
|
+
assert_equal 'message', Hashblue::API::Message.model_name.singular
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "A message initialized with attributes from the API" do
|
15
|
+
setup do
|
16
|
+
@attributes = {
|
17
|
+
:id => '12345678',
|
18
|
+
:subscriber_id => 'djash12kv2',
|
19
|
+
:timestamp => '2010-03-18T15:03:58Z',
|
20
|
+
:sent => true
|
21
|
+
}
|
22
|
+
@message = Hashblue::API::Message.new(@attributes)
|
23
|
+
end
|
24
|
+
|
25
|
+
should "return timestamp as ruby Time object" do
|
26
|
+
assert_equal Time.utc(2010, 03, 18, 15, 03, 58), @message.timestamp
|
27
|
+
end
|
28
|
+
|
29
|
+
should "be sent if sent is true" do
|
30
|
+
assert @message.sent?
|
31
|
+
end
|
32
|
+
|
33
|
+
should "not be received when sent is true" do
|
34
|
+
assert !@message.received?
|
35
|
+
end
|
36
|
+
|
37
|
+
should "delete the message by calling the API" do
|
38
|
+
Hashblue::API.expects(:delete).with("/subscribers/djash12kv2/messages/12345678.json", anything)
|
39
|
+
@message.delete
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class Hashblue::API::ModelTest < Test::Unit::TestCase
|
4
|
+
context "Hashblue API models in general" do
|
5
|
+
setup do
|
6
|
+
@attributes = {
|
7
|
+
:id => '12345678',
|
8
|
+
:subscriber_id => 'djash12kv2',
|
9
|
+
:anything => 'something'
|
10
|
+
}
|
11
|
+
@message = Hashblue::API::Model.new(@attributes)
|
12
|
+
end
|
13
|
+
|
14
|
+
should "return passed in id as id" do
|
15
|
+
assert_equal @attributes[:id], @message.id
|
16
|
+
end
|
17
|
+
|
18
|
+
should "return other passed in attributes when accessed through methods" do
|
19
|
+
assert_equal @attributes[:subscriber_id], @message.subscriber_id
|
20
|
+
assert_equal @attributes[:anything], @message.anything
|
21
|
+
end
|
22
|
+
|
23
|
+
should "return id when to_param called" do
|
24
|
+
assert_equal @message.id, @message.to_param
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class Hashblue::API::SubscriberTest < Test::Unit::TestCase
|
4
|
+
context "Hashblue::API::Subscriber" do
|
5
|
+
should "return Subscriber for model name" do
|
6
|
+
assert_equal 'Subscriber', Hashblue::API::Subscriber.model_name
|
7
|
+
end
|
8
|
+
|
9
|
+
should "return subscriber for model_name.singular" do
|
10
|
+
assert_equal 'subscriber', Hashblue::API::Subscriber.model_name.singular
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "A subscriber found with an id" do
|
15
|
+
setup do
|
16
|
+
@id = 'abcdef123456'
|
17
|
+
@subscriber = Hashblue::API::Subscriber.find(@id)
|
18
|
+
end
|
19
|
+
|
20
|
+
should "have same id as found with" do
|
21
|
+
assert_equal @id, @subscriber.id
|
22
|
+
end
|
23
|
+
|
24
|
+
should "return messages found at /subscribers/:id/messages.json as messages" do
|
25
|
+
Hashblue::API.expects(:get).with("/subscribers/#{@id}/messages.json", anything).returns(@json = :messages_json)
|
26
|
+
Hashblue::API::Message.expects(:from_json).with(@json).returns(:messages)
|
27
|
+
assert_equal :messages, @subscriber.messages
|
28
|
+
end
|
29
|
+
|
30
|
+
should "return deleted messages found at /subscribers/:id/messages/deleted.json as deleted_messages" do
|
31
|
+
Hashblue::API.expects(:get).with("/subscribers/#{@id}/messages/deleted.json", anything).returns(@json = :messages_json)
|
32
|
+
Hashblue::API::Message.expects(:from_json).with(@json).returns(:messages)
|
33
|
+
assert_equal :messages, @subscriber.deleted_messages
|
34
|
+
end
|
35
|
+
|
36
|
+
should "return contacts found at /subscribers/:id/contacts.json as contacts" do
|
37
|
+
Hashblue::API.expects(:get).with("/subscribers/#{@id}/contacts.json", anything).returns(@json = :contacts_json)
|
38
|
+
Hashblue::API::Contact.expects(:from_json).with(@json).returns(:contacts)
|
39
|
+
assert_equal :contacts, @subscriber.contacts
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashblue-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 8
|
9
|
+
version: 0.0.8
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- FreeRange
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-07 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: httparty
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 5
|
30
|
+
- 2
|
31
|
+
version: 0.5.2
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: activesupport
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 3
|
44
|
+
- 5
|
45
|
+
version: 2.3.5
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description:
|
49
|
+
email: lets@gofreerange.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- README
|
56
|
+
files:
|
57
|
+
- README
|
58
|
+
- Rakefile
|
59
|
+
- hashblue-api.gemspec
|
60
|
+
- lib/dependency/mime_type.rb
|
61
|
+
- lib/dependency/mime_types.rb
|
62
|
+
- lib/hashblue/api.rb
|
63
|
+
- lib/hashblue/api/contact.rb
|
64
|
+
- lib/hashblue/api/error.rb
|
65
|
+
- lib/hashblue/api/message.rb
|
66
|
+
- lib/hashblue/api/model.rb
|
67
|
+
- lib/hashblue/api/request.rb
|
68
|
+
- lib/hashblue/api/subscriber.rb
|
69
|
+
- lib/hashblue/api/test_helper.rb
|
70
|
+
- test/helpers/test_helper_test.rb
|
71
|
+
- test/test_helper.rb
|
72
|
+
- test/unit/api_test.rb
|
73
|
+
- test/unit/contact_test.rb
|
74
|
+
- test/unit/message_api_test.rb
|
75
|
+
- test/unit/message_test.rb
|
76
|
+
- test/unit/model_test.rb
|
77
|
+
- test/unit/subscriber_test.rb
|
78
|
+
has_rdoc: true
|
79
|
+
homepage: http://gofreerange.com
|
80
|
+
licenses: []
|
81
|
+
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options:
|
84
|
+
- --main
|
85
|
+
- README
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
requirements: []
|
103
|
+
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 1.3.6
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: It's the Hashblue API, stupid!
|
109
|
+
test_files: []
|
110
|
+
|