citrus-mail 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +95 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/citrus-mail.gemspec +64 -0
- data/lib/citrus_mail.rb +67 -0
- data/lib/citrus_mail/client.rb +77 -0
- data/lib/citrus_mail/list.rb +90 -0
- data/lib/citrus_mail/response.rb +35 -0
- data/test/helper.rb +18 -0
- data/test/test_client.rb +40 -0
- data/test/test_list.rb +30 -0
- data/test/test_response.rb +83 -0
- metadata +129 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.0)
|
6
|
+
bundler (~> 1.0.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.8.7)
|
10
|
+
rcov (0.9.9)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.6.0)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Galdomedia
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
= citrus-mail
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Example usage
|
6
|
+
|
7
|
+
=== Creating list instance(representing freshmial subscription list):
|
8
|
+
|
9
|
+
You can create client instance and then get list
|
10
|
+
@client = CitrusMail::Client.new('your_api_key')
|
11
|
+
@list = @client.get_list('your_list_key')
|
12
|
+
|
13
|
+
or create list
|
14
|
+
|
15
|
+
@list = CitrusMail::List.new('you_api_key', 'your_list_key')
|
16
|
+
|
17
|
+
=== Available CitrusMail::List methods
|
18
|
+
|
19
|
+
===== @list.add_subscriber(email, name, custom_fields, options)
|
20
|
+
Adds new subscriber with given email.
|
21
|
+
|
22
|
+
Params:
|
23
|
+
* name: name for subscriber(default: nil)
|
24
|
+
* cusom_fields: custom fileds for subscriber(default: {})
|
25
|
+
* options: only one option exists 'confirm_email' (if true subscriber will get email with confirmation link) (default: {:confirm_email => true})
|
26
|
+
|
27
|
+
|
28
|
+
===== @list.confirm(email)
|
29
|
+
Confirm subscriber with given email.
|
30
|
+
|
31
|
+
===== @list.add_and_confirm_subscriber(email)
|
32
|
+
Add new subscriber with given email and make it confirmed.
|
33
|
+
|
34
|
+
Params are the same as in add_subscriber method, except options[:confirm_email] which is always false.
|
35
|
+
|
36
|
+
===== @list.modify_subscriber(emial, fields)
|
37
|
+
Modify subscriber with given email.
|
38
|
+
|
39
|
+
Params:
|
40
|
+
* fields: fields to change. :name is maped to 'freshmail_name' , and every else is maped to 'freshmail_custom_field' (default: {})
|
41
|
+
|
42
|
+
===== @list.change_state(identifier, state)
|
43
|
+
Modify state subscriber with given email.
|
44
|
+
|
45
|
+
Params:
|
46
|
+
* state(Fixnum): possible values: 4 - unsubscribed, 6 - removed
|
47
|
+
|
48
|
+
===== @list.get_empty_fields(email)
|
49
|
+
Get empty fields for subscriber with given email.
|
50
|
+
|
51
|
+
===== @list.remove(email)
|
52
|
+
Remove(unsubscribe) subscriber with given email.
|
53
|
+
|
54
|
+
|
55
|
+
All this methods will raise
|
56
|
+
* CitrusMail::RequestFailed if http request faild
|
57
|
+
* CitrusMail::InvalidAPIKey if api key is invalid (FreshMail return code 104)
|
58
|
+
* CitrusMail::InvalidListKey if list key is invalid (FreshMail return code 101)
|
59
|
+
* CitrusMail::InvalidEmail if email is invalid (FreshMail return code 202)
|
60
|
+
* CitrusMail::EmailExists if email already exists for list (FreshMail return code 201)
|
61
|
+
* CitrusMail::SubscriberNotExists if subscriber not exists (FreshMail return code 206)
|
62
|
+
All above exceptions are descendants of CitrusMail::CitrusMailError.
|
63
|
+
* and CitrusMail::CitrusMailError if FreshMail return code is greater than 100
|
64
|
+
|
65
|
+
=== CitrusMail::Response
|
66
|
+
After each call to FreshMail api, CitrusMail::Response object is created.
|
67
|
+
CitrusMail::Response is responsible for parsing http response, and raise CitrusMail exceptions if needed.
|
68
|
+
You can access CitrusMail::Response instance by calling :response method of CitrusMail::List object.
|
69
|
+
Example:
|
70
|
+
|
71
|
+
@list = CitrusMail::List.new('you_api_key', 'your_list_key')
|
72
|
+
@list.response # => nil
|
73
|
+
@list.add_subscriber('test@test.com')
|
74
|
+
@resposne = @list.response
|
75
|
+
Accessing http response:
|
76
|
+
@response.http_response # => #<Net::HTTPOK 200 OK readbody=true>
|
77
|
+
Accessing FreshMail code:
|
78
|
+
@response.code # => 2
|
79
|
+
Accessing FreshMail response (value of <response> xml tag returned by FreshMail)
|
80
|
+
@response.freshmail_response # => '2'
|
81
|
+
|
82
|
+
== Contributing to citrus-mail
|
83
|
+
|
84
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
85
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
86
|
+
* Fork the project
|
87
|
+
* Start a feature/bugfix branch
|
88
|
+
* Commit and push until you are happy with your contribution
|
89
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
90
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
91
|
+
|
92
|
+
== Copyright
|
93
|
+
|
94
|
+
Copyright (c) 2011 Galdomedia. See LICENSE.txt for
|
95
|
+
further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "citrus-mail"
|
18
|
+
gem.homepage = "https://github.com/galdomedia/citrus-mail"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Nifty Rails gem for nice FreshMail integration}
|
21
|
+
gem.description = %Q{Gem that gives you easy integration with polish mailing service FreshMail (freshmail.pl)}
|
22
|
+
gem.email = "piotr@galdomedia.pl"
|
23
|
+
gem.authors = ["Piotr Boniecki", "GaldoMedia"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "citrus-test #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/citrus-mail.gemspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{citrus-mail}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Piotr Boniecki", "GaldoMedia"]
|
12
|
+
s.date = %q{2011-05-17}
|
13
|
+
s.description = %q{Gem that gives you easy integration with polish mailing service FreshMail (freshmail.pl)}
|
14
|
+
s.email = %q{piotr@galdomedia.pl}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"citrus-mail.gemspec",
|
27
|
+
"lib/citrus_mail.rb",
|
28
|
+
"lib/citrus_mail/client.rb",
|
29
|
+
"lib/citrus_mail/list.rb",
|
30
|
+
"lib/citrus_mail/response.rb",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/test_client.rb",
|
33
|
+
"test/test_list.rb",
|
34
|
+
"test/test_response.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{https://github.com/galdomedia/citrus-mail}
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.6}
|
40
|
+
s.summary = %q{Nifty Rails gem for nice FreshMail integration}
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
|
49
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
50
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
54
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
55
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
60
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
61
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
data/lib/citrus_mail.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
module CitrusMail
|
2
|
+
HOST = 'https://app.freshmail.pl'
|
3
|
+
ERRORS = {
|
4
|
+
101 => 'niepoprawny klucz określający listę subskrypcyjną',
|
5
|
+
102 => 'brak wymaganego pola email',
|
6
|
+
103 => 'klucze identyfikujące pola dodatkowe nie należą do tej listy subskrypcyjnej',
|
7
|
+
104 => 'klucz identyfikujący api jest niepoprawny',
|
8
|
+
201 => 'podany email jest już zapisany do danej listy subskrypcyjnej',
|
9
|
+
202 => 'podany adres email jest niepoprawny',
|
10
|
+
203 => 'długość znaków dla pól dodatkowych, bądź nazwy została przekroczona',
|
11
|
+
204 => 'podane kodowanie znaków jest nieprawidłowe',
|
12
|
+
205 => 'dane wprowadzone od pól dodatkowych przekraczają dozwoloną długość 255 znaków',
|
13
|
+
206 => 'nie ma takiego subskrybenta',
|
14
|
+
207 => 'niepoprawne kodowanie',
|
15
|
+
999 => 'błąd połączenia do bazy danych'
|
16
|
+
}
|
17
|
+
|
18
|
+
class CitrusMailError < StandardError
|
19
|
+
def initialize(code_or_message=nil)
|
20
|
+
if code_or_message.is_a?(Fixnum)
|
21
|
+
code_or_message = "#{code_or_message} - #{CitrusMail::ERRORS[code_or_message] || "Unknown error"}"
|
22
|
+
end
|
23
|
+
super(code_or_message)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
#raised when http request is failed
|
28
|
+
class RequestFailed < CitrusMailError
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
#raised when api key is invalid (FreshMail return code 104)
|
33
|
+
class InvalidAPIKey < CitrusMailError
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
#raised when list key is invalid (FreshMail return code 101)
|
38
|
+
class InvalidListKey < CitrusMailError
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
#raised when email is invalid (FreshMail return code 202)
|
43
|
+
class InvalidEmail < CitrusMailError
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
#raised when email already exists for list (FreshMail return code 201)
|
48
|
+
class EmailExists < CitrusMailError
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
#raised when subscriber not exists (FreshMail return code 206)
|
53
|
+
class SubscriberNotExists < CitrusMailError
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
require 'citrus_mail/client'
|
59
|
+
require 'citrus_mail/list'
|
60
|
+
require 'citrus_mail/response'
|
61
|
+
|
62
|
+
# jtidhzdxlp
|
63
|
+
|
64
|
+
#require 'citrus_mail'
|
65
|
+
#cm = CitrusMail::Client.new('0633bb5ff7191ff48c4f065aa17f0321')
|
66
|
+
#list = cm.get_list('hi03a662gw')
|
67
|
+
#r = list.add_subscriber('piotr@galdomedia.pl')
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
|
4
|
+
module CitrusMail
|
5
|
+
class Client
|
6
|
+
attr_accessor :api_key, :encoding
|
7
|
+
|
8
|
+
def initialize(api_key, options={})
|
9
|
+
@api_key = api_key
|
10
|
+
@encoding = options[:encoding] if options[:encoding]
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_list(key)
|
14
|
+
List.new(self, key)
|
15
|
+
end
|
16
|
+
|
17
|
+
#actions = [modify, get_empty_fields, change_state, confirm, remove, add_subscriber]
|
18
|
+
def path_for(action)
|
19
|
+
"/main.php?moduleName=fm_api&action=#{action}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def connection
|
23
|
+
@connection ||= begin
|
24
|
+
uri = URI.parse(HOST)
|
25
|
+
connection = Net::HTTP.new(uri.host, uri.port)
|
26
|
+
if uri.scheme == "https" # enable SSL/TLS
|
27
|
+
connection.use_ssl = true
|
28
|
+
connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
29
|
+
end
|
30
|
+
connection
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def http_get(url, params)
|
35
|
+
response = connection.start do |http|
|
36
|
+
url_with_params = url + "&" + process_params(params).map { |k, v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }.join('&')
|
37
|
+
get = Net::HTTP::Get.new(url_with_params)
|
38
|
+
http.request(get)
|
39
|
+
end
|
40
|
+
Net::HTTPOK
|
41
|
+
CitrusMail::Response.build_from_http_response(response)
|
42
|
+
end
|
43
|
+
|
44
|
+
def http_post(path, params)
|
45
|
+
response = connection.start do |http|
|
46
|
+
post = Net::HTTP::Post.new(path)
|
47
|
+
post.set_form_data(process_params(params))
|
48
|
+
http.request(post)
|
49
|
+
end
|
50
|
+
CitrusMail::Response.build_from_http_response(response)
|
51
|
+
end
|
52
|
+
|
53
|
+
def process_params(params)
|
54
|
+
hash = {:api_key => api_key}
|
55
|
+
hash[:encoding] = @encoding if @encoding
|
56
|
+
hash.merge!(params)
|
57
|
+
flatten_params(hash)
|
58
|
+
end
|
59
|
+
|
60
|
+
def flatten_params(params)
|
61
|
+
params.inject({}) do |h, (key, value)|
|
62
|
+
if value.is_a?(Hash)
|
63
|
+
value.each_pair { |k, v| h["#{key}[#{k}]"] = v.to_s }
|
64
|
+
else
|
65
|
+
h[key] = value.to_s
|
66
|
+
end
|
67
|
+
h
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
#copied from ''net/http'
|
72
|
+
def urlencode(str)
|
73
|
+
str.gsub(/[^a-zA-Z0-9_\.\-]/n) { |s| sprintf('%%%02x', s[0]) }
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module CitrusMail
|
2
|
+
class List
|
3
|
+
attr_accessor :key, :client, :response
|
4
|
+
|
5
|
+
def initialize(client_or_api_key, key)
|
6
|
+
@key = key
|
7
|
+
if client_or_api_key.is_a?(CitrusMail::Client)
|
8
|
+
@client = client_or_api_key
|
9
|
+
else
|
10
|
+
@client = CitrusMail::Client.new(client_or_api_key)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_subscriber(email, name=nil, custom_fields={}, options={:confirm_email => true})
|
15
|
+
params = {:freshmail_email => email}
|
16
|
+
params[:freshmail_name] = name if name
|
17
|
+
params[:freshmail_custom_field] = custom_fields if custom_fields.empty?
|
18
|
+
unless options[:confirm_email].nil?
|
19
|
+
params[:confirm_email] = options[:confirm_email]
|
20
|
+
end
|
21
|
+
|
22
|
+
path = client.path_for(:add_subscriber)
|
23
|
+
make_post_request(path, params)
|
24
|
+
end
|
25
|
+
|
26
|
+
def confirm(identifier)
|
27
|
+
params = {:freshmail_subscriber => identifier}
|
28
|
+
|
29
|
+
path = client.path_for(:confirm)
|
30
|
+
make_get_request(path, params)
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_and_confirm_subscriber(email, name=nil, custom_fields={}, options={})
|
34
|
+
options[:confirm_email] = false
|
35
|
+
add_subscriber(email, name, custom_fields, options)
|
36
|
+
confirm(email)
|
37
|
+
end
|
38
|
+
|
39
|
+
def modify_subscriber(email, fields={})
|
40
|
+
params = {:freshmail_subscriber => email}
|
41
|
+
if name = fields.delete(:name)
|
42
|
+
params[:freshmail_name] = name
|
43
|
+
end
|
44
|
+
params[:freshmail_custom_field] = fields unless fields.empty?
|
45
|
+
|
46
|
+
path = client.path_for(:modify)
|
47
|
+
make_post_request(path, params)
|
48
|
+
end
|
49
|
+
|
50
|
+
#4 – subskrybent wypisany
|
51
|
+
#6 – subskrybent skasowany
|
52
|
+
def change_state(identifier, state)
|
53
|
+
params = {:freshmail_subscriber => identifier, :state => state}
|
54
|
+
|
55
|
+
path = client.path_for(:change_state)
|
56
|
+
make_post_request(path, params)
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_empty_fields(email)
|
60
|
+
params = {:freshmail_subscriber => email}
|
61
|
+
|
62
|
+
path = client.path_for(:get_empty_fields)
|
63
|
+
make_get_request(path, params)
|
64
|
+
end
|
65
|
+
|
66
|
+
def remove(identifier)
|
67
|
+
params = {:freshmail_subscriber => identifier}
|
68
|
+
|
69
|
+
path = client.path_for(:remove)
|
70
|
+
make_get_request(path, params)
|
71
|
+
end
|
72
|
+
|
73
|
+
protected
|
74
|
+
|
75
|
+
def make_post_request(path, params)
|
76
|
+
self.response = nil
|
77
|
+
params = {:freshmail_list => key}.merge(params)
|
78
|
+
self.response = client.http_post(path, params)
|
79
|
+
response.code
|
80
|
+
end
|
81
|
+
|
82
|
+
def make_get_request(path, params)
|
83
|
+
self.response = nil
|
84
|
+
params = {:freshmail_list => key}.merge(params)
|
85
|
+
self.response = client.http_get(path, params)
|
86
|
+
response.code
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module CitrusMail
|
2
|
+
class Response
|
3
|
+
attr_accessor :http_response, :code, :freshmail_response
|
4
|
+
|
5
|
+
def self.build_from_http_response(http_response)
|
6
|
+
response = self.new()
|
7
|
+
response.http_response = http_response
|
8
|
+
response.process_http_response
|
9
|
+
response
|
10
|
+
end
|
11
|
+
|
12
|
+
def process_http_response
|
13
|
+
if http_response.code.to_i == 200 and res = (r = http_response.body.match(/\<response\>(.*)\<\/response\>/) and r[1])
|
14
|
+
self.freshmail_response = res
|
15
|
+
self.code = res.to_i
|
16
|
+
if code > 100
|
17
|
+
raise InvalidListKey.new(code) if code == 101
|
18
|
+
raise InvalidAPIKey.new(code) if code == 104
|
19
|
+
raise EmailExists.new(code) if code == 201
|
20
|
+
raise InvalidEmail.new(code) if code == 202
|
21
|
+
raise SubscriberNotExists.new(code) if code == 206
|
22
|
+
|
23
|
+
raise CitrusMailError.new(code)
|
24
|
+
end
|
25
|
+
else
|
26
|
+
raise RequestFailed
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def parse_json_freshmail_response
|
31
|
+
ActiveSupport::JSON.decode freshmail_response
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
14
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
15
|
+
require 'citrus_mail'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
data/test/test_client.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestClient < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "CitrusMail::Client" do
|
6
|
+
setup do
|
7
|
+
@api_key = 'fake_key'
|
8
|
+
@client = CitrusMail::Client.new(@api_key)
|
9
|
+
end
|
10
|
+
|
11
|
+
context "#flatten_params with {'a' => 'b', 'c' => {'d' => 'e', 'f' => 'g'}}" do
|
12
|
+
should "return {'a' => 'b', 'c[d]' => 'e', 'c[f]' => 'g'}" do
|
13
|
+
params = {'a' => 'b', 'c' => {'d' => 'e', 'f' => 'g'}}
|
14
|
+
ret = @client.flatten_params(params)
|
15
|
+
assert ret == {'a' => 'b', 'c[d]' => 'e', 'c[f]' => 'g'}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "#process_params with {'a' => 'b'}" do
|
20
|
+
should "return {'a' => 'b', :api_key => 'fake_key'}" do
|
21
|
+
params = {'a' => 'b'}
|
22
|
+
ret = @client.process_params(params)
|
23
|
+
assert ret == {'a' => 'b', :api_key => @api_key}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "#get_list with list_key" do
|
28
|
+
setup do
|
29
|
+
@list_key = "fake_list_key"
|
30
|
+
end
|
31
|
+
|
32
|
+
should "return CitrusMail::List instance" do
|
33
|
+
list = @client.get_list(@list_key)
|
34
|
+
assert list.is_a?(CitrusMail::List)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/test/test_list.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestList < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "CitrusMail::List" do
|
6
|
+
setup do
|
7
|
+
@api_key = 'fake_key'
|
8
|
+
@list_key = 'fake_list_key'
|
9
|
+
end
|
10
|
+
|
11
|
+
should "should be properly initialized and initialize client instance" do
|
12
|
+
list = CitrusMail::List.new(@api_key, @list_key)
|
13
|
+
assert list.client.is_a?(CitrusMail::Client)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "CitrusMail::List" do
|
18
|
+
setup do
|
19
|
+
@api_key = 'fake_key'
|
20
|
+
@client = CitrusMail::Client.new(@api_key)
|
21
|
+
@list_key = 'fake_list_key'
|
22
|
+
@list = @client.get_list(@list_key)
|
23
|
+
end
|
24
|
+
|
25
|
+
should "" do
|
26
|
+
assert true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class FakeHttpResponse < Struct.new(:code, :body)
|
4
|
+
end
|
5
|
+
|
6
|
+
class TestResponse < Test::Unit::TestCase
|
7
|
+
|
8
|
+
context "CitrusMail::Response" do
|
9
|
+
setup do
|
10
|
+
end
|
11
|
+
|
12
|
+
context "when http code is not 200" do
|
13
|
+
setup do
|
14
|
+
@http_response = FakeHttpResponse.new('100', '<root><response>1</response></root>')
|
15
|
+
end
|
16
|
+
should "raise CitrusMail::RequestFailed" do
|
17
|
+
exception = nil
|
18
|
+
begin
|
19
|
+
CitrusMail::Response.build_from_http_response(@http_response)
|
20
|
+
rescue => e
|
21
|
+
exception = e
|
22
|
+
end
|
23
|
+
assert exception.is_a?(CitrusMail::RequestFailed)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when http code is 200 and response body has no <response> xml tag" do
|
28
|
+
setup do
|
29
|
+
@http_response = FakeHttpResponse.new('200', 'something')
|
30
|
+
end
|
31
|
+
should "raise CitrusMail::RequestFailed" do
|
32
|
+
exception = nil
|
33
|
+
begin
|
34
|
+
CitrusMail::Response.build_from_http_response(@http_response)
|
35
|
+
rescue => e
|
36
|
+
exception = e
|
37
|
+
end
|
38
|
+
assert exception.is_a?(CitrusMail::RequestFailed)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when freshmail response is greater than 100" do
|
43
|
+
setup do
|
44
|
+
@http_response = FakeHttpResponse.new('200', '<root><response>201</response></root>')
|
45
|
+
end
|
46
|
+
should "raise CitrusMail::CitrusMailError" do
|
47
|
+
exception = nil
|
48
|
+
begin
|
49
|
+
CitrusMail::Response.build_from_http_response(@http_response)
|
50
|
+
rescue => e
|
51
|
+
exception = e
|
52
|
+
end
|
53
|
+
assert exception.is_a?(CitrusMail::CitrusMailError)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "when freshmail response is greater than 100" do
|
58
|
+
setup do
|
59
|
+
@http_response = FakeHttpResponse.new('200', '<root><response>201</response></root>')
|
60
|
+
end
|
61
|
+
should "raise CitrusMail::CitrusMailError" do
|
62
|
+
exception = nil
|
63
|
+
begin
|
64
|
+
CitrusMail::Response.build_from_http_response(@http_response)
|
65
|
+
rescue => e
|
66
|
+
exception = e
|
67
|
+
end
|
68
|
+
assert exception.is_a?(CitrusMail::CitrusMailError)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "when freshmail response is lower than 100" do
|
73
|
+
setup do
|
74
|
+
@http_response = FakeHttpResponse.new('200', '<root><response>1</response></root>')
|
75
|
+
end
|
76
|
+
should "return CitrusMail::Response" do
|
77
|
+
ret = CitrusMail::Response.build_from_http_response(@http_response)
|
78
|
+
assert ret.is_a?(CitrusMail::Response)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: citrus-mail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Piotr Boniecki
|
13
|
+
- GaldoMedia
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-17 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
segments:
|
27
|
+
- 1
|
28
|
+
- 0
|
29
|
+
- 0
|
30
|
+
version: 1.0.0
|
31
|
+
prerelease: false
|
32
|
+
requirement: *id001
|
33
|
+
name: bundler
|
34
|
+
type: :development
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 1
|
42
|
+
- 6
|
43
|
+
- 0
|
44
|
+
version: 1.6.0
|
45
|
+
prerelease: false
|
46
|
+
requirement: *id002
|
47
|
+
name: jeweler
|
48
|
+
type: :development
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
prerelease: false
|
58
|
+
requirement: *id003
|
59
|
+
name: rcov
|
60
|
+
type: :development
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
prerelease: false
|
70
|
+
requirement: *id004
|
71
|
+
name: shoulda
|
72
|
+
type: :development
|
73
|
+
description: Gem that gives you easy integration with polish mailing service FreshMail (freshmail.pl)
|
74
|
+
email: piotr@galdomedia.pl
|
75
|
+
executables: []
|
76
|
+
|
77
|
+
extensions: []
|
78
|
+
|
79
|
+
extra_rdoc_files:
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.rdoc
|
82
|
+
files:
|
83
|
+
- Gemfile
|
84
|
+
- Gemfile.lock
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.rdoc
|
87
|
+
- Rakefile
|
88
|
+
- VERSION
|
89
|
+
- citrus-mail.gemspec
|
90
|
+
- lib/citrus_mail.rb
|
91
|
+
- lib/citrus_mail/client.rb
|
92
|
+
- lib/citrus_mail/list.rb
|
93
|
+
- lib/citrus_mail/response.rb
|
94
|
+
- test/helper.rb
|
95
|
+
- test/test_client.rb
|
96
|
+
- test/test_list.rb
|
97
|
+
- test/test_response.rb
|
98
|
+
has_rdoc: true
|
99
|
+
homepage: https://github.com/galdomedia/citrus-mail
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
version: "0"
|
121
|
+
requirements: []
|
122
|
+
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.3.6
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Nifty Rails gem for nice FreshMail integration
|
128
|
+
test_files: []
|
129
|
+
|