sailthru-client 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ require 'digest/md5'
2
+
3
+ module Sailthru
4
+ module Helpers
5
+ # params:
6
+ # params, Hash
7
+ # returns:
8
+ # Array, values of each item in the Hash (and nested hashes)
9
+ #
10
+ # Extracts the values of a set of parameters, recursing into nested assoc arrays.
11
+ def extract_param_values(params)
12
+ values = []
13
+ params.each do |k, v|
14
+ if v.class == Hash
15
+ values.concat extract_param_values(v)
16
+ elsif v.class == Array
17
+ temp_hash = {}
18
+ v.each_with_index do |v_,i_|
19
+ temp_hash[i_.to_s] = v_
20
+ end
21
+ values.concat extract_param_values(temp_hash)
22
+ else
23
+ values.push v.to_s
24
+ end
25
+ end
26
+ return values
27
+ end
28
+
29
+ # params:
30
+ # params, Hash
31
+ # secret, String
32
+ # returns:
33
+ # String
34
+ #
35
+ # Returns the unhashed signature string (secret + sorted list of param values) for an API call.
36
+ def get_signature_string(params, secret)
37
+ return secret + extract_param_values(params).sort.join("")
38
+ end
39
+
40
+ # params:
41
+ # params, Hash
42
+ # secret, String
43
+ # returns:
44
+ # String
45
+ #
46
+ # Returns an MD5 hash of the signature string for an API call.
47
+ def get_signature_hash(params, secret)
48
+ Digest::MD5.hexdigest(get_signature_string(params, secret)).to_s
49
+ end
50
+
51
+ # Flatten nested hash for GET / POST request.
52
+ def flatten_nested_hash(hash, brackets = true)
53
+ f = {}
54
+ hash.each do |key, value|
55
+ _key = brackets ? "[#{key}]" : key.to_s
56
+ if value.class == Hash
57
+ flatten_nested_hash(value).each do |k, v|
58
+ f["#{_key}#{k}"] = v
59
+ end
60
+ elsif value.class == Array
61
+ temp_hash = {}
62
+ value.each_with_index do |v, i|
63
+ temp_hash[i.to_s] = v
64
+ end
65
+ flatten_nested_hash(temp_hash).each do |k, v|
66
+ f["#{_key}#{k}"] = v
67
+ end
68
+
69
+ else
70
+ f[_key] = value
71
+ end
72
+ end
73
+ return f
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module Sailthru
2
+ VERSION = '4.0.0'
3
+ end
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/sailthru', __FILE__)
2
+ require File.expand_path('../lib/sailthru/version', __FILE__)
3
3
  require 'date'
4
4
 
5
5
  Gem::Specification.new do |s|
@@ -20,7 +20,8 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_dependency(%q<json>, [">= 0"])
22
22
  s.add_dependency(%q<multipart-post>, [">= 0"])
23
+ s.add_development_dependency(%q<rake>, [">= 0"])
23
24
  s.add_development_dependency(%q<fakeweb>, [">= 0"])
24
- s.add_development_dependency(%q<shoulda>, [">= 0"])
25
+ s.add_development_dependency(%q<minitest>, [">= 5"])
25
26
  s.add_development_dependency(%q<mocha>, [">= 0"])
26
27
  end
@@ -6,7 +6,7 @@ class BlastTest < Minitest::Test
6
6
  api_url = 'http://api.sailthru.com'
7
7
  @secret = 'my_secret'
8
8
  @api_key = 'my_api_key'
9
- @sailthru_client = Sailthru::SailthruClient.new(@api_key, @secret, api_url)
9
+ @sailthru_client = Sailthru::Client.new(@api_key, @secret, api_url)
10
10
  @api_call_url = sailthru_api_call_url(api_url, 'blast')
11
11
  end
12
12
 
@@ -6,7 +6,7 @@ class ContentTest < Minitest::Test
6
6
  api_url = 'http://api.sailthru.com'
7
7
  @secret = 'my_secret'
8
8
  @api_key = 'my_api_key'
9
- @sailthru_client = Sailthru::SailthruClient.new(@api_key, @secret, api_url)
9
+ @sailthru_client = Sailthru::Client.new(@api_key, @secret, api_url)
10
10
  @api_call_url = sailthru_api_call_url(api_url, 'content')
11
11
  end
12
12
 
@@ -6,7 +6,7 @@ class EmailTest < Minitest::Test
6
6
  api_url = 'http://api.sailthru.com'
7
7
  @api_key = 'my_api_key'
8
8
  @secret = 'my_secret'
9
- @sailthru_client = Sailthru::SailthruClient.new(@api_key, @secret, api_url)
9
+ @sailthru_client = Sailthru::Client.new(@api_key, @secret, api_url)
10
10
  @api_call_url = sailthru_api_call_url(api_url, 'email')
