active_campaign 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84a934cf5dafa4193d0f9bfe8669c5f4de3353c3
4
- data.tar.gz: b2187ee064e4afc645073252f3d21eab2366177e
3
+ metadata.gz: 51738f35b3fdb61fa2e068ddcca2735c3e1a24ca
4
+ data.tar.gz: 335f4331bdb56d952ea512b73c95b9ca83de8524
5
5
  SHA512:
6
- metadata.gz: 434fef3c9cd60d40e7e21a34fc3f06717e901ea009c0f6ad3c747ed310efac78e567c13756a0823b0987dc48aa4d023f8ebfb7d490e4d87a7841e548192da123
7
- data.tar.gz: 3881c3aad507b4421a4d37cfabb7a980d624387de80fe0bc3194b2e5743076e690df2ae38455b9c62badef033297fe709967fc85e6ac4274c71b39cab12257bd
6
+ metadata.gz: 672b2039bb992045ac56ba02113b9eeb17dcc534a0dc7a88af8325e660b2b62816b6939945955e3557ec98461c40c2ef58269b9bc06c7d10ee0c9bfff51b6544
7
+ data.tar.gz: dba4079c479ee6bb751754421b5e07eed513f2b2e0791b3c2216444bea323050ba2657eeae8fe4fb3ac03655d8dc441ddfa22f8d1c488fbf92bb8ce8c1f7e6f7
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ repo_token: 9QHPyeb961SRsyxDttIHmzVNhXvP1RJKr
data/Gemfile CHANGED
@@ -2,28 +2,23 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem 'jruby-openssl', :platforms => :jruby
4
4
  gem 'rake'
5
+ gem 'activesupport'
5
6
 
6
7
  group :development do
7
8
  gem 'awesome_print', :require => 'ap'
8
- gem 'guard-rspec', '~> 2.5.1'
9
- gem 'hirb-unicode'
10
9
  gem 'pry'
11
- gem 'redcarpet'
12
- gem 'wirb'
13
- gem 'wirble'
14
- gem 'yard'
15
10
  end
16
11
 
17
12
  group :test do
13
+ gem "codeclimate-test-reporter", :require => false
18
14
  gem 'coveralls', :require => false
19
15
  gem 'json', '~> 1.7', :platforms => [:ruby_18, :jruby]
20
- gem 'netrc', '~> 0.7.7'
21
16
  gem 'rb-fsevent', '~> 0.9'
22
- gem 'rspec', '~> 2.13.0'
17
+ gem 'rspec', '~> 2.14.0'
23
18
  gem 'simplecov', :require => false
24
19
  gem 'test-queue', '~> 0.1.3'
25
- gem 'vcr', '~> 2.4.0'
26
- gem 'webmock', '~> 1.9.0'
20
+ gem 'vcr', '~> 2.6.0'
21
+ gem 'webmock', '~> 1.14.0'
27
22
  end
28
23
 
29
24
  gemspec
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Circle CI](https://circleci.com/gh/RushPlay/active_campaign.png?circle-token=908def2d4311fcbe28d2caabccaa703e2835cf37)](https://circleci.com/gh/RushPlay/active_campaign/tree/master)[![Coverage Status](https://coveralls.io/repos/RushPlay/active_campaign/badge.png?branch=master)](https://coveralls.io/r/RushPlay/active_campaign?branch=master) [![Code Climate](https://codeclimate.com/repos/525d012ec7f3a335f101a3d6/badges/74d14b105bf9f769f10f/gpa.png)](https://codeclimate.com/repos/525d012ec7f3a335f101a3d6/feed)
1
2
  # Active::Campaign::Ruby
2
3
 
3
4
  A simple wrapper for the ActiveCampaign API. Since their API seems to be
@@ -18,10 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "faraday", ">= 0.8.8"
22
- spec.add_dependency "activesupport", ">= 4.0.0"
23
- spec.add_dependency "uri_template"
21
+ spec.add_dependency "httpi"
24
22
  spec.add_dependency "hashie"
25
- spec.add_dependency "oj"
26
- spec.add_development_dependency 'bundler', '~> 1.0'
27
23
  end
@@ -1,3 +1,4 @@
1
+ require 'active_support/core_ext'
1
2
  module ActiveCampaign
2
3
  class Client
3
4
  module Contacts
@@ -1,30 +1,28 @@
1
- require 'faraday'
2
- require 'uri_template'
3
- require 'active_campaign/arguments'
4
- require 'active_campaign/authentication'
5
1
  require 'active_campaign/configurable'
6
2
  require 'active_campaign/client/contacts'
7
3
  require 'active_campaign/client/lists'
8
4
  require 'active_campaign/client/campaigns'
5
+ require 'httpi'
6
+ require 'hashie'
9
7
 
10
8
  module ActiveCampaign
11
9
  class Client
12
10
  include ActiveCampaign::Configurable
13
- include ActiveCampaign::Authentication
14
11
  include ActiveCampaign::Client::Contacts
15
12
  include ActiveCampaign::Client::Lists
16
13
  include ActiveCampaign::Client::Campaigns
17
14
 
18
- NO_BODY = Set.new([:get, :head])
19
- # Header keys that can be passed in options hash to {#get},{#head}
20
- CONVENIENCE_HEADERS = Set.new [:accept]
21
-
22
15
  def initialize(options = {})
23
- # Use options passed in, but fall back to module defaults
24
16
  ActiveCampaign::Configurable.keys.each do |key|
25
17
  instance_variable_set(:"@#{key}", options[key] ||
26
18
  ActiveCampaign.instance_variable_get(:"@#{key}"))
27
19
  end
20
+
21
+ unless debug
22
+ HTTPI.log = false
23
+ HTTPI.log_level = :info
24
+ HTTPI.logger = Rails.logger
25
+ end
28
26
  end
29
27
 
30
28
  # Compares client options to a Hash of requested options
@@ -35,32 +33,13 @@ module ActiveCampaign
35
33
  opts.hash == options.hash
36
34
  end
37
35
 
38
- # Text representation of the client, masking tokens and passwords
39
- #
40
- # @return [String]
41
- def inspect
42
- inspected = super
43
-
44
- # mask password
45
- inspected = inspected.gsub! @password, "*******" if @password
46
- # Only show last 4 of token, secret
47
- if @access_token
48
- inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}"
49
- end
50
- if @client_secret
51
- inspected = inspected.gsub! @client_secret, "#{'*'*36}#{@client_secret[36..-1]}"
52
- end
53
-
54
- inspected
55
- end
56
-
57
36
  # Make a HTTP GET request
