iterable-api 0.2.3 → 0.2.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4fe814e6c211b3dfc26c78f7748a4dfdc51d849
4
- data.tar.gz: f91e92f8b9cd6883d8612997691a8c56d0cfee33
3
+ metadata.gz: 2dc4e2e159bc6acb049c8157419327d85fa993d8
4
+ data.tar.gz: 1f10e1a2cb609298c14234616cb197b5487807a6
5
5
  SHA512:
6
- metadata.gz: 133219eaf8ec16f6704a3281df19158475c4113dc2898aa9e2417ce809afcdfbd3fee074b81d072204def6ca4aa0633fab5fc3062ea8421f74212bdfd9560bc5
7
- data.tar.gz: d5fc734f1468b1e6485b15af741d4174d1de547b5ed89c0d9d13ffa2cc8837bb9f04ed7e657b184e343ef50ae1abee3d61ada5e2dc53e24ae1179c2b5803ab16
6
+ metadata.gz: 42db222320f5e74460148ce1fa015f1715399e2e7e4ba36e3a3c0375d1461469641b06975654d3b72d83284e8e571b32ce4c57213e6145621ebd08f5e1c4b766
7
+ data.tar.gz: 13e1a52ca5f41d1cae7d00e9f3a61b6ec3dead296532c0ec734f89dc74ba9432e15dbb6d7fb9d121e01aea0f594ac93816aee1c32daf55d4d50908551006d4f6
data/lib/iterable/api.rb CHANGED
@@ -6,12 +6,14 @@
6
6
 
7
7
  module Iterable
8
8
  class Api
9
+ attr_accessor :api_key
10
+
9
11
  # Class constructor
10
12
  # @param [String] api_key - Iterable API Key
11
13
  # @return
12
14
  def initialize(api_key = nil)
13
- Services::BaseService.api_key = api_key || Util::Config.get('auth.api_key')
14
- if Services::BaseService.api_key.nil? || Services::BaseService.api_key == ''
15
+ @api_key = api_key || Util::Config.get('auth.api_key')
16
+ if @api_key.nil? || @api_key == ''
15
17
  raise ArgumentError.new(Util::Config.get('errors.api_key_missing'))
16
18
  end
17
19
  end
@@ -20,34 +22,34 @@ module Iterable
20
22
  # List Services
21
23
  #
22
24
  def lists
23
- Services::ListService.all
25
+ Services::ListService.new(@api_key).all
24
26
  end
25
27
 
26
28
  def lists_subscribe(list_id, subscribers)
27
- Services::ListService.subscribe(list_id, subscribers)
29
+ Services::ListService.new(@api_key).subscribe(list_id, subscribers)
28
30
  end
29
31
 
30
32
  def list_by_id(list_id)
31
- Services::ListService.find_by_id(list_id)
33
+ Services::ListService.new(@api_key).find_by_id(list_id)
32
34
  end
33
35
 
34
36
  #
35
37
  # User Services
36
38
  #
37
39
  def user_by_email(email)
38
- Services::UserService.find_by_email(email)
40
+ Services::UserService.new(@api_key).find_by_email(email)
39
41
  end
40
42
 
41
43
  def user_by_id(id)
42
- Services::UserService.find_by_id(id)
44
+ Services::UserService.new(@api_key).find_by_id(id)
43
45
  end
44
46
 
45
47
  def user_update(user)
46
- Services::UserService.update(user)
48
+ Services::UserService.new(@api_key).update(user)
47
49
  end
48
50
 
49
51
  def user_fields
50
- Services::UserService.fields
52
+ Services::UserService.new(@api_key).fields
51
53
  end
52
54
 
53
55
  def user_subscriptions_update
@@ -57,14 +59,14 @@ module Iterable
57
59
  # Event Services
58
60
  #
59
61
  def track_event(event)
60
- Services::EventService.track(event)
62
+ Services::EventService.new(@api_key).track(event)
61
63
  end
62
64
 
63
65
  #
64
66
  # Commerce Services
65
67
  #
66
68
  def track_purchase(track_purchase_request)
67
- Services::CommerceService.track_purchase(track_purchase_request)
69
+ Services::CommerceService.new(@api_key).track_purchase(track_purchase_request)
68
70
  end
69
71
  end
70
72
  end
@@ -7,69 +7,70 @@
7
7
  module Iterable
8
8
  module Services
9
9
  class BaseService
10
- class << self
11
- attr_accessor :api_key
10
+ attr_accessor :api_key
11
+ def initialize(api_key = nil)
12
+ @api_key = api_key
13
+ end
12
14
 
13
- protected
15
+ protected
14
16
 