11
11
  end
12
12
 
@@ -6,13 +6,13 @@ class ErrorTest < Minitest::Test
6
6
  api_url = 'http://api.sailthru.com'
7
7
  @secret = 'my_secret'
8
8
  @api_key = 'my_api_key'
9
- @sailthru_client = Sailthru::SailthruClient.new(@api_key, @secret, api_url)
9
+ @sailthru_client = Sailthru::Client.new(@api_key, @secret, api_url)
10
10
  @api_call_url = sailthru_api_call_url(api_url, 'blast')
11
11
  end
12
12
 
13
13
  it "raises an error when server misbehaves" do
14
14
  stub_exception(@api_call_url, 'blast_post_update_valid.json')
15
- assert_raises Sailthru::SailthruClientException do
15
+ assert_raises Sailthru::ClientError do
16
16
  @sailthru_client.cancel_blast(123)
17
17
  end
18
18
  end
@@ -21,8 +21,9 @@ class ErrorTest < Minitest::Test
21
21
  stub_exception(@api_call_url, 'blast_post_update_valid.json')
22
22
  begin
23
23
  @sailthru_client.cancel_blast(123)
24
- rescue Exception => e
25
- assert(e.message =~ /Exception from FakeWeb/)
24
+ rescue => e
25
+ assert_match /Exception from FakeWeb/, e.message
26
+ assert_match /Exception from FakeWeb/, e.cause.message if e.respond_to?(:cause) # Ruby 2.1
26
27
  end
27
28
  end
28
29
  end
@@ -8,7 +8,7 @@ class FileUploadTest < Minitest::Test
8
8
  before do
9
9
  @secret = 'my_secret'
10
10
  @api_key = 'my_api_key'