58
37
  #
59
38
  # @param url [String] The path, relative to {#api_endpoint}
60
39
  # @param options [Hash] Query and header params for request
61
40
  # @return [Sawyer::Resource]
62
41
  def get(api_method, options = {})
63
- request :get, api_method, parse_query_and_convenience_headers(options)
42
+ request :get, api_method, options
64
43
  end
65
44
 
66
45
  # Make a HTTP POST request
@@ -72,143 +51,59 @@ module ActiveCampaign
72
51
  request :post, api_method, options
73
52
  end
74
53
 
75
- # Make a HTTP PUT request
76
- #
77
- # @param url [String] The path, relative to {#api_endpoint}
78
- # @param options [Hash] Body and header params for request
79
- # @return [Sawyer::Resource]
80
- def put(api_method, options = {})
81
- request :put, api_method, options
82
- end
83
-
84
- # Make a HTTP PATCH request
85
- #
86
- # @param url [String] The path, relative to {#api_endpoint}
87
- # @param options [Hash] Body and header params for request
88
- # @return [Sawyer::Resource]
89
- def patch(api_method, options = {})
90
- request :patch, api_method, options
91
- end
92
-
93
- # Make a HTTP DELETE request
94
- #
95
- # @param url [String] The path, relative to {#api_endpoint}
96
- # @param options [Hash] Query and header params for request
97
- # @return [Sawyer::Resource]
98
- def delete(api_method, options = {})
99
- request :delete, api_method, options
100
- end
101
-
102
- # Make a HTTP HEAD request
103
- #
104
- # @param url [String] The path, relative to {#api_endpoint}
105
- # @param options [Hash] Query and header params for request
106
- # @return [Sawyer::Resource]
107
- def head(api_method, options = {})
108
- request :head, api_method, parse_query_and_convenience_headers(options)
109
- end
110
-
111
- # Response for last HTTP request
112
- #
113
- # @return [Sawyer::Response]
114
- def last_response
115
- @last_response
116
- end
117
-
118
54
  private
119
55
 
120
56
  def request(method, api_method, data)
121
- options = {}
122
- options[:query] = data.delete(:query) || {}
123
- options[:headers] = data.delete(:headers) || {}
124
-
125
- options[:query].merge!({
126
- api_key: api_key,
127
- api_action: api_method.to_s,
128
- api_output: api_output
129
- })
130
-
131
- if accept = data.delete(:accept)
132
- options[:headers][:accept] = accept
133
- end
134
-
135
- @last_response = response = process_request(
136
- method, URI.encode(api_path), data, options
137
- )
138
-
139
- response
57
+ req = create_request method, api_method, data
58
+ response = HTTPI.send(method, req)
59
+ response = JSON.parse(response.body)
60
+ normalize(response)
140
61
  end
141
62
 
142
- def connection
143
- @conn ||= Faraday.new do |builder|
144
-
145
- builder.request :url_encoded
146
63
 
147
- builder.response :mashify
148
- builder.response :json_normalizer
149
- builder.response :parse_json #, content_type: /\bjson$/
64
+ def create_request(method, api_method, options = {})
65
+ request = HTTPI::Request.new :url => File.join(api_endpoint, api_path)
66
+ request.headers = {"User-Agent" => user_agent}
67
+ request.query = {
68
+ :api_key => api_key,
69
+ :api_action => api_method.to_s,
70
+ :api_output => api_output
71
+ }.merge(options.delete(:query))
150
72
 
151
- if debug
152
- builder.use :debugger
153
- builder.use :instrumentation
154
- end
73
+ request.body = options.to_query if method == :post
155
74
 
