active_campaign 0.0.12 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +0,0 @@
1
- module ActiveCampaign
2
- module ParamValidation
3
- def filter_params(allowed_keys = [], params = {})
4
- params.keys.select do |k,v|
5
- allowed_keys.each do |param|
6
- params.include?(k) ||
7
- param.match(k)
8
- end
9
- end
10
- end
11
- end
12
- end
@@ -1,84 +0,0 @@
1
- require 'multi_json'
2
-
3
- module ActiveCampaign
4
- module Request
5
-
6
- def delete(api_method, options={})
7
- request(:delete, api_method, options).body
8
- end
9
-
10
- def get(api_method, options={})
11
- request(:get, api_method, options).body
12
- end
13
-
14
- def patch(api_method, options={})
15
- request(:patch, api_method, options).body
16
- end
17
-
18
- def post(api_method, options={})
19
- request(:post, api_method, options).body
20
- rescue => e
21
- puts e, e.backtrace
22
- end
23
-
24
- def put(api_method, options={})
25
- request(:put, api_method, options).body
26
- end
27
-
28
- private
29
-
30
- # Executes the request, checking if it was successful
31
- #
32
- # @return [Boolean] True on success, false otherwise
33
- def boolean_from_response(method, api_method, options={})
34
- request(method, api_method, options).status == 204
35
- rescue ActiveCampaign::NotFound
36
- false
37
- end
38
-
39
- def path(api_method)
40
- "#{api_path}?#{url_params(api_method).map{|k,v| "#{k}=#{v}" }.join('&')}"
41
- end
42
-
43
- def url_params(api_method)
44
- {
45
- api_key: api_key,
46
- api_action: api_method,
47
- api_output: api_output,
48
- }
49
- end
50
-
51
- def request(method, api_method, options={})
52
-
53
- force_urlencoded = options.delete(:force_urlencoded) || false
54
- url = options.delete(:endpoint) || api_endpoint
55
-
56
- conn_options = {
57
- :force_urlencoded => force_urlencoded,
58
- :url => url
59
- }
60
-
61
- response = connection(conn_options).send(method) do |request|
62
- case method
63
- when :get, :delete, :head
64
- options.reverse_merge!(url_params(api_method))
65
- request.url(api_path, options)
66
- when :patch, :post, :put
67
- request.url(api_path, url_params(api_method))
68
- if force_urlencoded
69
- request.body = options unless options.empty?
70
- else
71
- request.body = MultiJson.dump(options) unless options.empty?
72
- end
73
- end
74
-
75
- if ActiveCampaign.request_host
76
- request.headers['Host'] = ActiveCampaign.request_host
77
- end
78
-
79
- end
80
- response
81
- end
82
-
83
- end
84
- end
@@ -1,37 +0,0 @@
1
- require 'faraday'
2
- require 'forwardable'
3
-
4
- module Faraday
5
- class Response::BodyLogger < Response::Middleware
6
- extend Forwardable
7
-
8
- def initialize(app, logger = nil)
9
- super(app)
10
- @logger = logger || begin
11
- require 'logger'
12
- ::Logger.new(STDOUT)
13
- end
14
- end
15
-
16
- def_delegators :@logger, :debug, :info, :warn, :error, :fatal
17
-
18
- def call(env)
19
- info("#{env[:method]} #{env[:url].to_s}")
20
- debug('request') { dump_headers(env[:request_headers]) + "\nbody: #{env[:body]}" }
21
- super
22
- end
23
-
24
- def on_complete(env)
25
- info('Status') { env[:status].to_s }
26
- debug('response-head') { dump_headers env[:response_headers] }
27
- debug('response-body') { env[:body] }
28
- end
29
-
30
- private
31
-
32
- def dump_headers(headers)
33
- headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
34
- end
35
- end
36
- end
37
- Faraday.register_middleware :response, body_logger: lambda { Faraday::Response::BodyLogger }
@@ -1,46 +0,0 @@
1
- require 'faraday'
2
- require 'faraday/response'
3
-
4
- module Faraday
5
- class Response::JsonNormalizer < Response::Middleware
6
- def initialize(app, logger = nil)
7
- super(app)
8
- end
9
-
10
- def call(environment)
11
- @app.call(environment).on_complete do |env|
12
- if env[:body].is_a?(Hash)
13
- env[:body] = normalize(env[:body])
14
- end
15
- end
16
- end
17
-
18
- private
19
-
20
- def normalize(response)
21
- keys, values = keys_values(response)
22
-
23
- if keys.all?{|key| is_numeric?(key) }
24
- response[:results] = values
25
- keys.each do |key|
26
- response.delete(key)
27
- end
28
- end
29
- response
30
- end
31
-
32
- def is_numeric?(string)
33
- string.to_s.match(/\A[+-]?\d+\Z/) == nil ? false : true
34
- end
35
-
36
- def keys_values(response)
37
- results = results(response)
38
- [results.keys, results.values]
39
- end
40
-
41
- def results(response)
42
- response.reject{|k,v| %w(result_code result_message result_output).include?(k) }
43
- end
44
- end
45
- end
46
- Faraday.register_middleware :response, json_normalizer: lambda { Faraday::Response::JsonNormalizer }
@@ -1,25 +0,0 @@
1
- require 'faraday'
2
- require 'multi_json'
3
-
4
- # @api private
5
- module Faraday
6
- class Response::RaiseActiveCampaignError < Response::Middleware
7
- ERROR_MAP = {
8
- 400 => ActiveCampaign::BadRequest,
9
- 401 => ActiveCampaign::Unauthorized,
10
- 403 => ActiveCampaign::Forbidden,
11
- 404 => ActiveCampaign::NotFound,
12
- 406 => ActiveCampaign::NotAcceptable,
13
- 422 => ActiveCampaign::UnprocessableEntity,
14
- 500 => ActiveCampaign::InternalServerError,
15
- 501 => ActiveCampaign::NotImplemented,
16
- 502 => ActiveCampaign::BadGateway,
17
- 503 => ActiveCampaign::ServiceUnavailable
18
- }
19
-
20
- def on_complete(response)
21
- key = response[:status].to_i
22
- raise ERROR_MAP[key].new(response) if ERROR_MAP.has_key? key
23
- end
24
- end
25
- end