mailchimp_api 1.0.0.pre.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bb99723d02a7f76f16621524c27aab231833eb8b1f06f5d44377809a6d3dbccd
4
+ data.tar.gz: e0cd26d1801ba7a088e711cfccce24b9508667b6f657ed131e472c99b3a744c8
5
+ SHA512:
6
+ metadata.gz: 359bcaa56a36af4145627b393ead3442179e700c746f91191890f27a2c9d293b9cd758103b90e814d46a4d577a18f266a94cdb8bba482b0931f89de16d47f430
7
+ data.tar.gz: bdffffb268459ac74b5c5c22adb25cb20d17f0f12746fb9209684aff41063d57e79db5c61618e1cd49add201d4abeccfe5bbe3b9d039c0d9b7e670831cedee91
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # CHANGELOG
2
+
3
+ ## [1.0.0]
4
+
5
+ ### Added
6
+ - Support of `/`
7
+ - Support of `/lists`
8
+ - `.count` support
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Third Blink Software Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # MailchimpAPI
2
+ [![Build Status](https://travis-ci.org/rewindio/mailchimp_api.svg?branch=master)](https://travis-ci.org/rewindio/mailchimp_api)
3
+
4
+ The best way to consume Mailchimp's v3 APIs!
5
+
6
+ ## Installation
7
+
8
+ ###### Add this line to your application's Gemfile
9
+ ```ruby
10
+ gem 'mailchimp_api'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ###### Basic Account object
16
+ ```ruby
17
+ # frozen_string_literal: true
18
+
19
+ require 'mailchimp_api'
20
+
21
+ class Account
22
+ attr_accessor :access_token
23
+
24
+ def initialize(access_token)
25
+ @access_token = access_token
26
+ end
27
+
28
+ def with_mailchimp_session(&block)
29
+ MailchimpAPI::Session.temp access_token, &block
30
+ end
31
+ end
32
+
33
+ account = Account.new 'xxxyyyzzz-us7'
34
+ ```
35
+
36
+ ###### GET `/`
37
+ ```ruby
38
+ account.with_mailchimp_session { MailchimpAPI::AccountInformation.find '' }
39
+ ```
40
+
41
+ ###### GET `/lists`
42
+ ```ruby
43
+ account.with_mailchimp_session { MailchimpAPI::List.all }
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repository, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rewindio/mailchimp_api.
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mailchimp_api/connection'
4
+
5
+ module ActiveResource
6
+ class Connection
7
+ attr_reader :response
8
+
9
+ prepend MailchimpAPI::Connection::ResponseCapture
10
+ prepend MailchimpAPI::Connection::RequestNotification
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI::CollectionParsers
4
+ class Base < ActiveResource::Collection
5
+ attr_accessor :total_items, :links
6
+
7
+ def initialize(response = {})
8
+ @links = instantiate_links response.delete '_links'
9
+ @total_items = response.delete 'total_items'
10
+
11
+ @elements = response[element_key] || []
12
+ end
13
+
14
+ protected
15
+
16
+ def element_key
17
+ self.class.name.demodulize.downcase.pluralize
18
+ end
19
+
20
+ def instantiate_links(links)
21
+ links&.map do |link|
22
+ MailchimpAPI::Link.new link
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI::CollectionParsers
4
+ class Link < Base
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI::CollectionParsers
4
+ class List < Base
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mailchimp_api/collection_parsers/base'
4
+
5
+ Dir.glob("#{File.dirname(__FILE__)}/collection_parsers/*").each { |file| require file if file.end_with? '.rb' }
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ class Configuration
5
+ DEFAULT_API_REGION_IDENTIFIER = '__API_REGION_IDENTIFIER__'
6
+
7
+ attr_accessor :url
8
+
9
+ def initialize
10
+ # base url
11
+ @url = "https://#{DEFAULT_API_REGION_IDENTIFIER}.api.mailchimp.com/3.0/"
12
+ end
13
+ end
14
+
15
+ class << self
16
+ def configure
17
+ yield configuration
18
+
19
+ MailchimpAPI::Base.site = configuration.url
20
+
21
+ configuration
22
+ end
23
+
24
+ def configuration
25
+ @configuration ||= Configuration.new
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ class Connection < ActiveResource::Connection
5
+ attr_reader :response
6
+
7
+ module ResponseCapture
8
+ def handle_response(response)
9
+ @response = super
10
+ end
11
+ end
12
+
13
+ include ResponseCapture
14
+
15
+ module RequestNotification
16
+ def request(method, path, *arguments)
17
+ super.tap do |response|
18
+ notify_about_request response, arguments
19
+ end
20
+ rescue => e
21
+ notify_about_request e.response, arguments if e.respond_to? :response
22
+
23
+ raise
24
+ end
25
+
26
+ def notify_about_request(response, arguments)
27
+ ActiveSupport::Notifications.instrument('request.active_resource_detailed') do |payload|
28
+ payload[:response] = response
29
+ payload[:data] = arguments
30
+ end
31
+ end
32
+ end
33
+
34
+ include RequestNotification
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveResource
4
+ class DetailedLogSubscriber < ActiveSupport::LogSubscriber
5
+ def request(event)
6
+ data = event.payload[:data]
7
+ request_body = data.first
8
+
9
+ log_level_method = event.payload[:response].code.to_i < 400 ? :info : :error
10
+
11
+ send log_level_method, "Request: #{request_body}" if request_body
12
+ send log_level_method, "Response: #{event.payload[:response].body}" unless event.payload[:response].header['content-type'] == 'text/html'
13
+ end
14
+
15
+ def logger
16
+ ActiveResource::Base.logger
17
+ end
18
+ end
19
+ end
20
+
21
+ ActiveResource::DetailedLogSubscriber.attach_to :active_resource_detailed
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ class MissingParameter < StandardError
5
+ def initialize(msg = 'Missing Parameter(s)')
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ class JsonFormatter
5
+ include ActiveResource::Formats::JsonFormat
6
+
7
+ attr_reader :collection_name
8
+
9
+ def initialize(collection_name)
10
+ @collection_name = collection_name.to_s
11
+ end
12
+
13
+ def decode(json)
14
+ ActiveSupport::JSON.decode json.presence
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ class AccountInformation < Base
5
+ self.collection_name = ''
6
+ self.element_name = ''
7
+
8
+ class << self
9
+ def find(*arguments)
10
+ scope = arguments.slice!(0)
11
+ options = arguments.slice!(0) || {}
12
+
13
+ prefix_options, query_options = split_options options[:params]
14
+
15
+ path = element_path scope, prefix_options, query_options
16
+
17
+ instantiate_record format.decode(connection.get(path, headers).body), prefix_options
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ class Base < ActiveResource::Base
5
+ headers['User-Agent'] = "MailchimpAPI/#{MailchimpAPI::VERSION}"
6
+ headers['Accept'] = 'application/json'
7
+ headers['Authorization'] = 'OAuth '
8
+
9
+ self.site = MailchimpAPI.configuration.url
10
+
11
+ self.include_format_in_path = false
12
+
13
+ self.format = MailchimpAPI::JsonFormatter.new :result
14
+
15
+ has_many :_links, class_name: 'MailchimpAPI::Link'
16
+
17
+ class << self
18
+ def activate_session(session)
19
+ self.headers['Authorization'] = "OAuth #{session.oauth_token}" # rubocop:disable Style/RedundantSelf
20
+
21
+ MailchimpAPI::Base.site = MailchimpAPI.configuration.url.sub MailchimpAPI::Configuration::DEFAULT_API_REGION_IDENTIFIER, session.api_region_identifier
22
+ end
23
+
24
+ def reset_session
25
+ self.headers['Authorization'] = 'OAuth ' # rubocop:disable Style/RedundantSelf
26
+
27
+ MailchimpAPI::Base.site = MailchimpAPI.configuration.url
28
+ end
29
+
30
+ def headers
31
+ if _headers_defined?
32
+ _headers
33
+ elsif superclass != Object && superclass.headers
34
+ superclass.headers
35
+ else
36
+ _headers || {}
37
+ end
38
+ end
39
+ end
40
+
41
+ def to_h
42
+ JSON.parse(attributes.to_json).symbolize_keys
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ class Link < Base
5
+ self.collection_parser = CollectionParsers::Link
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ class List < Base
5
+ extend MailchimpAPI::Support::Countable
6
+
7
+ self.collection_parser = CollectionParsers::List
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI::Support
4
+ module Countable
5
+ def count
6
+ all(params: { fields: 'total_items', count: 0 }).total_items
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mailchimp_api/resources/base'
4
+
5
+ Dir.glob("#{File.dirname(__FILE__)}/resources/support/*").each { |file| require file if file.end_with? '.rb' }
6
+ Dir.glob("#{File.dirname(__FILE__)}/resources/*").each { |file| require file if file.end_with? '.rb' }
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ class Session
5
+ attr_accessor :oauth_token, :api_region_identifier
6
+
7
+ def initialize(oauth_token)
8
+ self.oauth_token = oauth_token
9
+ self.api_region_identifier = oauth_token&.split('-')&.last || MailchimpAPI::Configuration::DEFAULT_API_REGION_IDENTIFIER
10
+ end
11
+
12
+ class << self
13
+ def temp(oauth_token)
14
+ session = new oauth_token
15
+
16
+ MailchimpAPI::Base.activate_session session
17
+
18
+ yield
19
+ ensure
20
+ MailchimpAPI::Base.reset_session
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailchimpAPI
4
+ VERSION = '1.0.0.pre.0'
5
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.dirname(__FILE__)
4
+
5
+ # gem requires
6
+ require 'activeresource'
7
+
8
+ # gem extensions
9
+ require 'active_resource/connection_ext'
10
+
11
+ require 'mailchimp_api/version'
12
+ require 'mailchimp_api/configuration'
13
+
14
+ module MailchimpAPI
15
+ end
16
+
17
+ require 'mailchimp_api/json_formatter'
18
+ require 'mailchimp_api/connection'
19
+ require 'mailchimp_api/detailed_log_subscriber'
20
+ require 'mailchimp_api/exceptions'
21
+ require 'mailchimp_api/session'
22
+
23
+ require 'mailchimp_api/collection_parsers'
24
+ require 'mailchimp_api/resources'
metadata ADDED
@@ -0,0 +1,219 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailchimp_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre.0
5
+ platform: ruby
6
+ authors:
7
+ - rewind.io
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeresource
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 12.3.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 12.3.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: http_logger
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.6.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.6.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: factory_bot
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.11.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 4.11.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.15.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.15.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-minitest
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.4.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.4.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: minitest
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 5.11.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 5.11.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: minitest-reporters
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 1.3.6
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 1.3.6
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 3.5.1
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 3.5.1
167
+ description: Consume Mailchimp's API using ActiveResource
168
+ email:
169
+ - team@rewind.io
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - CHANGELOG.md
175
+ - LICENSE
176
+ - README.md
177
+ - lib/active_resource/connection_ext.rb
178
+ - lib/mailchimp_api.rb
179
+ - lib/mailchimp_api/collection_parsers.rb
180
+ - lib/mailchimp_api/collection_parsers/base.rb
181
+ - lib/mailchimp_api/collection_parsers/link.rb
182
+ - lib/mailchimp_api/collection_parsers/list.rb
183
+ - lib/mailchimp_api/configuration.rb
184
+ - lib/mailchimp_api/connection.rb
185
+ - lib/mailchimp_api/detailed_log_subscriber.rb
186
+ - lib/mailchimp_api/exceptions.rb
187
+ - lib/mailchimp_api/json_formatter.rb
188
+ - lib/mailchimp_api/resources.rb
189
+ - lib/mailchimp_api/resources/account_information.rb
190
+ - lib/mailchimp_api/resources/base.rb
191
+ - lib/mailchimp_api/resources/link.rb
192
+ - lib/mailchimp_api/resources/list.rb
193
+ - lib/mailchimp_api/resources/support/countable.rb
194
+ - lib/mailchimp_api/session.rb
195
+ - lib/mailchimp_api/version.rb
196
+ homepage: https://github.com/rewindio/mailchimp_api.git
197
+ licenses:
198
+ - MIT
199
+ metadata: {}
200
+ post_install_message:
201
+ rdoc_options: []
202
+ require_paths:
203
+ - lib
204
+ required_ruby_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ required_rubygems_version: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">"
212
+ - !ruby/object:Gem::Version
213
+ version: 1.3.1
214
+ requirements: []
215
+ rubygems_version: 3.0.2
216
+ signing_key:
217
+ specification_version: 4
218
+ summary: Consume Mailchimp's API using ActiveResource
219
+ test_files: []