156
- builder.adapter Faraday.default_adapter
157
- builder.url_prefix = api_endpoint
158
- end
159
- # @conn.url_prefix api_endpoint
160
- @conn
75
+ request
161
76
  end
162
77
 
78
+ def normalize(response)
79
+ keys, values = keys_values(response)
163
80
 
164
- def process_request(method, url, data = {}, options = {})
165
- if NO_BODY.include?(method)
166
- options ||= data
167
- data = nil
81
+ if keys.all?{|key| is_numeric?(key) }
82
+ response[:results] = values
83
+ keys.each do |key|
84
+ response.delete(key)
85
+ end
168
86
  end
169
- options ||= {}
170
- url = expand_url(url, options[:uri])
171
87
 
172
- res = connection.send method, url do |req|
173
- req.body = data if data
174
- if params = options[:query]
175
- req.params.update params
176
- end
177
- if headers = options[:headers]
178
- req.headers.update headers
179
- end
88
+ if mash
89
+ Hashie::Mash.new response
90
+ else
91
+ ActiveSupport::HashWithIndifferentAccess.new(response)
180
92
  end
181
- res.body
182
- end
183
93
 
184
- # Executes the request, checking if it was successful
185
- #
186
- # @return [Boolean] True on success, false otherwise
187
- def boolean_from_response(method, path, options = {})
188
- request(method, path, options)
189
- @last_response.status == 204
190
- rescue ActiveCampaign::NotFound
191
- false
192
94
  end
193
95
 
194
- def parse_query_and_convenience_headers(options)
195
- headers = options.fetch(:headers, {})
196
- CONVENIENCE_HEADERS.each do |h|
197
- if header = options.delete(h)
198
- headers[h] = header
199
- end
200
- end
201
- query = options.delete(:query)
202
- opts = {:query => options}
203
- opts[:query].merge!(query) if query && query.is_a?(Hash)
204
- opts[:headers] = headers unless headers.empty?
96
+ def is_numeric?(string)
97
+ string.to_s.match(/\A[+-]?\d+\Z/) == nil ? false : true
98
+ end
205
99
 
206
- opts
100
+ def keys_values(response)
101
+ results = results(response)
102
+ [results.keys, results.values]
207
103
  end
208
- def expand_url(url, options = nil)
209
- tpl = url.respond_to?(:expand) ? url : URITemplate.new(url.to_s)
210
- expand = tpl.method(:expand)
211
- options ? expand.call(options) : expand.call
104
+
105
+ def results(response)
106
+ response.reject{|k,v| %w(result_code result_message result_output).include?(k) }
212
107
  end
213
108
  end
214
109
  end
@@ -4,49 +4,24 @@ module ActiveCampaign
4
4
  # in {Default}
5
5
  module Configurable
6
6
 
7
- # @!attribute connection_options
8
- # @see https://github.com/lostisland/faraday
9
- # @return [Hash] Configure connection options for Faraday
10
- # @!attribute middleware
11
- # @see https://github.com/lostisland/faraday
12
- # @return [Faraday::Builder] Configure middleware for Faraday
13
- # @!attribute per_page
14
- # @return [String] Configure page size for paginated results. API default: 30
15
- # @!attribute user_agent
16
- # @return [String] Configure User-Agent header for requests.
17
- # @!attribute web_endpoint
18
- # @return [String] Base URL for web URLs. default: https://github.com/
19
-
20
- attr_accessor :api_url, :api_key, :api_output, :api_path, :api_action,
21
- :list_id, :api_endpoint, :proxy, :user_agent,
22
- :debug, :per_page, :connection_options,
23
- :api_endpoint, :auto_paginate,
24
- :default_media_type, :connection_options,
25
- :middleware, :user_agent, :web_endpoint
26
-
27
- attr_writer :client_secret, :password
7
+ CONFIG_KEYS = [
8
+ :api_key,
9
+ :api_path,
10
+ :api_output,
11
+ :api_endpoint,
12
+ :user_agent,
13
+ :debug,
14
+ :mash,
15
+ ]
16
+
17
+ attr_accessor(*CONFIG_KEYS)
28
18
 
29
19
  class << self
30
20
 
31
21
  # List of configurable keys for {ActiveCampaign::Client}
32
22
  # @return [Array] of option keys
33
23
  def keys
34
- @keys ||= [
35
- :api_key,
36
- :api_path,
37
- :api_output,
38
- :api_endpoint,
39
- :user_agent,
40
- :debug,
41
- :per_page,
42
- :connection_options,
43
- :auto_paginate,
44
- :default_media_type,
45
- :connection_options,
46
- :middleware,
47
- :user_agent,
48
- :web_endpoint
49
- ]
24
+ @keys ||= CONFIG_KEYS
50
25
  end
51
26
  end
52
27
 
@@ -1,10 +1,4 @@
1
1
  require 'active_campaign/version'
2
- require 'active_campaign/response/raise_error'
3
- require 'active_campaign/response/debugger'
4
- require 'active_campaign/response/json_normalizer'
5
- require 'active_campaign/response/mashify'
6
- require 'active_campaign/response/instrumentation'
7
- require 'active_campaign/response/parse_json'
8
2
 
9
3
  module ActiveCampaign
