letter-mx 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p429
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in letter-mx.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ letter-mx (1.0.0)
5
+ activeresource
6
+ httparty
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.0.0)
12
+ activesupport (= 4.0.0)
13
+ builder (~> 3.1.0)
14
+ activeresource (4.0.0)
15
+ activemodel (~> 4.0)
16
+ activesupport (~> 4.0)
17
+ rails-observers (~> 0.1.1)
18
+ activesupport (4.0.0)
19
+ i18n (~> 0.6, >= 0.6.4)
20
+ minitest (~> 4.2)
21
+ multi_json (~> 1.3)
22
+ thread_safe (~> 0.1)
23
+ tzinfo (~> 0.3.37)
24
+ atomic (1.1.14)
25
+ builder (3.1.4)
26
+ httparty (0.12.0)
27
+ json (~> 1.8)
28
+ multi_xml (>= 0.5.2)
29
+ i18n (0.6.5)
30
+ json (1.8.1)
31
+ minitest (4.7.5)
32
+ multi_json (1.8.2)
33
+ multi_xml (0.5.5)
34
+ rails-observers (0.1.2)
35
+ activemodel (~> 4.0)
36
+ rake (10.1.0)
37
+ thread_safe (0.1.3)
38
+ atomic
39
+ tzinfo (0.3.38)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ bundler (~> 1.3)
46
+ letter-mx!
47
+ rake
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bjoern B. Dorra
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,269 @@
1
+ # Letter::Mx
2
+
3
+ The Letter-MX-GEM is a lightweight GEM for accessing the Letter.MX subscriber REST web services.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ `gem 'letter-mx'`
10
+
11
+ And then execute:
12
+
13
+ `$ bundle`
14
+
15
+ Or install it yourself as:
16
+
17
+ `$ gem install letter-mx`
18
+
19
+ ## Usage
20
+
21
+ ### Basics
22
+
23
+ ```
24
+ # Instantiate the API that is ready to make calls to Letter.mx
25
+ lettermx = LetterMX::Api.new(ENV["app_key"], ENV["api_key"])
26
+
27
+ # Call one of the provided methods (e.g. list_subscribers)
28
+ response = lettermx.list_subscribers(query: {offset: 0, limit: 3})
29
+
30
+ # Get data from the response
31
+ subscribers = response.data
32
+
33
+ # Print useful http informations
34
+ puts "--- HTTP -----------------------------------------------------------------------"
35
+ puts "Status: #{response.http_status}"
36
+ puts "Body:"
37
+ puts response.http_body
38
+ puts
39
+
40
+ # Iterate over the result and print details of each subscriber
41
+ subscribers.each do |subscriber|
42
+ puts "--- Subscriber -----------------------------------------------------------------"
43
+ puts "ID.................: #{subscriber["id"]}"
44
+ puts "User ID............: #{subscriber["user_id"]}"
45
+ puts "Key................: #{subscriber["key"]}"
46
+ puts "Email..............: #{subscriber["email"]}"
47
+ puts "Firstname..........: #{subscriber["first_name"]}"
48
+ puts "Lastname...........: #{subscriber["last_name"]}"
49
+ puts "Optional 1.........: #{subscriber["optional_1"]}"
50
+ puts "Optional 2.........: #{subscriber["optional_2"]}"
51
+ puts "Optional 3.........: #{subscriber["optional_3"]}"
52
+ puts "Status.............: #{subscriber["status"]}"
53
+ puts "Created At.........: #{subscriber["created_at"]}"
54
+ puts "Updated At.........: #{subscriber["updated_at"]}"
55
+ puts "Soft Bounce Count..: #{subscriber["soft_bounce_count"]}"
56
+ puts
57
+ end
58
+ ```
59
+
60
+ ### Get Subscribers (`GET /api/contacts`)
61
+
62
+ ```
63
+ lettermx = LetterMX::Api.new(ENV["app_key"], ENV["api_key"])
64
+ response = lettermx.list_subscribers
65
+ puts response.data
66
+ ```
67
+
68
+ ```
69
+ [
70
+ {
71
+ "id"=>100,
72
+ "user_id"=>1,
73
+ "key"=>"ojgcrmjs",
74
+ "email"=>"email1@example.com",
75
+ "first_name"=>nil,
76
+ "last_name"=>nil,
77
+ "optional_1"=>nil,
78
+ "optional_2"=>nil,
79
+ "optional_3"=>nil,
80
+ "status"=>"active",
81
+ "created_at"=>"2013-11-09T07:43:48.000+01:00",
82
+ "updated_at"=>"2013-11-09T07:43:48.000+01:00",
83
+ "soft_bounce_count"=>0
84
+ },
85
+ {
86
+ "id"=>101,
87
+ "user_id"=>1,
88
+ "key"=>"jyyazhbl",
89
+ "email"=>"email2@example.com",
90
+ "first_name"=>nil,
91
+ "last_name"=>nil,
92
+ "optional_1"=>nil,
93
+ "optional_2"=>nil,
94
+ "optional_3"=>nil,
95
+ "status"=>"active",
96
+ "created_at"=>"2013-11-11T12:41:48.000+01:00",
97
+ "updated_at"=>"2013-11-11T12:41:48.000+01:00",
98
+ "soft_bounce_count"=>0
99
+ },
100
+ ...
101
+ ]
102
+ ```
103
+
104
+ ### Count Subscribers (`GET /api/contacts/count`)
105
+
106
+ ```
107
+ lettermx = LetterMX::Api.new(ENV["app_key"], ENV["api_key"])
108
+ response = lettermx.count_subscribers
109
+ puts response.data
110
+ ```
111
+
112
+ ```
113
+ {
114
+ "count"=>5
115
+ }
116
+ ```
117
+
118
+ ### Get Subscriber (`GET /api/contacts/100`)
119
+
120
+ ```
121
+ lettermx = LetterMX::Api.new(ENV["app_key"], ENV["api_key"])
122
+ response = lettermx.get_subscribers(100)
123
+ puts response.data
124
+ ```
125
+
126
+ ```
127
+ {
128
+ "id"=>100,
129
+ "user_id"=>1,
130
+ "key"=>"ojgcrmjs",
131
+ "email"=>"email1@example.com",
132
+ "first_name"=>nil,
133
+ "last_name"=>nil,
134
+ "optional_1"=>nil,
135
+ "optional_2"=>nil,
136
+ "optional_3"=>nil,
137
+ "status"=>"active",
138
+ "created_at"=>"2013-11-09T07:43:48.000+01:00",
139
+ "updated_at"=>"2013-11-09T07:43:48.000+01:00",
140
+ "soft_bounce_count"=>0
141
+ }
142
+ ```
143
+
144
+ ### Add Subscriber (`POST /api/contacts`)
145
+
146
+ ```
147
+ lettermx = LetterMX::Api.new(ENV["app_key"], ENV["api_key"])
148
+
149
+ new_subscriber = {
150
+ email: "john.doe@example.com",
151
+ first_name: "John",
152
+ last_name: "Doe"
153
+ }
154
+
155
+ response = lettermx.add_subscriber(new_subscriber)
156
+ puts response.data
157
+ ```
158
+
159
+ ```
160
+ {
161
+ "id": 110,
162
+ "user_id": 1,
163
+ "key": "tbythqbn",
164
+ "email": "john.doe@example.com",
165
+ "first_name": "John",
166
+ "last_name": "Doe",
167
+ "optional_1": null,
168
+ "optional_2": null,
169
+ "optional_3": null,
170
+ "status": "unconfirmed",
171
+ "created_at": "2013-11-14T15:01:48.000+01:00",
172
+ "updated_at": "2013-11-14T15:01:48.000+01:00",
173
+ "soft_bounce_count": 0
174
+ }
175
+ ```
176
+
177
+ ### Update Subscriber (`PUT /api/contacts/100`)
178
+
179
+ ```
180
+ lettermx = LetterMX::Api.new(ENV["app_key"], ENV["api_key"])
181
+
182
+ existing_subscriber = {
183
+ first_name: "John",
184
+ last_name: "Doe"
185
+ }
186
+
187
+ response = lettermx.update_subscriber(100, existing_subscriber)
188
+ puts response.data
189
+ ```
190
+
191
+ ```
192
+ {
193
+ "id"=>100,
194
+ "user_id"=>1,
195
+ "key"=>"ojgcrmjs",
196
+ "email"=>"email1@example.com",
197
+ "first_name"=>"John",
198
+ "last_name"=>"Doe",
199
+ "optional_1"=>nil,
200
+ "optional_2"=>nil,
201
+ "optional_3"=>nil,
202
+ "status"=>"active",
203
+ "created_at"=>"2013-11-09T07:43:48.000+01:00",
204
+ "updated_at"=>"2013-11-09T07:43:48.000+01:00",
205
+ "soft_bounce_count"=>0
206
+ }
207
+ ```
208
+
209
+ ### Remove Subscriber (`DELETE /api/contacts/100`)
210
+
211
+ ```
212
+ lettermx = LetterMX::Api.new(ENV["app_key"], ENV["api_key"])
213
+ response = lettermx.remove_subscriber(100)
214
+ puts response.data
215
+ ```
216
+
217
+ ```
218
+ {
219
+ "id"=>100,
220
+ "user_id"=>1,
221
+ "key"=>"ojgcrmjs",
222
+ "email"=>"email1@example.com",
223
+ "first_name"=>"John",
224
+ "last_name"=>"Doe",
225
+ "optional_1"=>nil,
226
+ "optional_2"=>nil,
227
+ "optional_3"=>nil,
228
+ "status"=>"deleted",
229
+ "created_at"=>"2013-11-09T07:43:48.000+01:00",
230
+ "updated_at"=>"2013-11-09T07:43:48.000+01:00",
231
+ "soft_bounce_count"=>0
232
+ }
233
+ ```
234
+
235
+ ### Get API limit (default) (`GET /api/contacts/limit-default`)
236
+
237
+ ```
238
+ lettermx = LetterMX::Api.new(ENV["app_key"], ENV["api_key"])
239
+ response = lettermx.subscribers_api_limit_default
240
+ puts response.data
241
+ ```
242
+
243
+ ```
244
+ {
245
+ "limit_default"=>250
246
+ }
247
+ ```
248
+
249
+ ### Get API limit (max) (`GET /api/contacts/limit-max`)
250
+
251
+ ```
252
+ lettermx = LetterMX::Api.new(ENV["app_key"], ENV["api_key"])
253
+ response = lettermx.subscribers_api_limit_max
254
+ puts response.data
255
+ ```
256
+
257
+ ```
258
+ {
259
+ "limit_max"=>3000
260
+ }
261
+ ```
262
+
263
+ ## Contributing
264
+
265
+ 1. Fork it
266
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
267
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
268
+ 4. Push to the branch (`git push origin my-new-feature`)
269
+ 5. Create new Pull Request
data/RELEASING ADDED
@@ -0,0 +1,4 @@
1
+ Releasing Letter.MX API GEM
2
+
3
+ $ gem build letter_mx.gemspec
4
+ $ gem push letter_mx-X.Y.Z.gem
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/letter_mx.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'letter_mx/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "letter-mx"
8
+ spec.version = LetterMX::VERSION
9
+ spec.authors = ["Bjoern B. Dorra", "Eduard Siebert"]
10
+ spec.description = %q{The Letter-MX-GEM is a lightweight GEM for accessing the Letter.MX subscriber REST web services.}
11
+ spec.summary = %q{The Letter-MX-GEM is a lightweight GEM for accessing the Letter.MX subscriber REST web services.}
12
+ spec.homepage = "http://www.letter.mx"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/).delete_if{|e| e == "test.rb"}
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_dependency("httparty")
21
+ spec.add_dependency("activeresource")
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
data/lib/letter_mx.rb ADDED
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require "letter_mx/version"
4
+ require "letter_mx/api"
5
+ require "letter_mx/subscriber"
6
+
7
+ module LetterMX
8
+
9
+ class Error < StandardError; end
10
+ class SSLRequiredError < Error; end
11
+ class AuthenticationFailed < Error; end
12
+ class ListenFailed < Error; end
13
+
14
+ end
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+
3
+ require 'httparty'
4
+
5
+ module LetterMX
6
+ # connection = LetterMX::API.new( username: 'the_app_key', password: 'the_api_key' )
7
+ # connection.add_subscriber( {
8
+ # email: 'user@domain.com',
9
+ # first_name: 'John',
10
+ # last_name: 'Doe',
11
+ # optional_1: 'Customer',
12
+ # optional_2: 'Active',
13
+ # optional_3: 'Male',
14
+ # optin: false
15
+ # } )
16
+
17
+ class API
18
+
19
+ CONTENT_TYPES = {
20
+ json: "json",
21
+ # TODO: Implement XML
22
+ # xml: "xml"
23
+ }
24
+
25
+ # https://github.com/jnunemaker/httparty
26
+ include HTTParty
27
+ base_uri "www.letter.mx/api"
28
+
29
+ class ApiException < RuntimeError; end
30
+
31
+
32
+ def initialize(username, password, type=LetterMX::API::CONTENT_TYPES[:json])
33
+ raise ArgumentError, "First argument (username) must not be blank." if username.nil? || username.size == 0
34
+ raise ArgumentError, "Second argument (password) must not be blank." if password.nil? || password.size == 0
35
+ raise ArgumentError, "Third argument (type) must be on of #{LetterMX::API::CONTENT_TYPES.values}." unless LetterMX::API::CONTENT_TYPES.has_value?(type)
36
+
37
+ @auth = { username: username, password: password }
38
+ @type = type
39
+ @default_options = { basic_auth: @auth }
40
+
41
+ case @type
42
+ when LetterMX::API::CONTENT_TYPES[:json]
43
+ @default_options.merge!({
44
+ headers: {
45
+ "Accept" => "application/json",
46
+ "Content-Type" => "application/json; charset=utf-8"
47
+ }
48
+ })
49
+ # TODO: Implement XML
50
+ # when LetterMX::API::CONTENT_TYPES[:xml]
51
+ # @default_options.merge!({
52
+ # headers: {
53
+ # "Accept" => "application/xml",
54
+ # "Content-Type" => "application/xml"
55
+ # }
56
+ # })
57
+ end
58
+ end
59
+
60
+ def list_subscribers(options={})
61
+ options = @default_options.merge(options)
62
+ LetterMX::Subscriber.new(self.class.get("/contacts", options))
63
+ end
64
+
65
+ def count_subscribers(options={})
66
+ options = @default_options.merge(options)
67
+ LetterMX::Subscriber.new(self.class.get("/contacts/count", options))
68
+ end
69
+
70
+ def add_subscriber(subscriber={})
71
+ options = @default_options.clone
72
+ options.merge!(body: build_request_body({ contact: subscriber }))
73
+ LetterMX::Subscriber.new(self.class.post("/contacts", options))
74
+ end
75
+
76
+ def get_subscriber(id, options={})
77
+ options = @default_options.merge(options)
78
+ LetterMX::Subscriber.new(self.class.get("/contacts/#{id}", options))
79
+ end
80
+
81
+ def update_subscriber(id, subscriber={})
82
+ options = @default_options.clone
83
+ options.merge!(body: build_request_body({ contact: subscriber }))
84
+ LetterMX::Subscriber.new(self.class.put("/contacts/#{id}", options))
85
+ end
86
+
87
+ def remove_subscriber(id)
88
+ LetterMX::Subscriber.new(self.class.delete("/contacts/#{id}", @default_options))
89
+ end
90
+
91
+ def subscribers_api_limit_default(options={})
92
+ options = @default_options.merge(options)
93
+ LetterMX::Subscriber.new(self.class.get("/contacts/limit-default", options))
94
+ end
95
+
96
+ def subscribers_api_limit_max(options={})
97
+ options = @default_options.merge(options)
98
+ LetterMX::Subscriber.new(self.class.get("/contacts/limit-max", options))
99
+ end
100
+
101
+ private
102
+
103
+ def build_request_body(body)
104
+ if body
105
+ case @type
106
+ when LetterMX::API::CONTENT_TYPES[:json]
107
+ return body.to_json
108
+ # TODO: Implement XML
109
+ # when LetterMX::API::CONTENT_TYPES[:xml]
110
+ # return body.to_xml
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module LetterMX
3
+ class Subscriber
4
+
5
+ attr_reader :http_response
6
+ attr_reader :http_status
7
+ attr_reader :http_body
8
+ attr_reader :data
9
+
10
+ def initialize(response)
11
+ @http_response = response
12
+
13
+ if @http_response
14
+ @http_status = @http_response.headers["status"] if @http_response.headers
15
+ @http_body = @http_response.body
16
+ @data = @http_response.parsed_response
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module LetterMX
3
+ VERSION = "1.0.0"
4
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: letter-mx
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bjoern B. Dorra
9
+ - Eduard Siebert
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-11-15 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: activeresource
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '1.3'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ description: The Letter-MX-GEM is a lightweight GEM for accessing the Letter.MX subscriber
80
+ REST web services.
81
+ email:
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .ruby-version
87
+ - Gemfile
88
+ - Gemfile.lock
89
+ - LICENSE.txt
90
+ - README.md
91
+ - RELEASING
92
+ - Rakefile
93
+ - letter_mx.gemspec
94
+ - lib/letter_mx.rb
95
+ - lib/letter_mx/api.rb
96
+ - lib/letter_mx/subscriber.rb
97
+ - lib/letter_mx/version.rb
98
+ homepage: http://www.letter.mx
99
+ licenses:
100
+ - MIT
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.25
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: The Letter-MX-GEM is a lightweight GEM for accessing the Letter.MX subscriber
123
+ REST web services.
124
+ test_files: []