uni_sender 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Rakefile +8 -0
- data/lib/uni_sender/camelize.rb +11 -0
- data/lib/uni_sender/version.rb +3 -0
- data/lib/uni_sender.rb +73 -0
- data/spec/data_macros.rb +11 -0
- data/spec/functionality/common_spec.rb +13 -0
- data/spec/functionality/managing_email_spec.rb +28 -0
- data/spec/functionality/managing_list_spec.rb +42 -0
- data/spec/spec.opt +3 -0
- data/spec/spec.opt~ +0 -0
- data/spec/spec_helper.rb +13 -0
- data/uni_sender.gemspec +21 -0
- metadata +122 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
String.class_eval do
|
2
|
+
|
3
|
+
# get from active support library
|
4
|
+
def camelize(first_letter_in_uppercase = true)
|
5
|
+
if first_letter_in_uppercase
|
6
|
+
self.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
7
|
+
else
|
8
|
+
self[0].chr.downcase + self.camelize[1..-1]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/uni_sender.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require "uni_sender/version"
|
2
|
+
require 'uni_sender/camelize'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module UniSender
|
7
|
+
|
8
|
+
class Client
|
9
|
+
|
10
|
+
attr_accessor :api_key, :client_ip, :locale
|
11
|
+
|
12
|
+
def initialize(api_key, params={})
|
13
|
+
self.api_key = api_key
|
14
|
+
params.each do |key, value|
|
15
|
+
if defined?("#{key}=")
|
16
|
+
self.send("#{key}=", value)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def subscribe(params)
|
22
|
+
params['request_ip'] ||= client_ip
|
23
|
+
return default_request('subscribe', params)
|
24
|
+
end
|
25
|
+
|
26
|
+
def client_ip
|
27
|
+
@client_ip || '0.0.0.0'
|
28
|
+
end
|
29
|
+
|
30
|
+
def locale
|
31
|
+
@locale || 'en'
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def translate_params(params)
|
37
|
+
params.inject({}) do |iparams, couple|
|
38
|
+
iparams[couple.first] = case couple.last
|
39
|
+
when String
|
40
|
+
URI.encode(couple.last)
|
41
|
+
when Array
|
42
|
+
couple.last.map{|item| URI.encode(item.to_s)}.join(',')
|
43
|
+
when Hash
|
44
|
+
couple.last.each do |key, value|
|
45
|
+
iparams["#{couple.first}[#{key}]"] = URI.encode(value.to_s)
|
46
|
+
end
|
47
|
+
nil
|
48
|
+
else
|
49
|
+
couple.last
|
50
|
+
end
|
51
|
+
iparams
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def method_missing(undefined_action, *args, &block)
|
56
|
+
params = (args.first.is_a?(Hash) ? args.first : {} )
|
57
|
+
default_request(undefined_action.to_s.camelize(false), params)
|
58
|
+
end
|
59
|
+
|
60
|
+
def default_request(action, params={})
|
61
|
+
params = translate_params(params) if defined?('translate_params')
|
62
|
+
params.merge!({'api_key'=>api_key, 'format'=>'json'})
|
63
|
+
query = make_query(params)
|
64
|
+
JSON.parse(open("http://www.unisender.com/#{locale}/api/#{action}?#{query}").read)
|
65
|
+
end
|
66
|
+
|
67
|
+
def make_query(params)
|
68
|
+
params.map{|key, value| value.nil? ? "" : "#{key}=#{value}"}.join('&')
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
data/spec/data_macros.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe UniSender::Client do
|
4
|
+
|
5
|
+
it 'should get invalid key error' do
|
6
|
+
answer = UniSender::Client.new("").getList
|
7
|
+
answer['error'].should_not be_nil
|
8
|
+
answer['code'].should == 'invalid_api_key'
|
9
|
+
end
|
10
|
+
|
11
|
+
specify{ test_client.get_lists.should == test_client.getLists}
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe UniSender::Client do
|
4
|
+
|
5
|
+
it 'should create email template' do
|
6
|
+
answer = test_client.create_email_message(:sender_name=>'Ваня Петров', :sender_email=>'uni.sender.gem@gmail.com',
|
7
|
+
:subject=>'You need your stuff', :list_id=>available_list_ids.last, :lang=>'en',
|
8
|
+
:body=>"Привет дорогой друг! Тебе одиноко? Купи продукт от фирмы FooCompany и почувствуй себя счастливым.")
|
9
|
+
answer.should include('result')
|
10
|
+
answer['result']['message_id'].should be_an_kind_of(Numeric)
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
it 'should create sms template by name' do
|
15
|
+
answer = test_client.create_sms_message(:sender=>'Tester',
|
16
|
+
:list_id=>available_list_ids.last,:body=>"Просто смска with foo message")
|
17
|
+
answer.should include('result')
|
18
|
+
answer['result']['message_id'].should be_an_kind_of(Numeric)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should create sms template by name' do
|
22
|
+
answer = test_client.create_sms_message(:sender=>'1 234 4567890',
|
23
|
+
:list_id=>available_list_ids.last,:body=>"Просто смска with foo message")
|
24
|
+
answer.should include('result')
|
25
|
+
answer['result']['message_id'].should be_an_kind_of(Numeric)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe UniSender::Client do
|
4
|
+
|
5
|
+
it "should get collection of contact's list" do
|
6
|
+
answer = test_client.getLists
|
7
|
+
answer['result'].should have(3).items
|
8
|
+
answer['result'].map{|item| item['title']}.should include('unisender_spec', 'test_sender')
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should test creation of list' do
|
12
|
+
answer = test_client.createList(:title=>'sample_title')
|
13
|
+
answer['result']['id'].should be_an_kind_of(Numeric)
|
14
|
+
answer = test_client.createList(:title=>'Кириллица тоже')
|
15
|
+
answer['result']['id'].should be_an_kind_of(Numeric)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'subscribe person to lists' do
|
19
|
+
answer = test_client.subscribe(:list_ids=>available_list_ids, :fields=>{:email=>'sam@parson.com', :phone=>'1 234 5678900',
|
20
|
+
:twitter=>'sammy', :name=>'Сеня Парсон'})
|
21
|
+
answer['result']['person_id'].should be_an_kind_of(Numeric)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should unsubscribe from lists by email' do
|
25
|
+
answer = test_client.unsubscribe(:contact_type=>'email', :contact=>'test@mail.com')
|
26
|
+
answer.should_not include('error')
|
27
|
+
answer.should include('result')
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should unsubscribe from lists by phone' do
|
31
|
+
answer = test_client.unsubscribe(:contact_type=>'phone', :contact=>'+1 111 1111111')
|
32
|
+
answer.should_not include('error')
|
33
|
+
answer.should include('result')
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should activate contacts' do
|
37
|
+
answer = test_client.activateContacts(:contact_type=>'email', :contacts=>['test@mail.com', 'john@doe.com'])
|
38
|
+
answer.should include('result')
|
39
|
+
#answer['result']['activated'].should == 2 falls because of rating or test mode
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/spec/spec.opt
ADDED
data/spec/spec.opt~
ADDED
File without changes
|
data/spec/spec_helper.rb
ADDED
data/uni_sender.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/uni_sender/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Sergey Pchelincev"]
|
6
|
+
gem.email = ["jalkoby91@gmail.com"]
|
7
|
+
gem.description = %q{Gem that provide access to unisender.com.ua api}
|
8
|
+
gem.summary = %q{See description}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.add_development_dependency 'rspec'
|
12
|
+
gem.add_development_dependency 'json'
|
13
|
+
gem.add_development_dependency 'ruby-debug'
|
14
|
+
|
15
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
gem.files = `git ls-files`.split("\n")
|
17
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
gem.name = "uni_sender"
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
gem.version = UniSender::VERSION
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uni_sender
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sergey Pchelincev
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-09 00:00:00 +03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: json
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: ruby-debug
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !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
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
description: Gem that provide access to unisender.com.ua api
|
64
|
+
email:
|
65
|
+
- jalkoby91@gmail.com
|
66
|
+
executables: []
|
67
|
+
|
68
|
+
extensions: []
|
69
|
+
|
70
|
+
extra_rdoc_files: []
|
71
|
+
|
72
|
+
files:
|
73
|
+
- .gitignore
|
74
|
+
- Gemfile
|
75
|
+
- Rakefile
|
76
|
+
- lib/uni_sender.rb
|
77
|
+
- lib/uni_sender/camelize.rb
|
78
|
+
- lib/uni_sender/version.rb
|
79
|
+
- spec/data_macros.rb
|
80
|
+
- spec/functionality/common_spec.rb
|
81
|
+
- spec/functionality/managing_email_spec.rb
|
82
|
+
- spec/functionality/managing_list_spec.rb
|
83
|
+
- spec/spec.opt
|
84
|
+
- spec/spec.opt~
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- uni_sender.gemspec
|
87
|
+
has_rdoc: true
|
88
|
+
homepage: ""
|
89
|
+
licenses: []
|
90
|
+
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 3
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
requirements: []
|
115
|
+
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.5.3
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: See description
|
121
|
+
test_files: []
|
122
|
+
|