10
4
 
@@ -19,26 +13,12 @@ module ActiveCampaign
19
13
  # Default User Agent header string
20
14
  USER_AGENT = "ActiveCampaign Ruby Gem #{ActiveCampaign::VERSION}".freeze
21
15
 
22
- # Default media type
23
- MEDIA_TYPE = "text/html"
24
-
25
16
  # Default media type
26
17
  API_OUTPUT = "json"
27
18
 
28
19
  # Default WEB endpoint
29
20
  WEB_ENDPOINT = "http://www.activecampaign.com/".freeze
30
21
 
31
- # Default Faraday middleware stack
32
- MIDDLEWARE = Faraday::Builder.new do |builder|
33
- builder.request :url_encoded
34
- builder.response :mashify
35
- builder.response :json_normalizer
36
- builder.response :parse_json
37
- builder.use :debugger
38
- builder.use :instrumentation
39
- builder.adapter Faraday.default_adapter
40
- end
41
-
42
22
  class << self
43
23
 
44
24
  # Configuration options
@@ -59,29 +39,6 @@ module ActiveCampaign
59
39
  API_PATH
60
40
  end
61
41
 
62
- # Default pagination preference from ENV
63
- # @return [String]
64
- def auto_paginate
65
- ENV['ACTIVE_CAMPAIGN_AUTO_PAGINATE']
66
- end
67
-
68
- # Default options for Faraday::Connection
69
- # @return [Hash]
70
- def connection_options
71
- {
72
- :headers => {
73
- :accept => default_media_type,
74
- :user_agent => user_agent
75
- }
76
- }
77
- end
78
-
79
- # Default media type from ENV or {MEDIA_TYPE}
80
- # @return [String]
81
- def default_media_type
82
- ENV['ACTIVE_CAMPAIGN_DEFAULT_MEDIA_TYPE'] || MEDIA_TYPE
83
- end
84
-
85
42
  # Default GitHub username for Basic Auth from ENV
86
43
  # @return [String]
87
44
  def api_key
@@ -92,11 +49,8 @@ module ActiveCampaign
92
49
  false
93
50
  end
94
51
 
95
- # Default middleware stack for Faraday::Connection
96
- # from {MIDDLEWARE}
97
- # @return [String]
98
- def middleware
99
- MIDDLEWARE
52
+ def mash
53
+ false
100
54
  end
101
55
 
102
56
  # Default media api_output from ENV or {API_OUTPUT}
@@ -105,31 +59,11 @@ module ActiveCampaign
105
59
  ENV['ACTIVE_CAMPAIGN_API_OUTPUT'] || API_OUTPUT
106
60
  end
107
61
 
108
- # Default pagination page size from ENV
109
- # @return [Fixnum] Page size
110
- def per_page
111
- page_size = ENV['ACTIVE_CAMPAIGN_PER_PAGE']
112
-
113
- page_size.to_i if page_size
114
- end
115
-
116
- # Default proxy server URI for Faraday connection from ENV
117
- # @return [String]
118
- def proxy
119
- ENV['ACTIVE_CAMPAIGN_PROXY']
120
- end
121
-
122
62
  # Default User-Agent header string from ENV or {USER_AGENT}
123
63
  # @return [String]
124
64
  def user_agent
125
65
  ENV['ACTIVE_CAMPAIGN_USER_AGENT'] || USER_AGENT
126
66
  end
127
-
128
- # Default web endpoint from ENV or {WEB_ENDPOINT}
129
- # @return [String]
130
- def web_endpoint
131
- ENV['ACTIVE_CAMPAIGN_WEB_ENDPOINT'] || WEB_ENDPOINT
132
- end
133
67
  end
134
68
  end
135
69
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveCampaign
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -1,10 +1,11 @@
1
- require 'active_support/core_ext'
1
+ # require 'active_support/core_ext'
2
2
  require 'active_campaign/client'
3
3
  require 'active_campaign/default'
4
4
 
5
5
  module ActiveCampaign
6
6
 
7
7
  class << self
8
+
8
9
  include ActiveCampaign::Configurable
9
10
 
10
11
  # API client based on configured options {Configurable}
@@ -1,32 +1,47 @@
1
- require 'helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe ActiveCampaign do
4
+ before do
5
+ ActiveCampaign.reset!
6
+ end
4
7
 
5
8
  after do
6
- ActiveCampaign.setup
9
+ ActiveCampaign.reset!
7
10
  end
8
11
 
9
- describe ".respond_to?" do
10
- it "is true if method exists" do
11
- expect(ActiveCampaign.respond_to?(:client, true)).to eq(true)
12
+ it 'sets defaults' do
13
+ ActiveCampaign::Configurable.keys.each do |key|
14
+ expect(ActiveCampaign.instance_variable_get(:"@#{key}")).to eq ActiveCampaign::Default.send(key)
12
15
  end
13
16
  end
14
17
 
15
- describe ".new" do
16
- it "is a ActiveCampaign::Client" do
17
- expect(ActiveCampaign.client).to be_a ActiveCampaign::Client
18
+ describe '.client' do
19
+ it 'creates an ActiveCampaign::Client' do
20
+ expect(ActiveCampaign.client).to be_kind_of ActiveCampaign::Client
18
21
  end