15
- def get(path, params = {}, response_type = Iterable::Responses::General)
16
- url = URI::join(Util::Config.get('endpoints.base_url'), path).to_s
17
- url = build_url(url, params)
18
- response = RestClient.get(url, get_headers())
19
- begin
20
- response_type.new JSON.parse(response.body)
21
- rescue
22
- Hashie::Mash.new JSON.parse(response.body)
23
- end
17
+ def get(path, params = {}, response_type = Iterable::Responses::General)
18
+ url = URI::join(Util::Config.get('endpoints.base_url'), path).to_s
19
+ url = build_url(url, params)
20
+ response = RestClient.get(url, get_headers())
21
+ begin
22
+ response_type.new JSON.parse(response.body)
23
+ rescue
24
+ Hashie::Mash.new JSON.parse(response.body)
24
25
  end
26
+ end
25
27
 
26
- def post(path, body = {}, params = {}, response_type = Iterable::Responses::General)
27
- url = URI::join(Util::Config.get('endpoints.base_url'), path).to_s
28
- url = build_url(url, params)
29
- response = RestClient.post(url, body.to_json, get_headers())
30
- begin
31
- response_type.new JSON.parse(response.body)
32
- rescue
33
- Hashie::Mash.new JSON.parse(response.body)
34
- end
28
+ def post(path, body = {}, params = {}, response_type = Iterable::Responses::General)
29
+ url = URI::join(Util::Config.get('endpoints.base_url'), path).to_s
30
+ url = build_url(url, params)
31
+ response = RestClient.post(url, body.to_json, get_headers())
32
+ begin
33
+ response_type.new JSON.parse(response.body)
34
+ rescue
35
+ Hashie::Mash.new JSON.parse(response.body)
35
36
  end
37
+ end
36
38
 
37
- # Return required headers for making an http request with Iterable
38
- # @param [String] content_type - The MIME type of the body of the request, default is 'application/json'
39
- # @return [Hash] - authorization headers
40
- def get_headers(content_type = 'application/json')
41
- {
42
- :content_type => content_type,
43
- :accept => 'application/json',
44
- :user_agent => "Iterable Ruby SDK v#{Iterable::VERSION} (#{RUBY_DESCRIPTION})",
45
- :x_ctct_request_source => "sdk.ruby.#{Iterable::VERSION}",
46
- 'Api-Key' => BaseService.api_key
47
- }
48
- end
39
+ # Return required headers for making an http request with Iterable
40
+ # @param [String] content_type - The MIME type of the body of the request, default is 'application/json'
41
+ # @return [Hash] - authorization headers
42
+ def get_headers(content_type = 'application/json')
43
+ {
44
+ :content_type => content_type,
45
+ :accept => 'application/json',
46
+ :user_agent => "Iterable Ruby SDK v#{Iterable::VERSION} (#{RUBY_DESCRIPTION})",
47
+ :x_ctct_request_source => "sdk.ruby.#{Iterable::VERSION}",
48
+ 'Api-Key' => @api_key
49
+ }
50
+ end
49
51
 
50
- # Build a url from the base url and query parameters hash. Query parameters
51
- # should not be URL encoded because this method will handle that
52
- # @param [String] url - The base url
53
- # @param [Hash] params - A hash with query parameters
54
- # @return [String] - the url with query parameters hash
55
- def build_url(url, params = nil)
56
- if params.respond_to? :each
57
- params.each do |key, value|
58
- # Convert dates to CC date format
59
- if value.respond_to? :iso8601
60
- params[key] = value.iso8601
61
- end
52
+ # Build a url from the base url and query parameters hash. Query parameters
53
+ # should not be URL encoded because this method will handle that
54
+ # @param [String] url - The base url
55
+ # @param [Hash] params - A hash with query parameters
56
+ # @return [String] - the url with query parameters hash
57
+ def build_url(url, params = nil)
58
+ if params.respond_to? :each
59
+ params.each do |key, value|
60
+ # Convert dates to CC date format
61
+ if value.respond_to? :iso8601
62
+ params[key] = value.iso8601
63
+ end
62
64
 
63
- if key.to_s == 'next' && value.match(/^.*?next=(.*)$/)
64
- params[key] = $1
65
- end
65
+ if key.to_s == 'next' && value.match(/^.*?next=(.*)$/)
66
+ params[key] = $1
66
67
  end
67
- else
68
- params ||= {}
69
68
  end
70
- url + '?' + Util::Helpers.http_build_query(params)
69
+ else
70
+ params ||= {}
71
71
  end
72
+ url + '?' + Util::Helpers.http_build_query(params)
72
73
  end
73
74
  end
74
75
  end
75
- end
76
+ end
@@ -7,12 +7,10 @@
7
7
  module Iterable
8
8
  module Services
