ct_sendyr 0.2.1 → 0.2.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sendyr.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Carl Mercier
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Sendyr
2
+
3
+ A Ruby interface for the wonderful e-mail newsletter application Sendy.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sendyr'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sendyr
18
+
19
+ ## Usage
20
+
21
+ Sendyr.configure do |c|
22
+ c.url = 'http://my.sendy-install.com'
23
+ c.api_key = '1234567890'
24
+ # c.noop = true # You can use this to noop in dev and test environments
25
+ end
26
+
27
+ list_id = 1
28
+ client = Sendyr::Client.new(list_id)
29
+ client.subscribe(email: 'joe@example.org', name: 'Joe Smith', 'FirstName' => 'Joe') # => true
30
+
31
+ client.subscription_status(email: 'joe@example.org') # => :subscribed
32
+
33
+ client.active_subscriber_count # => 1
34
+
35
+ client.unsubscribe(email: 'joe@example.org') # => true
36
+
37
+ client.update_subscription('joe@example.org', email: 'newemail@example.com', name: 'Joe Smith', FirstName => 'Joe') # => true
38
+
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
7
+
@@ -0,0 +1,153 @@
1
+ require 'uri'
2
+
3
+ module Sendyr
4
+ class Client
5
+ attr_reader :last_result, :api_key, :base_uri, :list_id
6
+
7
+ def initialize(list_id = nil)
8
+ @list_id = list_id
9
+ @api_key = Sendyr.configuration.api_key
10
+ @base_uri = Sendyr.configuration.url
11
+ @noop = Sendyr.configuration.noop || false
12
+ end
13
+
14
+ def subscribe(opts = {})
15
+ return noop if @noop
16
+
17
+ opts = {boolean: true, list: @list_id}.merge(opts)
18
+ raise_if_missing_arg([:email, :list], opts)
19
+
20
+ path = '/subscribe'
21
+ result = post_to(path, opts)
22
+
23
+ if result.success? && %w(true 1).include?(clean_body(result))
24
+ respond_with_success(result)
25
+ else
26
+ respond_with_failure(result)
27
+ end
28
+ end
29
+
30
+ def unsubscribe(opts = {})
31
+ return noop if @noop
32
+
33
+ opts = {boolean: true, list: @list_id}.merge(opts)
34
+ raise_if_missing_arg([:email, :list], opts)
35
+
36
+ path = '/unsubscribe'
37
+ result = post_to(path, opts)
38
+
39
+ if result.success? && %w(true 1).include?(clean_body(result))
40
+ respond_with_success(result)
41
+ else
42
+ respond_with_failure(result)
43
+ end
44
+ end
45
+
46
+ def subscription_status(opts = {})
47
+ return noop if @noop
48
+
49
+ opts = {api_key: @api_key, list_id: @list_id}.merge(opts)
50
+ raise_if_missing_arg([:api_key, :email, :list_id, :api_key], opts)
51
+
52
+ path = '/api/subscribers/subscription-status.php'
53
+ result = post_to(path, opts)
54
+
55
+ success_messages = { "subscribed" => :subscribed,
56
+ "unsubscribed" => :unsubscribed,
57
+ "unconfirmed" => :unconfirmed,
58
+ "bounced" => :bounced,
59
+ "soft bounced" => :soft_bounced,
60
+ "complained" => :complained,
61
+ "email does not exist in list" => :not_in_list }
62
+
63
+ cleaned_body = clean_body(result)
64
+ if result.success? && success_messages.keys.include?(cleaned_body)
65
+ respond_with_success(result, success_messages[cleaned_body])
66
+ else
67
+ respond_with_failure(result, underscore(cleaned_body).to_sym)
68
+ end
69
+ end
70
+
71
+ def update_subscription(email, opts = {})
72
+ return noop if @noop
73
+
74
+ status = subscription_status(email: email)
75
+
76
+ return false if status == :not_in_list
77
+
78
+ # Trying to change the email address?
79
+ # Need to unsubscribe and subscribe again.
80
+ if (!opts[:email].nil? && opts[:email] != email) &&
81
+ [:subscribed, :unconfirmed, :bounced, :soft_bounced].include?(status)
82
+ unsubscribe(email: email)
83
+ end
84
+
85
+ unless [:complained, :unsubscribed].include?(status)
86
+ subscribe({email: email}.merge(opts)) == true
87
+ else
88
+ false
89
+ end
90
+ end
91
+
92
+ def active_subscriber_count(opts = {})
93
+ return noop if @noop
94
+
95
+ opts = {api_key: @api_key, list_id: @list_id}.merge(opts)
96
+ raise_if_missing_arg([:list_id, :api_key], opts)
97
+
98
+ path = '/api/subscribers/active-subscriber-count.php'
99
+ result = post_to(path, opts)
100
+
101
+ cleaned_body = clean_body(result)
102
+ if result.success? && !!(cleaned_body =~ /^[-+]?[0-9]+$/)
103
+ respond_with_success(result, cleaned_body.to_i)
104
+ else
105
+ respond_with_failure(result)
106
+ end
107
+ end
108
+
109
+ private
110
+ def raise_if_missing_arg(mandatory_fields, opts)
111
+ mandatory_fields.each do |key|
112
+ if opts[key].nil? || opts[key].to_s.strip == ''
113
+ raise ArgumentError.new("You must specify :#{key}.")
114
+ end
115
+ end; nil
116
+ end
117
+
118
+ def post_to(path, params)
119
+ Faraday.post(url_for(path), params)
120
+ end
121
+
122
+ def url_for(path)
123
+ return File.join(@base_uri, path)
124
+ end
125
+
126
+ def clean_body(result)
127
+ result.body.strip.chomp.downcase
128
+ end
129
+
130
+ def respond_with_success(result, value = nil)
131
+ @last_result = result
132
+ value.nil? ? true : value
133
+ end
134
+
135
+ def respond_with_failure(result, value = nil)
136
+ @last_result = result
137
+ value.nil? ? false : value
138
+ end
139
+
140
+ def underscore(word)
141
+ word.gsub(/::/, '/').
142
+ gsub(/\s/, '_').
143
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
144
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
145
+ tr("-", "_").
146
+ downcase
147
+ end
148
+
149
+ def noop
150
+ :noop
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,3 @@
1
+ module Sendyr
2
+ VERSION = "0.2.1.1"
3
+ end
data/lib/sendyr.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "faraday"
2
+ require "require_all"
3
+ require_all File.dirname(__FILE__) + "/sendyr"
4
+
5
+ module Sendyr
6
+ class << self
7
+ attr_accessor :configuration
8
+ end
9
+
10
+ def self.configure
11
+ self.configuration ||= Configuration.new
12
+ yield(configuration)
13
+ end
14
+
15
+ class Configuration
16
+ attr_accessor :url, :api_key, :noop
17
+
18
+ def initialize
19
+ end
20
+ end
21
+ end
data/sendyr.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sendyr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ct_sendyr"
8
+ spec.version = Sendyr::VERSION
9
+ spec.authors = [" "]
10
+ spec.email = [" "]
11
+ spec.summary = %q{A Ruby interface for the wonderful e-mail newsletter application Sendy.}
12
+ spec.homepage = "http://github.com/coupontom/sendyr"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "faraday"
21
+ spec.add_runtime_dependency "require_all"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "webmock"
27
+ spec.add_development_dependency "pry"
28
+ end
@@ -0,0 +1,207 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sendyr::Client do
4
+ before do
5
+ @base_url = 'http://localhost'
6
+ @api_key = '1234567890'
7
+ @email = 'john@example.org'
8
+ @list_id = '1'
9
+
10
+ Sendyr.configure do |c|
11
+ c.url = @base_url
12
+ c.api_key = @api_key
13
+ end
14
+ end
15
+
16
+ let(:client) { Sendyr::Client.new(@list_id) }
17
+
18
+ describe ".initialize" do
19
+ it "should properly set instance variables" do
20
+ client = Sendyr::Client.new(@list_id)
21
+ client.list_id.should == @list_id
22
+ client.base_uri.should == @base_url
23
+ client.api_key.should == @api_key
24
+ end
25
+ end
26
+
27
+ describe "#subscribe" do
28
+ it "raises exception if email is missing" do
29
+ expect {
30
+ client.subscribe(foo: @email)
31
+ }.to raise_error(ArgumentError, 'You must specify :email.')
32
+ end
33
+
34
+ it "subscribes the email and passes the other arguments" do
35
+ stub_request(:post, "#{@base_url}/subscribe").
36
+ with(:body => {"FirstName"=>"John",
37
+ "boolean"=>"true",
38
+ "email"=> @email,
39
+ "list"=>@list_id,
40
+ "name"=>"John Smith"}).
41
+ to_return(:status => 200, :body => "true")
42
+
43
+ client.subscribe(email: @email, name: 'John Smith', "FirstName" => "John").should == true
44
+ end
45
+
46
+ it "succeeds when the response body is '1'" do
47
+ # The API doc says it should return 'true', but we see '1' in real life.
48
+ stub_request(:post, "#{@base_url}/subscribe").
49
+ with(:body => {"boolean"=>"true",
50
+ "email"=> @email,
51
+ "list"=>@list_id}).
52
+ to_return(:status => 200, :body => "1")
53
+
54
+ client.subscribe(email: @email).should == true
55
+ end
56
+
57
+ it "fails when the response message is an error" do
58
+ stub_request(:post, "#{@base_url}/subscribe").
59
+ with(:body => {"FirstName"=>"John",
60
+ "boolean"=>"true",
61
+ "email"=> @email,
62
+ "list"=>@list_id,
63
+ "name"=>"John Smith"}).
64
+ to_return(:status => 200, :body => "Already subscribed.")
65
+
66
+ client.subscribe(email: @email, name: 'John Smith', "FirstName" => "John").should == false
67
+ end
68
+ end
69
+
70
+ describe "#unsubscribe" do
71
+ it "raises exception if email is missing" do
72
+ expect {
73
+ client.unsubscribe(foo: @email)
74
+ }.to raise_error(ArgumentError, 'You must specify :email.')
75
+ end
76
+
77
+ it "unsubscribes the email" do
78
+ stub_request(:post, "#{@base_url}/unsubscribe").
79
+ with(:body => {"boolean"=>"true",
80
+ "email"=> @email,
81
+ "list"=>@list_id}).
82
+ to_return(:status => 200, :body => "true")
83
+
84
+ client.unsubscribe(email: @email).should == true
85
+ end
86
+
87
+ it "succeeds when the response body is '1'" do
88
+ # The API doc says it should return 'true', but we see '1' in real life.
89
+ stub_request(:post, "#{@base_url}/unsubscribe").
90
+ with(:body => {"boolean"=>"true",
91
+ "email"=> @email,
92
+ "list"=>@list_id}).
93
+ to_return(:status => 200, :body => "1")
94
+
95
+ client.unsubscribe(email: @email).should == true
96
+ end
97
+
98
+ it "fails when the response message is an error" do
99
+ stub_request(:post, "#{@base_url}/unsubscribe").
100
+ with(:body => {"boolean"=>"true",
101
+ "email"=> @email,
102
+ "list"=>@list_id}).
103
+ to_return(:status => 200, :body => "Invalid email address.")
104
+
105
+ client.unsubscribe(email: @email).should == false
106
+ end
107
+ end
108
+
109
+ describe "#subscription_status" do
110
+ it "raises exception if email is missing" do
111
+ expect {
112
+ client.subscription_status(foo: @email)
113
+ }.to raise_error(ArgumentError, 'You must specify :email.')
114
+ end
115
+
116
+ it "returns the correct response when email is not in list" do
117
+ body = "Email does not exist in list"
118
+
119
+ stub_request(:post, "#{@base_url}/api/subscribers/subscription-status.php").
120
+ with(:body => {"api_key"=> @api_key,
121
+ "email" => @email,
122
+ "list_id"=> @list_id}).
123
+ to_return(:status => 200, :body => body)
124
+
125
+ client.subscription_status(email: @email).should == :not_in_list
126
+ end
127
+
128
+ it "returns the correct response when other messages are returned" do
129
+ messages = ["Subscribed","Unsubscribed","Unconfirmed","Bounced","Soft Bounced","Complained"]
130
+ expected_responses = [:subscribed, :unsubscribed, :unconfirmed, :bounced, :soft_bounced, :complained]
131
+
132
+ messages.each_index do |i|
133
+ stub_request(:post, "#{@base_url}/api/subscribers/subscription-status.php").
134
+ with(:body => {"api_key"=> @api_key,
135
+ "email" => @email,
136
+ "list_id"=> @list_id}).
137
+ to_return(:status => 200, :body => messages[i])
138
+
139
+ client.subscription_status(email: @email).should == expected_responses[i]
140
+ end
141
+ end
142
+ end
143
+
144
+ describe "#active_subscriber_count" do
145
+ it "returns the number of subscribers when the body is an integer" do
146
+ stub_request(:post, "#{@base_url}/api/subscribers/active-subscriber-count.php").
147
+ with(:body => {"api_key"=> @api_key,
148
+ "list_id"=> @list_id}).
149
+ to_return(:status => 200, :body => "10")
150
+
151
+ client.active_subscriber_count.should == 10
152
+ end
153
+
154
+ it "returns false when the body is an error message" do
155
+ stub_request(:post, "#{@base_url}/api/subscribers/active-subscriber-count.php").
156
+ with(:body => {"api_key"=> @api_key,
157
+ "list_id"=> @list_id}).
158
+ to_return(:status => 200, :body => "List does not exist")
159
+
160
+ client.active_subscriber_count.should == false
161
+ end
162
+ end
163
+
164
+ describe "#update_subscription" do
165
+ it "returns false if email was never subscribed" do
166
+ client.should_receive(:subscription_status).with(email: @email).and_return(:not_in_list)
167
+ client.should_receive(:unsubscribe).never
168
+ client.should_receive(:subscribe).never
169
+
170
+ client.update_subscription(@email, { name: 'John'}).should == false
171
+ end
172
+
173
+ it "unsubscribes then creates a new subscription if trying to change email address" do
174
+ new_email = 'newemail@example.org'
175
+ name = 'John Smith'
176
+
177
+ client.should_receive(:subscription_status).with(email: @email).and_return(:subscribed)
178
+ client.should_receive(:unsubscribe).with(email: @email).and_return(true)
179
+ client.should_receive(:subscribe).with(email: new_email, name: name).and_return(true)
180
+
181
+ client.update_subscription(@email, { email: 'newemail@example.org', name: name}).should == true
182
+ end
183
+
184
+ it "doesn't change the email if the user complained" do
185
+ new_email = 'newemail@example.org'
186
+ name = 'John Smith'
187
+
188
+ client.should_receive(:subscription_status).with(email: @email).and_return(:complained)
189
+ client.should_receive(:unsubscribe).never
190
+ client.should_receive(:subscribe).never
191
+
192
+ client.update_subscription(@email, { email: 'newemail@example.org', name: name}).should == false
193
+ end
194
+
195
+ it "doesn't change the email if the user unsubscribed" do
196
+ new_email = 'newemail@example.org'
197
+ name = 'John Smith'
198
+
199
+ client.should_receive(:subscription_status).with(email: @email).and_return(:unsubscribed)
200
+ client.should_receive(:unsubscribe).never
201
+ client.should_receive(:subscribe).never
202
+
203
+ client.update_subscription(@email, { email: 'newemail@example.org', name: name}).should == false
204
+ end
205
+
206
+ end
207
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sendyr do
4
+ before do
5
+ @base_url = 'http://localhost'
6
+ Sendyr.configure do |c|
7
+ c.url = @base_url
8
+ end
9
+ end
10
+
11
+ describe ".configure" do
12
+ it "configures itself properly" do
13
+ url = 'http://example.org'
14
+ api_key = 'abcd'
15
+
16
+ Sendyr.configure do |c|
17
+ c.url = url
18
+ c.api_key = api_key
19
+ end
20
+
21
+ Sendyr.configuration.url.should == url
22
+ Sendyr.configuration.api_key.should == api_key
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require File.dirname(__FILE__) + '/../lib/sendyr'
4
+ require 'webmock/rspec'
5
+ require 'pry'
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ct_sendyr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - ''
8
+ - ! ' '
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
@@ -125,11 +125,24 @@ dependencies:
125
125
  version: '0'
126
126
  description:
127
127
  email:
128
- - ''
128
+ - ! ' '
129
129
  executables: []
130
130
  extensions: []
131
131
  extra_rdoc_files: []
132
- files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - lib/sendyr.rb
140
+ - lib/sendyr/client.rb
141
+ - lib/sendyr/version.rb
142
+ - sendyr.gemspec
143
+ - spec/sendyr_client_spec.rb
144
+ - spec/sendyr_spec.rb
145
+ - spec/spec_helper.rb
133
146
  homepage: http://github.com/coupontom/sendyr
134
147
  licenses:
135
148
  - MIT
@@ -155,4 +168,7 @@ rubygems_version: 1.8.25
155
168
  signing_key:
156
169
  specification_version: 3
157
170
  summary: A Ruby interface for the wonderful e-mail newsletter application Sendy.
158
- test_files: []
171
+ test_files:
172
+ - spec/sendyr_client_spec.rb
173
+ - spec/sendyr_spec.rb
174
+ - spec/spec_helper.rb