19
- end
20
22
 
21
- describe ".delegate" do
22
- it "delegates missing methods to ActiveCampaign::Client" do
23
- initialize_active_campaign
23
+ it 'caches the client when the same options are passed' do
24
+ expect(ActiveCampaign.client).to eq ActiveCampaign.client
25
+ end
24
26
 
25
- stub_get("contact_view", email: "mikael@zoolutions.se").
26
- to_return json_response('contact_view.json')
27
- contact = ActiveCampaign.contact_view(email: "mikael@zoolutions.se")
28
- expect(contact.email).to eq('mikael@zoolutions.se')
27
+ it 'returns a fresh client when options are not the same' do
28
+ client = ActiveCampaign.client
29
+ ActiveCampaign.api_key = 'somekey'
30
+ client_two = ActiveCampaign.client
31
+ client_three = ActiveCampaign.client
32
+ expect(client).to_not eq client_two
33
+ expect(client_three).to eq client_two
29
34
  end
35
+ end
30
36
 
37
+ describe '.configure' do
38
+ ActiveCampaign::Configurable.keys.each do |key|
39
+ it "sets the #{key.to_s.gsub('_', ' ')}" do
40
+ ActiveCampaign.configure do |config|
41
+ config.send("#{key}=", key)
42
+ end
43
+ expect(ActiveCampaign.instance_variable_get(:"@#{key}")).to eq key
44
+ end
45
+ end
31
46
  end
32
47
  end
@@ -1,14 +1,11 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require 'helper'
2
+ require 'spec_helper'
3
3
 
4
- # need this to filter the data
5
- require 'json'
6
- require 'date'
7
4
  describe ActiveCampaign::Client::Contacts do
8
5
 
9
6
  initialize_new_client
10
7
 
