streamsend 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ Rakefile
3
+ lib/streamsend.rb
4
+ spec/streamsend_spec.rb
5
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1 @@
1
+ Ruby wrapper for the StreamSend API.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('streamsend', '0.1.0') do |p|
6
+ p.description = "Ruby wrapper for the StreamSend API."
7
+ p.url = "http://github.com/salbertson/streamsend-ruby"
8
+ p.author = "Scott Albertson"
9
+ p.email = "salbertson@streamsend.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/lib/streamsend.rb ADDED
@@ -0,0 +1,95 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+
4
+ module StreamSend
5
+ include HTTParty
6
+ format :xml
7
+
8
+ def self.configure(username, password, host = "app.streamsend.com")
9
+ base_uri host
10
+ basic_auth username, password
11
+ end
12
+
13
+ class Resource
14
+ def initialize(data)
15
+ @data = data
16
+ end
17
+
18
+ def method_missing(method, *args, &block)
19
+ if @data.include?(method.to_s)
20
+ @data[method.to_s]
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def id
27
+ @data["id"]
28
+ end
29
+ end
30
+
31
+ class Subscriber < Resource
32
+ def self.all(audience_id)
33
+ response = StreamSend.get("/audiences/#{audience_id}/people.xml")
34
+
35
+ case response.code
36
+ when 200
37
+ response["people"].collect { |data| new(data) }
38
+ else
39
+ raise "Could not find any subscribers. Make sure your audience ID is correct. (#{response.code})"
40
+ end
41
+ end
42
+
43
+ def self.find(audience_id, email_address)
44
+ response = StreamSend.get("/audiences/#{audience_id}/people.xml?email_address=#{email_address}")
45
+
46
+ case response.code
47
+ when 200
48
+ if subscriber = response["people"].first
49
+ new(subscriber)
50
+ else
51
+ nil
52
+ end
53
+ else
54
+ raise "Could not find the subscriber. Make sure your audience ID is correct. (#{response.code})"
55
+ end
56
+ end
57
+
58
+ def show
59
+ response = StreamSend.get("/audiences/#{audience_id}/people/#{id}.xml")
60
+
61
+ case response.code
62
+ when 200
63
+ if subscriber = response["person"]
64
+ self.class.new(subscriber)
65
+ else
66
+ nil
67
+ end
68
+ else
69
+ raise "Could not show the subscriber. (#{response.code})"
70
+ end
71
+ end
72
+
73
+ def activate
74
+ response = StreamSend.post("/audiences/#{audience_id}/people/#{id}/activate.xml")
75
+
76
+ case response.code
77
+ when 200
78
+ true
79
+ else
80
+ raise "Could not activate the subscriber. (#{response.code})"
81
+ end
82
+ end
83
+
84
+ def unsubscribe
85
+ response = StreamSend.post("/audiences/#{audience_id}/people/#{id}/unsubscribe.xml")
86
+
87
+ case response.code
88
+ when 200
89
+ true
90
+ else
91
+ raise "Could not subscribe the subscriber. (#{response.code})"
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,214 @@
1
+ require 'uri'
2
+ require 'webmock/rspec'
3
+ require 'streamsend'
4
+
5
+ describe "StreamSend" do
6
+ describe "Resource" do
7
+ describe "with missing method" do
8
+ before(:each) do
9
+ @resource = StreamSend::Resource.new({"name" => "scott"})
10
+ end
11
+
12
+ it "should return value" do
13
+ @resource.name.should == "scott"
14
+ end
15
+ end
16
+
17
+ describe "#id" do
18
+ it "should return id" do
19
+ StreamSend::Resource.new({"id" => 99}).id.should == 99
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "Subscriber" do
25
+ before(:each) do
26
+ stub_http_request(:any, //).to_return(:body => "Page not found.", :status => 404)
27
+
28
+ @username = "scott"
29
+ @password = "topsecret"
30
+ @host = "test.host"
31
+
32
+ StreamSend.configure(@username, @password, @host)
33
+ end
34
+
35
+ describe ".all" do
36
+ describe "with subscribers" do
37
+ before(:each) do
38
+ xml = <<-XML
39
+ <?xml version="1.0" encoding="UTF-8"?>
40
+ <people type="array">
41
+ <person>
42
+ <id type="integer">2</id>
43
+ <email-address>scott@gmail.com</email-address>
44
+ <created-at type="datetime">2009-09-18T01:27:05Z</created-at>
45
+ </person>
46
+ </people>
47
+ XML
48
+
49
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml").to_return(:body => xml)
50
+ end
51
+
52
+ it "should return array of one subscriber object" do
53
+ subscribers = StreamSend::Subscriber.all(1)
54
+ subscribers.size.should == 1
55
+
56
+ subscribers.first.should be_instance_of(StreamSend::Subscriber)
57
+ subscribers.first.id.should == 2
58
+ subscribers.first.email_address.should == "scott@gmail.com"
59
+ subscribers.first.created_at.should == Time.parse("2009-09-18T01:27:05Z")
60
+ end
61
+ end
62
+
63
+ describe "with no subscribers" do
64
+ before(:each) do
65
+ xml = <<-XML
66
+ <?xml version="1.0" encoding="UTF-8"?>
67
+ <people type="array"/>
68
+ XML
69
+
70
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml").to_return(:body => xml)
71
+ end
72
+
73
+ it "should return an empty array" do
74
+ StreamSend::Subscriber.all(1).should == []
75
+ end
76
+ end
77
+
78
+ describe "with invalid audience" do
79
+ it "should raise an exception" do
80
+ lambda { StreamSend::Subscriber.all(99) }.should raise_error
81
+ end
82
+ end
83
+ end
84
+
85
+ describe ".find" do
86
+ describe "with matching subscriber" do
87
+ before(:each) do
88
+ xml = <<-XML
89
+ <?xml version="1.0" encoding="UTF-8"?>
90
+ <people type="array">
91
+ <person>
92
+ <id type="integer">2</id>
93
+ <email-address>scott@gmail.com</email-address>
94
+ <created-at type="datetime">2009-09-18T01:27:05Z</created-at>
95
+ </person>
96
+ </people>
97
+ XML
98
+
99
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml?email_address=scott@gmail.com").to_return(:body => xml)
100
+ end
101
+
102
+ it "should return subscriber" do
103
+ subscriber = StreamSend::Subscriber.find(1, "scott@gmail.com")
104
+
105
+ subscriber.should be_instance_of(StreamSend::Subscriber)
106
+ subscriber.id.should == 2
107
+ subscriber.email_address.should == "scott@gmail.com"
108
+ subscriber.created_at.should == Time.parse("2009-09-18T01:27:05Z")
109
+ end
110
+ end
111
+
112
+ describe "with no matching subscriber" do
113
+ before(:each) do
114
+ xml = <<-XML
115
+ <?xml version="1.0" encoding="UTF-8"?>
116
+ <people type="array"\>
117
+ XML
118
+
119
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml?email_address=bad.email@gmail.com").to_return(:body => xml)
120
+ end
121
+
122
+ it "should return nil" do
123
+ StreamSend::Subscriber.find(1, "bad.email@gmail.com").should == nil
124
+ end
125
+ end
126
+
127
+ describe "with invalid audience" do
128
+ it "should raise an exception" do
129
+ lambda { StreamSend::Subscriber.find(99, "scott@gmail.com") }.should raise_error
130
+ end
131
+ end
132
+ end
133
+
134
+ describe "#show" do
135
+ describe "with valid subscriber instance" do
136
+ before(:each) do
137
+ xml = <<-XML
138
+ <?xml version="1.0" encoding="UTF-8"?>
139
+ <person>
140
+ <id type="integer">2</id>
141
+ <email-address>scott@gmail.com</email-address>
142
+ <created-at type="datetime">2009-09-18T01:27:05Z</created-at>
143
+ <first-name>Scott</first-name>
144
+ <last-name>Albertson</last-name>
145
+ </person>
146
+ XML
147
+
148
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/1/people/2.xml").to_return(:body => xml)
149
+ end
150
+
151
+ it "should return subscriber" do
152
+ subscriber = StreamSend::Subscriber.new({"id" => 2, "audience_id" => 1}).show
153
+
154
+ subscriber.should be_instance_of(StreamSend::Subscriber)
155
+ subscriber.id.should == 2
156
+ subscriber.email_address.should == "scott@gmail.com"
157
+ subscriber.created_at.should == Time.parse("2009-09-18T01:27:05Z")
158
+ subscriber.first_name.should == "Scott"
159
+ subscriber.last_name.should == "Albertson"
160
+ end
161
+ end
162
+
163
+ describe "with invalid subscriber instance" do
164
+ it "should raise exception" do
165
+ lambda { StreamSend::Subscriber.new({"id" => 99, "audience_id" => 1}).show }.should raise_error
166
+ end
167
+ end
168
+
169
+ describe "with invalid audience" do
170
+ it "should raise exception" do
171
+ lambda { StreamSend::Subscriber.new({"id" => 2}).show }.should raise_error
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "#activate" do
177
+ before(:each) do
178
+ stub_http_request(:post, "http://#{@username}:#{@password}@#{@host}/audiences/1/people/2/activate.xml").to_return(:body => nil)
179
+ end
180
+
181
+ describe "with valid subscriber" do
182
+ it "should be successful" do
183
+ response = StreamSend::Subscriber.new({"id" => 2, "audience_id" => 1}).activate
184
+ response.should be_true
185
+ end
186
+ end
187
+
188
+ describe "with invalid subscriber" do
189
+ it "should raise exception" do
190
+ lambda { StreamSend::Subscriber.new({"id" => 99, "audience_id" => 1}).activate }.should raise_error
191
+ end
192
+ end
193
+ end
194
+
195
+ describe "#unsubscribe" do
196
+ before(:each) do
197
+ stub_http_request(:post, "http://#{@username}:#{@password}@#{@host}/audiences/1/people/2/unsubscribe.xml").to_return(:body => nil)
198
+ end
199
+
200
+ describe "with valid subscriber" do
201
+ it "should be successful" do
202
+ response = StreamSend::Subscriber.new({"id" => 2, "audience_id" => 1}).unsubscribe
203
+ response.should be_true
204
+ end
205
+ end
206
+
207
+ describe "with invalid subscriber" do
208
+ it "should raise exception" do
209
+ lambda { StreamSend::Subscriber.new({"id" => 99, "audience_id" => 1}).unsubscribe }.should raise_error
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{streamsend}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Scott Albertson"]
9
+ s.date = %q{2012-02-10}
10
+ s.description = %q{Ruby wrapper for the StreamSend API.}
11
+ s.email = %q{salbertson@streamsend.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/streamsend.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "lib/streamsend.rb", "spec/streamsend_spec.rb", "Manifest", "streamsend.gemspec"]
14
+ s.homepage = %q{http://github.com/salbertson/streamsend-ruby}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Streamsend", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{streamsend}
18
+ s.rubygems_version = %q{1.5.2}
19
+ s.summary = %q{Ruby wrapper for the StreamSend API.}
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ else
26
+ end
27
+ else
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: streamsend
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Scott Albertson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-02-10 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Ruby wrapper for the StreamSend API.
23
+ email: salbertson@streamsend.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ - lib/streamsend.rb
31
+ files:
32
+ - README.rdoc
33
+ - Rakefile
34
+ - lib/streamsend.rb
35
+ - spec/streamsend_spec.rb
36
+ - Manifest
37
+ - streamsend.gemspec
38
+ has_rdoc: true
39
+ homepage: http://github.com/salbertson/streamsend-ruby
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --line-numbers
45
+ - --inline-source
46
+ - --title
47
+ - Streamsend
48
+ - --main
49
+ - README.rdoc
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 11
67
+ segments:
68
+ - 1
69
+ - 2
70
+ version: "1.2"
71
+ requirements: []
72
+
73
+ rubyforge_project: streamsend
74
+ rubygems_version: 1.5.2
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Ruby wrapper for the StreamSend API.
78
+ test_files: []
79
+