11
- @sailthru_client = Sailthru::SailthruClient.new(
11
+ @sailthru_client = Sailthru::Client.new(
12
12
  @api_key, @secret, 'http://api.sailthru.com'
13
13
  )
14
14
  @api_call_url = sailthru_api_call_url(
@@ -6,14 +6,14 @@ class ListTest < Minitest::Test
6
6
  api_url = 'http://api.sailthru.com'
7
7
  @secret = 'my_secret'
8
8
  @api_key = 'my_api_key'
9
- @sailthru_client = Sailthru::SailthruClient.new(@api_key, @secret, api_url)
9
+ @sailthru_client = Sailthru::Client.new(@api_key, @secret, api_url)
10
10
  @api_call_url = sailthru_api_call_url(api_url, 'list')
11
11
  end
12
12
 
13
13
  it "can get all lists meta data" do
14
14
  query_string = create_json_payload(@api_key, @secret, {})
15
15
  stub_get(@api_call_url + '?' + query_string, 'list_get_all.json')
16
- response = @sailthru_client.get_lists()
16
+ response = @sailthru_client.get_lists
17
17
  assert_equal(response['lists'].length, 2)
18
18
  refute_nil(response['lists'][0]['name'])
19
19
  end
@@ -41,7 +41,7 @@ class ListTest < Minitest::Test
41
41
  list = 'new-list2'
42
42
  primary = 1
43
43
  options = {
44
- 'primary' => primary
44
+ 'primary' => primary
45
45
  }
46
46
  stub_post(@api_call_url, 'list_save_valid.json')
47
47
  response = @sailthru_client.save_list(list, options)
@@ -4,7 +4,7 @@ class PurchaseTest < Minitest::Test
4
4
  describe "API Call: purchase" do
5
5
  before do
6
6
  api_url = 'http://api.sailthru.com'
7
- @sailthru_client = Sailthru::SailthruClient.new("my_api_key", "my_secret", api_url)
7
+ @sailthru_client = Sailthru::Client.new("my_api_key", "my_secret", api_url)
8
8
  @api_call_url = sailthru_api_call_url(api_url, 'purchase')
9
9
  end
10
10
 
@@ -6,7 +6,7 @@ class SendTest < Minitest::Test
6
6
  api_url = 'http://api.sailthru.com'
7
7
  @secret = 'my_secret'
8
8
  @api_key = 'my_api_key'
9
- @sailthru_client = Sailthru::SailthruClient.new("my_api_key", "my_secret", api_url)
9
+ @sailthru_client = Sailthru::Client.new("my_api_key", "my_secret", api_url)
10
10
  @api_call_url = sailthru_api_call_url(api_url, 'send')
11
11
  end
12
12
 
@@ -30,22 +30,6 @@ class SendTest < Minitest::Test
30
30
  assert_equal 12, response['error']
31
31
  end
32
32
 
33
- it "can post send with valid template name and email using deprecated 'send' method" do
34
- template_name = 'default'
35
- email = 'example@example.com'
36
- stub_post(@api_call_url, 'send_get_valid.json')
37
- response = @sailthru_client.send template_name, email, {"name" => "Unix", "myvar" => [1111,2,3], "mycomplexvar" => {"tags" => ["obama", "aaa", "c"]}}
38
- assert_equal template_name, response['template']
39
- end
40
-
41
- it "cannot post send with invalid template name and/or email using deprecated 'send' method" do
42
- template_name = 'invalid-template'
43
- email = 'example@example.com'
44
- stub_post(@api_call_url, 'send_post_invalid.json')
45
- response = @sailthru_client.send template_name, email, {"name" => "Unix", "myvar" => [1111,2,3], "mycomplexvar" => {"tags" => ["obama", "aaa", "c"]}}
46
- assert_equal 14, response['error']
47
- end
48
-
49
33
  it "can post send with valid template name and email" do
50
34
  template_name = 'default'
51
35
  email = 'example@example.com'
@@ -6,7 +6,7 @@ class StatsTest < Minitest::Test
6
6
  api_url = 'http://api.sailthru.com'
7
7
  @secret = 'my_secret'
8
8
  @api_key = 'my_api_key'
9
- @sailthru_client = Sailthru::SailthruClient.new(@api_key, @secret, api_url)
9
+ @sailthru_client = Sailthru::Client.new(@api_key, @secret, api_url)
10
10
  @api_call_url = sailthru_api_call_url(api_url, 'stats')
11
11
  end
12
12
 
@@ -34,7 +34,7 @@ class StatsTest < Minitest::Test
34
34
  params = {'stat' => 'list'}
35
35
  query_string = create_json_payload(@api_key, @secret, params)
36
36
  stub_get(@api_call_url + '?' + query_string, 'stats_lists_valid.json')
37
- response = @sailthru_client.stats_list()
37
+ response = @sailthru_client.stats_list
38
38
  refute_nil response['lists_signup_count']
39
39
  end
40
40
 
@@ -6,7 +6,7 @@ class TemplateTest < Minitest::Test
6
6
  api_url = 'http://api.sailthru.com'
7
7
  @secret = 'my_secret'
8
8
  @api_key = 'my_api_key'
9
- @sailthru_client = Sailthru::SailthruClient.new(@api_key, @secret, api_url)
9
+ @sailthru_client = Sailthru::Client.new(@api_key, @secret, api_url)
10
10
  @api_call_url = sailthru_api_call_url(api_url, 'template')
11
11
  end
12
12
 
data/test/test_helper.rb CHANGED
@@ -49,7 +49,7 @@ class Minitest::Test
49
49
  end
50
50
 
51
51
  def stub_exception(url, filename)
52
- FakeWeb.register_uri(:any, URI.parse(url), :exception => Exception)
52
+ FakeWeb.register_uri(:any, URI.parse(url), :exception => StandardError)
53
53
  end
54
54
 
55
55
  def create_query_string(secret, params)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sailthru-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Prajwal Tuladhar
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-16 00:00:00.000000000 Z
12
+ date: 2014-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -40,7 +40,7 @@ dependencies:
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
- name: fakeweb
43
+ name: rake
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
- name: shoulda
57
+ name: fakeweb
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ! '>='
@@ -67,6 +67,20 @@ dependencies:
67
67
  - - ! '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: minitest
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '5'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '5'
70
84
  - !ruby/object:Gem::Dependency
71
85
  name: mocha
72
86
  requirement: !ruby/object:Gem::Requirement
@@ -91,11 +105,13 @@ files:
91
105
  - .gitignore
92
106
  - CHANGELOG.md
93
107
  - Gemfile
94
- - Gemfile.lock
95
108
  - MIT-LICENSE
96
109
  - README.md
97
110
  - Rakefile
98
111
  - lib/sailthru.rb
112
+ - lib/sailthru/client.rb
113
+ - lib/sailthru/helpers.rb
114
+ - lib/sailthru/version.rb
99
115
  - sailthru-client.gemspec
100
116
  - test/fixtures/blast_delete_invalid.json
101
117
  - test/fixtures/blast_delete_valid.json
data/Gemfile.lock DELETED
@@ -1,22 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- fakeweb (1.3.0)
5
- json (1.8.1)
6
- metaclass (0.0.4)
7
- minitest (5.3.4)
8
- mocha (0.14.0)
9
- metaclass (~> 0.0.1)
10
- multipart-post (2.0.0)
11
- rake (10.3.2)
12
-
13
- PLATFORMS
14
- ruby
15
-
16
- DEPENDENCIES
17
- fakeweb
18
- json
19
- minitest
20
- mocha
21
- multipart-post
22
- rake