streamsend-ruby 0.0.1 → 0.0.2.pre1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ The Streamsend Ruby Gem
2
+ ====================
3
+ A Ruby wrapper for the [Streamsend API](http://app.streamsend.com/docs/api/index.html).
4
+
5
+ Installation
6
+ ------------
7
+ gem install streamsend-ruby
8
+
9
+ Documentation
10
+ -------------
11
+ [http://github.com/gaslightsoftware/streamsend-ruby](http://github.com/gaslightsoftware/streamsend-ruby)
12
+
13
+ What's new in 0.0.2?
14
+ ------------------
15
+
16
+ Added create method to Subscriber, added StreamSend::Audience, moved find method into Resource, and added a default rake task.
17
+
18
+ **0.0.1**
19
+
20
+ Everything! This is the initial gemified version forked from the original author [salbertson](http://github.com/salbertson/streamsend-ruby).
21
+
22
+ Usage Examples
23
+ --------------
24
+ require "rubygems"
25
+ require "streamsend"
26
+
27
+ login_id = '1234567890AB'
28
+ key = 'AbCdEfGhIjKlMnOp'
29
+ audience = 2
30
+
31
+ # Configure the connection
32
+ StreamSend.configure(login_id, key)
33
+
34
+ # Get all subscribers for a specific audience
35
+ subscribers = StreamSend::Subscriber.all(audience)
36
+ puts subscribers.count # 100 <- current page limit
37
+
38
+ # Find a subscriber
39
+ subscriber = StreamSend::Subscriber.find(audience, 'bob@example.com')
40
+ puts subscriber.opt_status # "unsubscribed"
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ require 'rake'
3
+ require 'spec'
4
+
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ namespace :spec do
8
+ spec_files = Dir[File.dirname(__FILE__) + '/spec/**/*_spec.rb'].join(' ')
9
+ sh exec "spec #{spec_files}"
10
+ end
11
+
12
+ task :test => :spec
13
+ task :default => :spec
data/lib/spec/version.rb CHANGED
@@ -3,8 +3,8 @@ module Spec # :nodoc:
3
3
  unless defined? MAJOR
4
4
  MAJOR = 0
5
5
  MINOR = 0
6
- TINY = 1
7
- PRE = nil # or pre1, pre2, etc.
6
+ TINY = 2
7
+ PRE = "pre1" # VALID VALUES: nil, "pre1", "pre2", ...
8
8
 
9
9
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
10
10
 
data/lib/streamsend.rb CHANGED
@@ -11,62 +11,139 @@ module StreamSend
11
11
  end
12
12
 
13
13
  class Resource
14
- def initialize(data)
14
+ def initialize(data, from_api=nil)
15
15
  @data = data
16
+ @new_record = !from_api
17
+ end
18
+
19
+ def new_record?
20
+ @new_record
21
+ end
22
+
23
+ def saved!
24
+ @new_record = false
16
25
  end
17
26
 
18
27
  def method_missing(method, *args, &block)
19
- if @data.include?(method.to_s)
28
+ if method.to_s.match(/^(.*)=$/)
29
+ @data[$1] = *args
30
+ elsif @data.has_key?(method.to_s)
20
31
  @data[method.to_s]
21
32
  else
22
33
  super
23
34
  end
24
35
  end
25
36
 
37
+ def attributes
38
+ @data
39
+ end
40
+
26
41
  def id
27
42
  @data["id"]
28
43
  end
44
+
45
+ def self.find(options={})
46
+ response = StreamSend.get(resource_path(options))
47
+ case response.code
48
+ when 200
49
+ if resource = response[resource_name]
50
+ self.new(resource, true)
51
+ elsif resources = response['people']
52
+ results = []
53
+ resources.each { |r| results << self.new(r, true) }
54
+ results.count == 1 ? results.first : results
55
+ else
56
+ nil
57
+ end
58
+ else
59
+ raise "Could not find the #{resource_name}. Make sure the ID is correct. (#{response.code})"
60
+ end
61
+ end
62
+
63
+ def save
64
+ if self.new_record?
65
+ attributes = StreamSend.post(self.class.resource_path("audience_id" => self.audience_id),
66
+ self.attributes)
67
+ self.id = attributes["id"]
68
+ self.created_at = Time.now
69
+ self.saved!
70
+ else
71
+ attributes = StreamSend.put(self.class.resource_path({ "id" => self.id,
72
+ "audience_id" => self.audience_id,
73
+ }.merge( self.attributes)))
74
+ self.updated_at = TIme.now
75
+ self.saved!
76
+ end
77
+ end
78
+
79
+ def self.resource_name
80
+ # we will need to deal with CamelCased names eventually
81
+ self.name.downcase.split('::').last
82
+ end
83
+
84
+ def self.resource_path(options={})
85
+ ""
86
+ end
87
+ end
88
+
89
+ class Audience < Resource
90
+ def self.resource_path(options={})
91
+ "/audiences/#{options["id"]}.xml"
92
+ end
29
93
  end
30
94
 
31
95
  class Subscriber < Resource
96
+
97
+ def email_address
98
+ @data['email_address']
99
+ end
100
+
101
+ def email_address=(val)
102
+ @data['email_address'] = val
103
+ end
104
+
105
+ def to_s
106
+ "#{self.email_address}#{%{ (#{self.email_content_format})} unless self.email_content_format.blank?}"
107
+ end
108
+
32
109
  def self.all(audience_id)
33
110
  response = StreamSend.get("/audiences/#{audience_id}/people.xml")
34
111
 
35
112
  case response.code
36
113
  when 200
37
- response["people"].collect { |data| new(data) }
114
+ response["people"].collect { |data| new(data, true) }
38
115
  else
39
116
  raise "Could not find any subscribers. Make sure your audience ID is correct. (#{response.code})"
40
117
  end
41
118
  end
42
119
 
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
120
+ def self.create(audience_id, subscriber_hash)
121
+ if attributes = StreamSend.post(resource_path("audience_id" => audience_id), subscriber_hash)
122
+ new(attributes, true)
53
123
  else
54
- raise "Could not find the subscriber. Make sure your audience ID is correct. (#{response.code})"
124
+ raise("Could not create the subscriber.")
55
125
  end
56
126
  end
57
127
 
58
- def show(audience_id)
59
- response = StreamSend.get("/audiences/#{audience_id}/people/#{id}.xml")
128
+ def self.find_by_email_address(email_address, options={})
129
+ audience_id = options["audience_id"] || 1
130
+ result = find("audience_id" => audience_id, "email_address" => email_address )
131
+ result == [] ? nil : result
132
+ end
60
133
 
61
- case response.code
62
- when 200
63
- if subscriber = response["person"]
64
- self.class.new(subscriber)
65
- else
66
- nil
67
- end
134
+ def self.resource_name
135
+ "person"
136
+ end
137
+
138
+ def self.resource_path(options={})
139
+ opts = options.dup
140
+ audience_id = opts.delete("audience_id") || 1
141
+ id = opts.delete('id')
142
+ if id.nil?
143
+ params = opts.collect {|k,v| "#{k}=#{v}" }
144
+ "/audiences/#{audience_id}/people.xml?#{params.join('&')}"
68
145
  else
69
- raise "Could not show the subscriber. (#{response.code})"
146
+ "/audiences/#{audience_id}/people/#{id}.xml"
70
147
  end
71
148
  end
72
149
  end
@@ -3,6 +3,16 @@ require 'webmock/rspec'
3
3
  require 'streamsend'
4
4
 
5
5
  describe "StreamSend" do
6
+ before(:each) do
7
+ stub_http_request(:any, //).to_return(:body => "Page not found.", :status => 404)
8
+
9
+ @username = "scott"
10
+ @password = "topsecret"
11
+ @host = "test.host"
12
+
13
+ StreamSend.configure(@username, @password, @host)
14
+ end
15
+
6
16
  describe "Resource" do
7
17
  describe "with missing method" do
8
18
  before(:each) do
@@ -19,23 +29,59 @@ describe "StreamSend" do
19
29
  StreamSend::Resource.new({"id" => 99}).id.should == 99
20
30
  end
21
31
  end
32
+
33
+ describe "#attributes" do
34
+ before do
35
+ @attributes = { "id" => 99, "email_address" => "foo@example.org" }
36
+ end
37
+ it "should return a hash of attributes" do
38
+ StreamSend::Resource.new(@attributes).attributes.should == @attributes
39
+ end
40
+ end
22
41
  end
23
42
 
24
- describe "Subscriber" do
25
- before(:each) do
26
- stub_http_request(:any, //).to_return(:body => "Page not found.", :status => 404)
43
+ describe "Audience" do
44
+ before do
45
+ @audience = StreamSend::Audience.new({"id"=> 1, "name" => "subscriber_list" })
46
+ end
27
47
 
28
- @username = "scott"
29
- @password = "topsecret"
30
- @host = "test.host"
48
+ describe "#find" do
49
+ before(:each) do
50
+ xml = <<-"XML".gsub(/^\s+/,'')
51
+ <?xml version="1.0" encoding="UTF-8"?>
52
+ <audience>
53
+ <active-people-count type="integer">121677</active-people-count>
54
+ <created-at type="datetime">2009-07-23T18:07:46Z</created-at>
55
+ <description nil="true"></description>
56
+ <id type="integer">1</id>
57
+ <inactive-people-count type="integer">23768</inactive-people-count>
58
+ <name>subscriber_list</name>
59
+ <pending-people-count type="integer">8</pending-people-count>
60
+ <status type="enum">active</status>
61
+ <tracking-hash encoding="base64" type="binary">ek1Q
62
+ </tracking-hash>
63
+ <unsubscribed-people-count type="integer">4328</unsubscribed-people-count>
64
+ <updated-at type="datetime">2009-07-23T18:08:16Z</updated-at>
65
+ </audience>
66
+ XML
67
+
68
+ stub_http_request(:any, //).to_return(:body => xml, :status => 200)
69
+ end
31
70
 
32
- StreamSend.configure(@username, @password, @host)
71
+ it "should return an audience object" do
72
+ audience = StreamSend::Audience.find("id" => 1)
73
+ audience.should be_instance_of(StreamSend::Audience)
74
+ audience.id.should == @audience.id
75
+ audience.created_at.should == Time.parse("2009-07-23T18:07:46Z")
76
+ end
33
77
  end
78
+ end
34
79
 
80
+ describe "Subscriber" do
35
81
  describe ".all" do
36
82
  describe "with subscribers" do
37
83
  before(:each) do
38
- xml = <<-XML
84
+ xml = <<-"XML".gsub(/^\s+/,'')
39
85
  <?xml version="1.0" encoding="UTF-8"?>
40
86
  <people type="array">
41
87
  <person>
@@ -62,7 +108,7 @@ describe "StreamSend" do
62
108
 
63
109
  describe "with no subscribers" do
64
110
  before(:each) do
65
- xml = <<-XML
111
+ xml = <<-"XML".gsub(/^\s+/,'')
66
112
  <?xml version="1.0" encoding="UTF-8"?>
67
113
  <people type="array"/>
68
114
  XML
@@ -82,10 +128,10 @@ describe "StreamSend" do
82
128
  end
83
129
  end
84
130
 
85
- describe ".find" do
131
+ describe ".find_by_email_address" do
86
132
  describe "with matching subscriber" do
87
133
  before(:each) do
88
- xml = <<-XML
134
+ xml = <<-"XML".gsub(/^\s+/,'')
89
135
  <?xml version="1.0" encoding="UTF-8"?>
90
136
  <people type="array">
91
137
  <person>
@@ -100,8 +146,7 @@ describe "StreamSend" do
100
146
  end
101
147
 
102
148
  it "should return subscriber" do
103
- subscriber = StreamSend::Subscriber.find(1, "scott@gmail.com")
104
-
149
+ subscriber = StreamSend::Subscriber.find_by_email_address("scott@gmail.com")
105
150
  subscriber.should be_instance_of(StreamSend::Subscriber)
106
151
  subscriber.id.should == 2
107
152
  subscriber.email_address.should == "scott@gmail.com"
@@ -111,7 +156,7 @@ describe "StreamSend" do
111
156
 
112
157
  describe "with no matching subscriber" do
113
158
  before(:each) do
114
- xml = <<-XML
159
+ xml = <<-"XML".gsub(/^\s+/,'')
115
160
  <?xml version="1.0" encoding="UTF-8"?>
116
161
  <people type="array"\>
117
162
  XML
@@ -120,21 +165,22 @@ describe "StreamSend" do
120
165
  end
121
166
 
122
167
  it "should return nil" do
123
- StreamSend::Subscriber.find(1, "bad.email@gmail.com").should == nil
168
+ StreamSend::Subscriber.find_by_email_address("bad.email@gmail.com").should == nil
124
169
  end
125
170
  end
126
171
 
127
172
  describe "with invalid audience" do
128
173
  it "should raise an exception" do
129
- lambda { StreamSend::Subscriber.find(99, "scott@gmail.com") }.should raise_error
174
+ lambda { StreamSend::Subscriber.find_by_email_address("scott@gmail.com", :audience_id => 99) }.should raise_error
130
175
  end
131
176
  end
132
177
  end
133
178
 
179
+ =begin
134
180
  describe "#show" do
135
181
  describe "with valid subscriber instance" do
136
182
  before(:each) do
137
- xml = <<-XML
183
+ xml = <<-"XML".gsub(/^\s+/,'')
138
184
  <?xml version="1.0" encoding="UTF-8"?>
139
185
  <person>
140
186
  <id type="integer">2</id>
@@ -172,5 +218,32 @@ describe "StreamSend" do
172
218
  end
173
219
  end
174
220
  end
221
+ =end
222
+
223
+ describe "#create" do
224
+ before(:each) do
225
+ @subscriber = StreamSend::Subscriber.new(:email_address =>"scott@example.com")
226
+ stub_http_request(:post, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml").with(:body => @subscriber).to_return({ :response => true })
227
+
228
+ @bad_subscriber = StreamSend::Subscriber.new(:email_address =>"scott@example")
229
+ stub_http_request(:post, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml").with(:body => @bad_subscriber).to_return({ :response => Exception })
230
+ end
231
+
232
+ describe "with valid subscriber instance" do
233
+ it "should return instance of subscriber" do
234
+ s = StreamSend::Subscriber.new(@subscriber.attributes.merge("audience_id" => 1))
235
+ s.should be_instance_of(StreamSend::Subscriber)
236
+ s.should be_new_record
237
+ s.save
238
+ s.should_not be_new_record
239
+ end
240
+ end
241
+
242
+ describe "with an invalid email address" do
243
+ it "should raise exception" do
244
+ StreamSend::Subscriber.create(1, @bad_subscriber.attributes).should raise_error
245
+ end
246
+ end
247
+ end
175
248
  end
176
249
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: streamsend-ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease:
4
+ hash: 270495464
5
+ prerelease: true
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ - pre1
11
+ version: 0.0.2.pre1
11
12
  platform: ruby
12
13
  authors:
13
14
  - Gaslight Software
@@ -16,7 +17,7 @@ autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
19
 
19
- date: 2011-07-28 00:00:00 -04:00
20
+ date: 2011-08-04 00:00:00 -04:00
20
21
  default_executable:
21
22
  dependencies:
22
23
  - !ruby/object:Gem::Dependency
@@ -74,6 +75,8 @@ extra_rdoc_files: []
74
75
  files:
75
76
  - .gitignore
76
77
  - Gemfile
78
+ - README.md
79
+ - Rakefile
77
80
  - lib/spec/version.rb
78
81
  - lib/streamsend.rb
79
82
  - spec/streamsend_spec.rb
@@ -99,18 +102,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
102
  required_rubygems_version: !ruby/object:Gem::Requirement
100
103
  none: false
101
104
  requirements:
102
- - - ">="
105
+ - - ">"
103
106
  - !ruby/object:Gem::Version
104
- hash: 3
107
+ hash: 25
105
108
  segments:
106
- - 0
107
- version: "0"
109
+ - 1
110
+ - 3
111
+ - 1
112
+ version: 1.3.1
108
113
  requirements: []
109
114
 
110
115
  rubyforge_project: streamsend-ruby
111
- rubygems_version: 1.4.2
116
+ rubygems_version: 1.3.7
112
117
  signing_key:
113
118
  specification_version: 3
114
- summary: streamsend-ruby 0.0.1
119
+ summary: streamsend-ruby 0.0.2.pre1
115
120
  test_files:
116
121
  - spec/streamsend_spec.rb