9
9
  class CommerceService < BaseService
10
- class << self
11
- def track_purchase(request)
12
- raise Exceptions::ServiceException, "Must be a Iterable::Requests::TrackPurchase" unless request.is_a?(Iterable::Requests::TrackPurchase)
13
- post(Util::Config.get('endpoints.track_purchase'), request)
14
- end
10
+ def track_purchase(request)
11
+ raise Exceptions::ServiceException, "Must be a Iterable::Requests::TrackPurchase" unless request.is_a?(Iterable::Requests::TrackPurchase)
12
+ post(Util::Config.get('endpoints.track_purchase'), request)
15
13
  end
16
14
  end
17
15
  end
18
- end
16
+ end
@@ -7,12 +7,10 @@
7
7
  module Iterable
8
8
  module Services
9
9
  class EventService < BaseService
10
- class << self
11
10
  def track(event)
12
11
  raise Exceptions::ServiceException, "Must be a Iterable::Event" unless event.is_a?(Iterable::Event)
13
12
  post(Util::Config.get('endpoints.track_event'), event)
14
13
  end
15
- end
16
14
  end
17
15
  end
18
16
  end
@@ -7,20 +7,18 @@
7
7
  module Iterable
8
8
  module Services
9
9
  class ListService < BaseService
10
- class << self
11
- def all
12
- get(Util::Config.get('endpoints.lists'), nil, Iterable::Responses::Lists)
13
- end
10
+ def all
11
+ get(Util::Config.get('endpoints.lists'), nil, Iterable::Responses::Lists)
12
+ end
14
13
 
15
- def find_by_id(id)
16
- # iterate over all lists to find the id
17
- all.lists.select{|x| x.id == id.to_i}.first
18
- end
14
+ def find_by_id(id)
15
+ # iterate over all lists to find the id
16
+ all.lists.select{|x| x.id == id.to_i}.first
17
+ end
19
18
 
20
- def subscribe(list_id, subscribers)
21
- request = Iterable::Requests::Subscribe.new(listId: list_id, subscribers: subscribers)
22
- post(Util::Config.get('endpoints.lists_subscribe'), request, nil, Iterable::Responses::Subscribe)
23
- end
19
+ def subscribe(list_id, subscribers)
20
+ request = Iterable::Requests::Subscribe.new(listId: list_id, subscribers: subscribers)
21
+ post(Util::Config.get('endpoints.lists_subscribe'), request, nil, Iterable::Responses::Subscribe)
24
22
  end
25
23
  end
26
24
  end
@@ -7,32 +7,30 @@
7
7
  module Iterable
8
8
  module Services
9
9
  class UserService < BaseService
10
- class << self
11
- def find_by_email(email)
12
- raise Exceptions::ServiceException, "Email is required." if email.nil?
13
- find_by((Util::Config.get('endpoints.user_by_email') % [email]))
14
- end
10
+ def find_by_email(email)
11
+ raise Exceptions::ServiceException, "Email is required." if email.nil?
12
+ find_by((Util::Config.get('endpoints.user_by_email') % [email]))
13
+ end
15
14
 
16
- def find_by_id(id)
17
- raise Exceptions::ServiceException, "Id is required." if id.nil?
18
- find_by((Util::Config.get('endpoints.user_by_id') % [id]))
19
- end
15
+ def find_by_id(id)
16
+ raise Exceptions::ServiceException, "Id is required." if id.nil?
17
+ find_by((Util::Config.get('endpoints.user_by_id') % [id]))
18
+ end
20
19
 
21
- def update(user)
22
- raise Exceptions::ServiceException, "Must be a Iterable::Requests::UserUpdate" unless user.is_a?(Iterable::Requests::UserUpdate)
23
- post(Util::Config.get('endpoints.user_update'), user)
24
- end
20
+ def update(user)
21
+ raise Exceptions::ServiceException, "Must be a Iterable::Requests::UserUpdate" unless user.is_a?(Iterable::Requests::UserUpdate)
22
+ post(Util::Config.get('endpoints.user_update'), user)
23
+ end
25
24
 
26
- def fields
27
- get(Util::Config.get('endpoints.user_fields'))
28
- end
25
+ def fields
26
+ get(Util::Config.get('endpoints.user_fields'))
27
+ end
29
28
 
30
- private
29
+ private
31
30
 
32
- def find_by(path)
33
- get(path, nil, Iterable::Responses::User)
34
- end
31
+ def find_by(path)
32
+ get(path, nil, Iterable::Responses::User)
35
33
  end
36
34
  end
37
35
  end
38
- end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module Iterable
2
- VERSION = "0.2.3".freeze
2
+ VERSION = "0.2.4".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iterable-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iterable