11
- it "syncs a contact" do
8
+ pending "syncs a contact" do
12
9
  params = {
13
10
  id: 1,
14
11
  email: 'mhenrixon@me.com',
@@ -1,9 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require 'helper'
2
+ require 'spec_helper'
3
3
 
4
- # need this to filter the data
5
- require 'json'
6
- require 'date'
7
4
  describe ActiveCampaign::Client::Lists do
8
5
 
9
6
  initialize_new_client
data/spec/client_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'helper'
1
+ require 'spec_helper'
2
2
 
3
3
  module ActiveCampaign
4
4
  describe Client do
@@ -1,17 +1,21 @@
1
- # require 'simplecov'
2
- # require 'coveralls'
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+ require "codeclimate-test-reporter"
4
+ formatters = [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter]
3
5
 
4
- # SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
- # SimpleCov::Formatter::HTMLFormatter,
6
- # Coveralls::SimpleCov::Formatter
7
- # ]
8
- # SimpleCov.start
6
+ if ENV['CODECLIMATE_REPO_TOKEN']
7
+ formatters << CodeClimate::TestReporter::Formatter
8
+ CodeClimate::TestReporter.start
9
+ end
10
+
11
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[*formatters]
12
+ SimpleCov.start
9
13
 
10
14
  require "active_campaign"
11
15
  require 'rspec'
12
16
  require 'webmock/rspec'
13
17
 
14
- WebMock.disable_net_connect!(:allow => 'coveralls.io')
18
+ WebMock.disable_net_connect!(:allow => ['coveralls.io', 'codeclimate.com'])
15
19
 
16
20
  RSpec.configure do |config|
17
21
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -23,6 +27,25 @@ RSpec.configure do |config|
23
27
  end
24
28
  end
25
29
 
30
+ require 'vcr'
31
+ VCR.configure do |c|
32
+ c.configure_rspec_metadata!
33
+ c.filter_sensitive_data('<API_KEY>') do
34
+ ENV['ACTIVE_CAMPAIGN_API_KEY']
35
+ end
36
+
37
+ c.default_cassette_options = {
38
+ serialize_with: :json,
39
+ # TODO: Track down UTF-8 issue and remove
40
+ preserve_exact_body_bytes: true,
41
+ decode_compressed_response: true,
42
+ record: ENV['TRAVIS'] ? :none : :once
43
+ }
44
+ c.cassette_library_dir = 'spec/cassettes'
45
+ c.hook_into :webmock
46
+ c.ignore_hosts 'codeclimate.com'
47
+ end
48
+
26
49
  def a_delete(url, options = {})
27
50
  a_request(:delete, active_campaign_url(url, options))
28
51
  end
@@ -97,7 +120,7 @@ end
97
120
  def initialize_new_client
98
121
  before do
99
122
  initialize_active_campaign
100
- @client = ActiveCampaign::Client.new
123
+ @client = ActiveCampaign::Client.new mash: true, debug: true
101
124
  end
102
125
  end
103
126
 
@@ -107,5 +130,6 @@ def initialize_active_campaign
107
130
  config.api_key = "YOURAPIKEY"
108
131
  config.api_output = "json"
109
132
  config.debug = true
133
+ config.mash = true
110
134
  end
111
135
  end
metadata CHANGED
@@ -1,99 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_campaign
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-24 00:00:00.000000000 Z
11
+ date: 2013-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: faraday
14
+ name: httpi
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: 0.8.8
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '>='
25
- - !ruby/object:Gem::Version
26
- version: 0.8.8
27
- - !ruby/object:Gem::Dependency
28
- name: activesupport
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - '>='
32
- - !ruby/object:Gem::Version
33
- version: 4.0.0
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - '>='
39
- - !ruby/object:Gem::Version
40
- version: 4.0.0
41
- - !ruby/object:Gem::Dependency
42
- name: uri_template
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - '>='
17
+ - - ">="
46
18
  - !ruby/object:Gem::Version
47
19
  version: '0'
48
20
  type: :runtime
49
21
  prerelease: false
50
22
  version_requirements: !ruby/object:Gem::Requirement
51
23
  requirements:
52
- - - '>='
24
+ - - ">="
53
25
  - !ruby/object:Gem::Version
54
26
  version: '0'
55
27
  - !ruby/object:Gem::Dependency
56
28
  name: hashie
57
29
  requirement: !ruby/object:Gem::Requirement
58
30
  requirements:
59
- - - '>='
31
+ - - ">="
60
32
  - !ruby/object:Gem::Version
61
33
  version: '0'
62
34
  type: :runtime
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
- - - '>='
38
+ - - ">="
67
39
  - !ruby/object:Gem::Version
68
40
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: oj
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - '>='
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - '>='
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: bundler
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ~>
88
- - !ruby/object:Gem::Version
89
- version: '1.0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ~>
95
- - !ruby/object:Gem::Version
96
- version: '1.0'
97
41
  description: A simple ruby wrapper for the ActiveCampaign API
98
42
  email:
99
43
  - mikael@zoolutions.se
@@ -101,10 +45,11 @@ executables: []
101
45
  extensions: []
102
46
  extra_rdoc_files: []
103
47
  files:
104
- - .gitignore
105
- - .rspec
106
- - .ruby-version
107
- - .travis.yml
48
+ - ".coveralls.yml"
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".ruby-version"
52
+ - ".travis.yml"
108
53
  - Gemfile
109
54
  - LICENSE.txt
110
55
  - README.md
@@ -112,8 +57,6 @@ files:
112
57
  - active_campaign.gemspec
113
58
  - lib/active_campaign.rb
114
59
  - lib/active_campaign/.DS_Store
115
- - lib/active_campaign/arguments.rb
116
- - lib/active_campaign/authentication.rb
117
60
  - lib/active_campaign/client.rb
118
61
  - lib/active_campaign/client/campaigns.rb
119
62
  - lib/active_campaign/client/contacts.rb
@@ -122,12 +65,6 @@ files:
122
65
  - lib/active_campaign/configurable.rb
123
66
  - lib/active_campaign/default.rb
124
67
  - lib/active_campaign/error.rb
125
- - lib/active_campaign/response/debugger.rb
126
- - lib/active_campaign/response/instrumentation.rb
127
- - lib/active_campaign/response/json_normalizer.rb
128
- - lib/active_campaign/response/mashify.rb
129
- - lib/active_campaign/response/parse_json.rb
130
- - lib/active_campaign/response/raise_error.rb
131
68
  - lib/active_campaign/version.rb
132
69
  - spec/active_campaign_spec.rb
133
70
  - spec/client/contacts_spec.rb
@@ -136,7 +73,7 @@ files:
136
73
  - spec/fixtures/contact_sync.json
137
74
  - spec/fixtures/contact_view.json
138
75
  - spec/fixtures/list_view.json
139
- - spec/helper.rb
76
+ - spec/spec_helper.rb
140
77
  homepage: ''
141
78
  licenses:
142
79
  - MIT
@@ -147,17 +84,17 @@ require_paths:
147
84
  - lib
148
85
  required_ruby_version: !ruby/object:Gem::Requirement
149
86
  requirements:
150
- - - '>='
87
+ - - ">="
151
88
  - !ruby/object:Gem::Version
152
89
  version: '0'
153
90
  required_rubygems_version: !ruby/object:Gem::Requirement
154
91
  requirements:
155
- - - '>='
92
+ - - ">="
156
93
  - !ruby/object:Gem::Version
157
94
  version: '0'
158
95
  requirements: []
159
96
  rubyforge_project:
160
- rubygems_version: 2.0.7
97
+ rubygems_version: 2.1.3
161
98
  signing_key:
162
99
  specification_version: 4
163
100
  summary: See http://www.activecampaign.com/api/overview.php for more information
@@ -169,5 +106,5 @@ test_files:
169
106
  - spec/fixtures/contact_sync.json
170
107
  - spec/fixtures/contact_view.json
171
108
  - spec/fixtures/list_view.json
172
- - spec/helper.rb
109
+ - spec/spec_helper.rb
173
110
  has_rdoc:
@@ -1,14 +0,0 @@
1
- module ActiveCampaign
2
-
3
- # Extracts options from method arguments
4
- # @private
5
- class Arguments < Array
6
- attr_reader :options
7
-
8
- def initialize(args)
9
- @options = args.last.is_a?(::Hash) ? args.pop : {}
10
- super(args)
11
- end
12
-
13
- end
14
- end
@@ -1,68 +0,0 @@
1
- module ActiveCampaign
2
-
3
- # Authentication methods for {ActiveCampaign::Client}
4
- module Authentication
5
-
6
- # Indicates if the client was supplied Basic Auth
7
- # username and password
8
- #
9
- # @see http://developer.github.com/v3/#authentication
10
- # @return [Boolean]
11
- def basic_authenticated?
12
- @login && @password
13
- end
14
-
15
- # Indicates if the client was supplied an OAuth
16
- # access token
17
- #
18
- # @see http://developer.github.com/v3/#authentication
19
- # @return [Boolean]
20
- def token_authenticated?
21
- !!@access_token
22
- end
23
-
24
- # Indicates if the client was supplied an OAuth
25
- # access token or Basic Auth username and password
26
- #
27
- # @see http://developer.github.com/v3/#authentication
28
- # @return [Boolean]
29
- def user_authenticated?
30
- basic_authenticated? || token_authenticated?
31
- end
32
-
33
- # Indicates if the client has OAuth Application
34
- # client_id and secret credentials to make anonymous
35
- # requests at a higher rate limit
36
- #
37
- # @see http://developer.github.com/v3/#unauthenticated-rate-limited-requests
38
- # @return Boolean
39
- def application_authenticated?
40
- !!application_authentication
41
- end
42
-
43
- private
44
-
45
- def application_authentication
46
- if @client_id && @client_secret
47
- {
48
- :client_id => @client_id,
49
- :client_secret => @client_secret
50
- }
51
- end
52
- end
53
-
54
- def login_from_netrc
55
- return unless netrc?
56
-
57
- require 'netrc'
58
- info = Netrc.read netrc_file
59
- netrc_host = URI.parse(api_endpoint).host
60
- creds = info[netrc_host]
61
- self.login = creds.shift
62
- self.password = creds.shift
63
- rescue LoadError
64
- warn "Please install netrc gem for .netrc support"
65
- end
66
-
67
- end
68
- end
@@ -1,42 +0,0 @@
1
- require 'faraday'
2
- require 'forwardable'
3
-
4
- module ActiveCampaign
5
- module Response
6
- class Debugger < Faraday::Response::Middleware
7
- extend Forwardable
8
-
9
- def initialize(app, logger = nil)
10
- super(app)
11
- @logger = logger || begin
12
- require 'logger'
13
- ::Logger.new(STDOUT)
14
- end
15
- end
16
-
17
- def_delegators :@logger, :debug, :info, :warn, :error, :fatal
18
-
19
- def call(env)
20
- info("#{env[:method]} #{env[:url].to_s}")
21
- debug('request') { dump_headers(env[:request_headers]) + "\nbody: #{env[:body]}" }
22
- super
23
- end
24
-
25
- def on_complete(env)
26
- info('Status') { env[:status].to_s }
27
- debug('response-head') { dump_headers env[:response_headers] }
28
- debug('response-body') { env[:body] }
29
- end
30
-
31
- private
32
-
33
- def dump_headers(headers)
34
- headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
35
- end
36
- end
37
- end
38
- end
39
-
40
- Faraday.register_middleware :middleware, debugger: lambda {
41
- ActiveCampaign::Response::Debugger
42
- }
@@ -1,33 +0,0 @@
1
- module ActiveCampaign
2
- module Response
3
- # Public: Instruments requests using Active Support.
4
- #
5
- # Measures time spent only for synchronous requests.
6
- #
7
- # Examples
8
- #
9
- # ActiveSupport::Notifications.subscribe('request.faraday') do |name, starts, ends, _, env|
10
- # url = env[:url]
11
- # http_method = env[:method].to_s.upcase
12
- # duration = ends - starts
13
- # $stderr.puts '[%s] %s %s (%.3f s)' % [url.host, http_method, url.request_uri, duration]
14
- # end
15
- class Instrumentation < Faraday::Middleware
16
- dependency 'active_support/notifications'
17
-
18
- def initialize(app, options = {})
19
- super(app)
20
- @name = options.fetch(:name, 'request.faraday')
21
- end
22
-
23
- def call(env)
24
- ActiveSupport::Notifications.instrument(@name, env) do
25
- @app.call(env)
26
- end
27
- end
28
- end
29
- end
30
- end
31
- Faraday.register_middleware :middleware, instrumentation: lambda {
32
- ActiveCampaign::Response::Instrumentation
33
- }
@@ -1,49 +0,0 @@
1
- require 'faraday'
2
- require 'faraday/response'
3
-
4
- module ActiveCampaign
5
- module Response
6
- class JsonNormalizer < Faraday::Response::Middleware
7
- def initialize(app, logger = nil)
8
- super(app)
9
- end
10
-
11
- def call(environment)
12
- @app.call(environment).on_complete do |env|
13
- if env[:body].is_a?(Hash)
14
- env[:body] = normalize(env[:body])
15
- end
16
- end
17
- end
18
-
19
- private
20
-
21
- def normalize(response)
22
- keys, values = keys_values(response)
23
-
24
- if keys.all?{|key| is_numeric?(key) }
25
- response[:results] = values
26
- keys.each do |key|
27
- response.delete(key)
28
- end
29
- end
30
-
31
- response
32
- end
33
-
34
- def is_numeric?(string)
35
- string.to_s.match(/\A[+-]?\d+\Z/) == nil ? false : true
36
- end
37
-
38
- def keys_values(response)
39
- results = results(response)
40
- [results.keys, results.values]
41
- end
42
-
43
- def results(response)
44
- response.reject{|k,v| %w(result_code result_message result_output).include?(k) }
45
- end
46
- end
47
- end
48
- end
49
- Faraday.register_middleware :response, json_normalizer: lambda { ActiveCampaign::Response::JsonNormalizer }
@@ -1,40 +0,0 @@
1
- require 'faraday'
2
-
3
- module ActiveCampaign
4
- module Response
5
-
6
- # Public: Converts parsed response bodies to a Hashie::Mash if they were of
7
- # Hash or Array type.
8
- class Mashify < Faraday::Response::Middleware
9
- attr_accessor :mash_class
10
-
11
- class << self
12
- attr_accessor :mash_class
13
- end
14
-
15
- dependency do
16
- require 'hashie/mash'
17
- self.mash_class = ::Hashie::Mash
18
- end
19
-
20
- def initialize(app = nil, options = {})
21
- super(app)
22
- self.mash_class = options[:mash_class] || self.class.mash_class
23
- end
24
-
25
- def parse(body)
26
- case body
27
- when Hash
28
- mash_class.new(body)
29
- when Array
30
- body.map { |item| parse(item) }
31
- else
32
- body
33
- end
34
- end
35
- end
36
- end
37
- end
38
- Faraday.register_middleware :response, mashify: lambda {
39
- ActiveCampaign::Response::Mashify
40
- }
@@ -1,83 +0,0 @@
1
- module ActiveCampaign
2
- module Response
3
- # Public: Parse response bodies as Oj.
4
- class ParseJson < Faraday::Response::Middleware
5
- CONTENT_TYPE = 'Content-Type'.freeze
6
- MIME_TYPE = 'application/json'.freeze
7
- BRACKETS = %w- [ { -
8
- WHITESPACE = [ " ", "\n", "\r", "\t" ]
9
-
10
- dependency do
11
- require 'oj' unless defined?(::Oj)
12
- end
13
-
14
- def parser
15
- lambda { |body|
16
- ::Oj.load body unless body.strip.empty?
17
- }
18
- end
19
-
20
- def initialize(app = nil, options = {})
21
- super(app)
22
- @options = options
23
- @content_types = Array(options[:content_type])
24
- end
25
-
26
- def call(environment)
27
-
28
-
29
- @app.call(environment).on_complete do |env|
30
- process_response(env)
31
- end
32
- end
33
-
34
- def process_response(env)
35
- old_type = env[:response_headers][CONTENT_TYPE].to_s
36
- new_type = MIME_TYPE.dup
37
- new_type << ';' << old_type.split(';', 2).last if old_type.index(';')
38
- env[:response_headers][CONTENT_TYPE] = new_type
39
-
40
- env[:raw_body] = env[:body] if preserve_raw?(env)
41
- env[:body] = parse(env[:body])
42
- end
43
-
44
- def parse(body)
45
- parser.call(body)
46
- end
47
-
48
- def response_type(env)
49
- type = env[:response_headers][CONTENT_TYPE].to_s
50
- type = type.split(';', 2).first if type.index(';')
51
- type
52
- end
53
-
54
- def process_response_type?(type)
55
- @content_types.empty? or @content_types.any? { |pattern|
56
- pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
57
- }
58
- end
59
-
60
- def parse_response?(env)
61
- env[:body].respond_to?(:to_str) &&
62
- BRACKETS.include?(first_char(env[:body]))
63
- end
64
-
65
- def preserve_raw?(env)
66
- env[:request].fetch(:preserve_raw, @options[:preserve_raw])
67
- end
68
-
69
- def first_char(body)
70
- idx = -1
71
- begin
72
- char = body[idx += 1]
73
- char = char.chr if char
74
- end while char and WHITESPACE.include? char
75
- char
76
- end
77
-
78
- end
79
- end
80
- end
81
- Faraday.register_middleware :response, parse_json: lambda {
82
- ActiveCampaign::Response::ParseJson
83
- }
@@ -1,24 +0,0 @@
1
- require 'faraday'
2
- require 'active_campaign/error'
3
-
4
- module ActiveCampaign
5
- # Faraday response middleware
6
- module Response
7
-
8
- # This class raises an ActiveCampaign-flavored exception based
9
- # HTTP status codes returned by the API
10
- class RaiseError < Faraday::Response::Middleware
11
-
12
- private
13
-
14
- def on_complete(response)
15
- if error = ActiveCampaign::Error.from_response(response)
16
- raise error
17
- end
18
- end
19
- end
20
- end
21
- end
22
- Faraday.register_middleware :response, raise_error: lambda {
23
- ActiveCampaign::Response::RaiseError
24
- }