sweettooth 1.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.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/CONTRIBUTORS +1 -0
  4. data/Gemfile +2 -0
  5. data/LICENSE +20 -0
  6. data/README.rdoc +32 -0
  7. data/Rakefile +15 -0
  8. data/VERSION +1 -0
  9. data/gemfiles/default-with-activesupport.gemfile +3 -0
  10. data/gemfiles/json.gemfile +4 -0
  11. data/gemfiles/yajl.gemfile +4 -0
  12. data/lib/sweettooth.rb +237 -0
  13. data/lib/sweettooth/account.rb +4 -0
  14. data/lib/sweettooth/activity.rb +10 -0
  15. data/lib/sweettooth/api_operations/create.rb +16 -0
  16. data/lib/sweettooth/api_operations/delete.rb +11 -0
  17. data/lib/sweettooth/api_operations/list.rb +16 -0
  18. data/lib/sweettooth/api_operations/update.rb +57 -0
  19. data/lib/sweettooth/api_resource.rb +38 -0
  20. data/lib/sweettooth/collection_object.rb +35 -0
  21. data/lib/sweettooth/customer.rb +8 -0
  22. data/lib/sweettooth/errors/api_connection_error.rb +4 -0
  23. data/lib/sweettooth/errors/api_error.rb +4 -0
  24. data/lib/sweettooth/errors/authentication_error.rb +4 -0
  25. data/lib/sweettooth/errors/invalid_request_error.rb +10 -0
  26. data/lib/sweettooth/errors/sweettooth_error.rb +20 -0
  27. data/lib/sweettooth/json.rb +21 -0
  28. data/lib/sweettooth/redemption.rb +5 -0
  29. data/lib/sweettooth/redemption_option.rb +5 -0
  30. data/lib/sweettooth/singleton_api_resource.rb +20 -0
  31. data/lib/sweettooth/sweettooth_object.rb +168 -0
  32. data/lib/sweettooth/util.rb +102 -0
  33. data/lib/sweettooth/version.rb +3 -0
  34. data/sweettooth.gemspec +26 -0
  35. data/test/sweettooth/activity_test.rb +13 -0
  36. data/test/sweettooth/customer_test.rb +36 -0
  37. data/test/sweettooth/redemption_option_test.rb +14 -0
  38. data/test/sweettooth/redemption_test.rb +13 -0
  39. data/test/test_helper.rb +168 -0
  40. metadata +192 -0
@@ -0,0 +1,3 @@
1
+ module SweetTooth
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,26 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
+
3
+ require 'sweettooth/version'
4
+
5
+ spec = Gem::Specification.new do |s|
6
+ s.name = 'sweettooth'
7
+ s.version = SweetTooth::VERSION
8
+ s.summary = 'Ruby bindings for the Sweet Tooth API'
9
+ s.description = 'Sweet Tooth is the easiest way to create powerful customer loyalty programs for your business. See https://www.sweettoothrewards.com for details.'
10
+ s.authors = ['Bill Curtis']
11
+ s.email = ['wcurtis@sweettoothhq.com']
12
+ s.homepage = 'https://www.sweettoothrewards.com/api'
13
+
14
+ s.add_dependency('rest-client', '~> 1.4')
15
+ s.add_dependency('mime-types', '~> 1.25')
16
+ s.add_dependency('multi_json', '>= 1.0.4', '< 2')
17
+
18
+ s.add_development_dependency('mocha', '~> 0.13.2')
19
+ s.add_development_dependency('shoulda', '~> 3.4.0')
20
+ s.add_development_dependency('test-unit')
21
+ s.add_development_dependency('rake')
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- test/*`.split("\n")
25
+ s.require_paths = ['lib']
26
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module SweetTooth
4
+ class ActivityTest < Test::Unit::TestCase
5
+
6
+ should "create should return a new activity" do
7
+ @mock.expects(:post).once.returns(test_response(test_activity))
8
+ c = SweetTooth::Activity.create
9
+ assert_equal "act_test_activity", c.id
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module SweetTooth
4
+ class CustomerTest < Test::Unit::TestCase
5
+ should "customers should be listable" do
6
+ @mock.expects(:get).once.returns(test_response(test_customer_array))
7
+ c = SweetTooth::Customer.all.items
8
+ assert c.kind_of? Array
9
+ assert c[0].kind_of? SweetTooth::Customer
10
+ end
11
+
12
+ should "customers should be deletable" do
13
+ @mock.expects(:delete).once.returns(test_response(test_customer({:deleted => true})))
14
+ c = SweetTooth::Customer.new("test_customer")
15
+ c.delete
16
+ assert c.deleted
17
+ end
18
+
19
+ should "customers should be updateable" do
20
+ @mock.expects(:get).once.returns(test_response(test_customer({:email => "lmessi@example.com"})))
21
+ @mock.expects(:post).once.returns(test_response(test_customer({:email => "cronaldo@example.com"})))
22
+ c = SweetTooth::Customer.new("test_customer").refresh
23
+ assert_equal c.email, "lmessi@example.com"
24
+ c.email = "cronaldo@example.com"
25
+ c.save
26
+ assert_equal c.email, "cronaldo@example.com"
27
+ end
28
+
29
+ should "create should return a new customer" do
30
+ @mock.expects(:post).once.returns(test_response(test_customer))
31
+ c = SweetTooth::Customer.create
32
+ assert_equal "cus_test_customer", c.id
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module SweetTooth
4
+ class RedemptionOptionTest < Test::Unit::TestCase
5
+
6
+ should "redemption options should be listable" do
7
+ @mock.expects(:get).once.returns(test_response(test_redemption_option_array))
8
+ c = SweetTooth::RedemptionOption.all.items
9
+ assert c.kind_of? Array
10
+ assert c[0].kind_of? SweetTooth::RedemptionOption
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module SweetTooth
4
+ class RedemptionTest < Test::Unit::TestCase
5
+
6
+ should "create should return a new redemption" do
7
+ @mock.expects(:post).once.returns(test_response(test_redemption))
8
+ r = SweetTooth::Redemption.create
9
+ assert_equal "red_test_redemption", r.id
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,168 @@
1
+ require 'sweettooth'
2
+ require 'test/unit'
3
+ require 'mocha/setup'
4
+ require 'stringio'
5
+ require 'shoulda'
6
+
7
+ #monkeypatch request methods
8
+ module SweetTooth
9
+ @mock_rest_client = nil
10
+
11
+ def self.mock_rest_client=(mock_client)
12
+ @mock_rest_client = mock_client
13
+ end
14
+
15
+ def self.execute_request(opts)
16
+ get_params = (opts[:headers] || {})[:params]
17
+ post_params = opts[:payload]
18
+ case opts[:method]
19
+ when :get then @mock_rest_client.get opts[:url], get_params, post_params
20
+ when :post then @mock_rest_client.post opts[:url], get_params, post_params
21
+ when :delete then @mock_rest_client.delete opts[:url], get_params, post_params
22
+ end
23
+ end
24
+ end
25
+
26
+ def test_response(body, code=200)
27
+ # When an exception is raised, restclient clobbers method_missing. Hence we
28
+ # can't just use the stubs interface.
29
+ body = MultiJson.dump(body) if !(body.kind_of? String)
30
+ m = mock
31
+ m.instance_variable_set('@sweettooth_values', { :body => body, :code => code })
32
+ def m.body; @sweettooth_values[:body]; end
33
+ def m.code; @sweettooth_values[:code]; end
34
+ m
35
+ end
36
+
37
+ def test_customer(params={})
38
+ {
39
+ :_object => "customer",
40
+ :id => "cus_test_customer",
41
+ :account_id => "acc_test_account",
42
+ :first_name => "Wayne",
43
+ :last_name => "Rooney",
44
+ :email => "wrooney@example.com",
45
+ :date_of_birth => "1985-10-24",
46
+ :points_balance => 1200,
47
+ :created => "2014-01-14T17:25:32.000Z",
48
+ :updated => "2014-01-14T17:25:32.000Z"
49
+ }.merge(params)
50
+ end
51
+
52
+ def test_customer_array
53
+ {
54
+ :items => [test_customer, test_customer, test_customer],
55
+ :_object => 'collection',
56
+ :url => '/v1/customers'
57
+ }
58
+ end
59
+
60
+ def test_activity(params={})
61
+ {
62
+ :_object => "activity",
63
+ :id => "act_test_activity",
64
+ :account_id => "acc_test_account",
65
+ :channel_id => "cha_test_channel",
66
+ :customer_id => "cus_test_customer",
67
+ :verb => "signup",
68
+ :object => {},
69
+ :processed => nil,
70
+ :created => "2014-01-14T19:25:32.000Z",
71
+ :updated => "2014-01-14T19:25:32.000Z"
72
+ }.merge(params)
73
+ end
74
+
75
+ def test_redemption(params={})
76
+ {
77
+ :_object => "redemption",
78
+ :id => "red_test_redemption",
79
+ :customer_id => "cus_test_customer",
80
+ :channel_id => "cha_test_channel",
81
+ :redemption_option_id => "rop_test_redemption_option",
82
+ :status => "completed",
83
+ :comment => nil,
84
+ :created => "2014-01-14T19:25:32.000Z",
85
+ :updated => "2014-01-14T19:25:32.000Z"
86
+ }.merge(params)
87
+ end
88
+
89
+ def test_redemption_option(params={})
90
+ {
91
+ :_object => "redemption_option",
92
+ :id => "rop_test_redemption_option",
93
+ :account_id => "acc_test_account",
94
+ :name => "Free Shipping",
95
+ :description => "Spend 100 points to receive free shipping",
96
+ :created => "2014-01-14T17:25:32.000Z",
97
+ :updated => "2014-01-14T17:25:32.000Z"
98
+ }.merge(params)
99
+ end
100
+
101
+ def test_redemption_option_array
102
+ {
103
+ :items => [test_redemption_option, test_redemption_option, test_redemption_option],
104
+ :_object => 'collection',
105
+ :url => '/v1/redemption_options'
106
+ }
107
+ end
108
+
109
+ def test_invalid_api_key_error
110
+ {
111
+ "error" => {
112
+ "type" => "invalid_request_error",
113
+ "message" => "Invalid API Key provided: invalid"
114
+ }
115
+ }
116
+ end
117
+
118
+ def test_invalid_exp_year_error
119
+ {
120
+ "error" => {
121
+ "code" => "invalid_expiry_year",
122
+ "param" => "exp_year",
123
+ "type" => "card_error",
124
+ "message" => "Your card's expiration year is invalid"
125
+ }
126
+ }
127
+ end
128
+
129
+ def test_missing_id_error
130
+ {
131
+ :error => {
132
+ :param => "id",
133
+ :type => "invalid_request_error",
134
+ :message => "Missing id"
135
+ }
136
+ }
137
+ end
138
+
139
+ def test_api_error
140
+ {
141
+ :error => {
142
+ :type => "api_error"
143
+ }
144
+ }
145
+ end
146
+
147
+ def test_delete_discount_response
148
+ {
149
+ :deleted => true,
150
+ :id => "di_test_coupon"
151
+ }
152
+ end
153
+
154
+ class Test::Unit::TestCase
155
+ include Mocha
156
+
157
+ setup do
158
+ @mock = mock
159
+ SweetTooth.mock_rest_client = @mock
160
+ SweetTooth.api_key="sk_test_q6p2uvfgQzgposDZ4n1qJdUM"
161
+ end
162
+
163
+ teardown do
164
+ SweetTooth.mock_rest_client = nil
165
+ SweetTooth.api_key=nil
166
+ end
167
+ end
168
+
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sweettooth
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bill Curtis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mime-types
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.25'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.25'
41
+ - !ruby/object:Gem::Dependency
42
+ name: multi_json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.4
48
+ - - <
49
+ - !ruby/object:Gem::Version
50
+ version: '2'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: 1.0.4
58
+ - - <
59
+ - !ruby/object:Gem::Version
60
+ version: '2'
61
+ - !ruby/object:Gem::Dependency
62
+ name: mocha
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 0.13.2
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: 0.13.2
75
+ - !ruby/object:Gem::Dependency
76
+ name: shoulda
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ version: 3.4.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: 3.4.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: test-unit
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: Sweet Tooth is the easiest way to create powerful customer loyalty programs
118
+ for your business. See https://www.sweettoothrewards.com for details.
119
+ email:
120
+ - wcurtis@sweettoothhq.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - .gitignore
126
+ - CONTRIBUTORS
127
+ - Gemfile
128
+ - LICENSE
129
+ - README.rdoc
130
+ - Rakefile
131
+ - VERSION
132
+ - gemfiles/default-with-activesupport.gemfile
133
+ - gemfiles/json.gemfile
134
+ - gemfiles/yajl.gemfile
135
+ - lib/sweettooth.rb
136
+ - lib/sweettooth/account.rb
137
+ - lib/sweettooth/activity.rb
138
+ - lib/sweettooth/api_operations/create.rb
139
+ - lib/sweettooth/api_operations/delete.rb
140
+ - lib/sweettooth/api_operations/list.rb
141
+ - lib/sweettooth/api_operations/update.rb
142
+ - lib/sweettooth/api_resource.rb
143
+ - lib/sweettooth/collection_object.rb
144
+ - lib/sweettooth/customer.rb
145
+ - lib/sweettooth/errors/api_connection_error.rb
146
+ - lib/sweettooth/errors/api_error.rb
147
+ - lib/sweettooth/errors/authentication_error.rb
148
+ - lib/sweettooth/errors/invalid_request_error.rb
149
+ - lib/sweettooth/errors/sweettooth_error.rb
150
+ - lib/sweettooth/json.rb
151
+ - lib/sweettooth/redemption.rb
152
+ - lib/sweettooth/redemption_option.rb
153
+ - lib/sweettooth/singleton_api_resource.rb
154
+ - lib/sweettooth/sweettooth_object.rb
155
+ - lib/sweettooth/util.rb
156
+ - lib/sweettooth/version.rb
157
+ - sweettooth.gemspec
158
+ - test/sweettooth/activity_test.rb
159
+ - test/sweettooth/customer_test.rb
160
+ - test/sweettooth/redemption_option_test.rb
161
+ - test/sweettooth/redemption_test.rb
162
+ - test/test_helper.rb
163
+ homepage: https://www.sweettoothrewards.com/api
164
+ licenses: []
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - '>='
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubyforge_project:
182
+ rubygems_version: 2.1.11
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: Ruby bindings for the Sweet Tooth API
186
+ test_files:
187
+ - test/sweettooth/activity_test.rb
188
+ - test/sweettooth/customer_test.rb
189
+ - test/sweettooth/redemption_option_test.rb
190
+ - test/sweettooth/redemption_test.rb
191
+ - test/test_helper.rb
192
+ has_rdoc: