constantcontact-ruby 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/README.md +136 -0
  4. data/constantcontact-ruby.gemspec +31 -0
  5. data/lib/constantcontact/api.rb +1012 -0
  6. data/lib/constantcontact/auth/oauth2.rb +105 -0
  7. data/lib/constantcontact/auth/session_data_store.rb +69 -0
  8. data/lib/constantcontact/components/account/account_address.rb +26 -0
  9. data/lib/constantcontact/components/account/account_info.rb +38 -0
  10. data/lib/constantcontact/components/account/verified_email_address.rb +26 -0
  11. data/lib/constantcontact/components/activities/activity.rb +43 -0
  12. data/lib/constantcontact/components/activities/activity_error.rb +26 -0
  13. data/lib/constantcontact/components/activities/add_contacts.rb +127 -0
  14. data/lib/constantcontact/components/activities/add_contacts_import_data.rb +46 -0
  15. data/lib/constantcontact/components/activities/export_contacts.rb +30 -0
  16. data/lib/constantcontact/components/component.rb +48 -0
  17. data/lib/constantcontact/components/contacts/address.rb +27 -0
  18. data/lib/constantcontact/components/contacts/contact.rb +92 -0
  19. data/lib/constantcontact/components/contacts/contact_list.rb +26 -0
  20. data/lib/constantcontact/components/contacts/custom_field.rb +26 -0
  21. data/lib/constantcontact/components/contacts/email_address.rb +33 -0
  22. data/lib/constantcontact/components/contacts/note.rb +26 -0
  23. data/lib/constantcontact/components/email_marketing/campaign.rb +83 -0
  24. data/lib/constantcontact/components/email_marketing/campaign_preview.rb +29 -0
  25. data/lib/constantcontact/components/email_marketing/click_through_details.rb +27 -0
  26. data/lib/constantcontact/components/email_marketing/message_footer.rb +29 -0
  27. data/lib/constantcontact/components/email_marketing/schedule.rb +27 -0
  28. data/lib/constantcontact/components/email_marketing/test_send.rb +45 -0
  29. data/lib/constantcontact/components/event_spot/contact.rb +27 -0
  30. data/lib/constantcontact/components/event_spot/event.rb +69 -0
  31. data/lib/constantcontact/components/event_spot/event_address.rb +29 -0
  32. data/lib/constantcontact/components/event_spot/event_fee.rb +27 -0
  33. data/lib/constantcontact/components/event_spot/event_item.rb +36 -0
  34. data/lib/constantcontact/components/event_spot/event_item_attribute.rb +26 -0
  35. data/lib/constantcontact/components/event_spot/event_track.rb +31 -0
  36. data/lib/constantcontact/components/event_spot/guest.rb +31 -0
  37. data/lib/constantcontact/components/event_spot/guest_section.rb +34 -0
  38. data/lib/constantcontact/components/event_spot/notification_option.rb +27 -0
  39. data/lib/constantcontact/components/event_spot/online_meeting.rb +28 -0
  40. data/lib/constantcontact/components/event_spot/payment_address.rb +29 -0
  41. data/lib/constantcontact/components/event_spot/payment_summary.rb +33 -0
  42. data/lib/constantcontact/components/event_spot/promocode.rb +25 -0
  43. data/lib/constantcontact/components/event_spot/registrant.rb +52 -0
  44. data/lib/constantcontact/components/event_spot/registrant_fee.rb +29 -0
  45. data/lib/constantcontact/components/event_spot/registrant_field.rb +27 -0
  46. data/lib/constantcontact/components/event_spot/registrant_order.rb +39 -0
  47. data/lib/constantcontact/components/event_spot/registrant_promo_code.rb +31 -0
  48. data/lib/constantcontact/components/event_spot/registrant_promo_code_info.rb +27 -0
  49. data/lib/constantcontact/components/event_spot/registrant_section.rb +34 -0
  50. data/lib/constantcontact/components/event_spot/sale_item.rb +29 -0
  51. data/lib/constantcontact/components/library/file/library_file.rb +27 -0
  52. data/lib/constantcontact/components/library/folder/library_folder.rb +26 -0
  53. data/lib/constantcontact/components/library/info/library_summary.rb +26 -0
  54. data/lib/constantcontact/components/library/info/move_results.rb +26 -0
  55. data/lib/constantcontact/components/library/info/upload_status.rb +26 -0
  56. data/lib/constantcontact/components/result_set.rb +50 -0
  57. data/lib/constantcontact/components/tracking/bounce_activity.rb +28 -0
  58. data/lib/constantcontact/components/tracking/click_activity.rb +26 -0
  59. data/lib/constantcontact/components/tracking/forward_activity.rb +26 -0
  60. data/lib/constantcontact/components/tracking/open_activity.rb +26 -0
  61. data/lib/constantcontact/components/tracking/send_activity.rb +27 -0
  62. data/lib/constantcontact/components/tracking/tracking_activity.rb +26 -0
  63. data/lib/constantcontact/components/tracking/tracking_summary.rb +27 -0
  64. data/lib/constantcontact/components/tracking/unsubscribe_activity.rb +27 -0
  65. data/lib/constantcontact/exceptions/ctct_exception.rb +25 -0
  66. data/lib/constantcontact/exceptions/illegal_argument_exception.rb +11 -0
  67. data/lib/constantcontact/exceptions/oauth2_exception.rb +11 -0
  68. data/lib/constantcontact/exceptions/webhooks_exception.rb +11 -0
  69. data/lib/constantcontact/services/account_service.rb +39 -0
  70. data/lib/constantcontact/services/activity_service.rb +136 -0
  71. data/lib/constantcontact/services/base_service.rb +68 -0
  72. data/lib/constantcontact/services/campaign_schedule_service.rb +101 -0
  73. data/lib/constantcontact/services/campaign_tracking_service.rb +152 -0
  74. data/lib/constantcontact/services/contact_service.rb +105 -0
  75. data/lib/constantcontact/services/contact_tracking_service.rb +152 -0
  76. data/lib/constantcontact/services/email_marketing_service.rb +94 -0
  77. data/lib/constantcontact/services/event_spot_service.rb +448 -0
  78. data/lib/constantcontact/services/library_service.rb +281 -0
  79. data/lib/constantcontact/services/list_service.rb +81 -0
  80. data/lib/constantcontact/util/config.rb +180 -0
  81. data/lib/constantcontact/util/helpers.rb +27 -0
  82. data/lib/constantcontact/version.rb +12 -0
  83. data/lib/constantcontact/webhooks/helpers/validator.rb +30 -0
  84. data/lib/constantcontact/webhooks/models/billing_change_notification.rb +27 -0
  85. data/lib/constantcontact/webhooks/webhooks_util.rb +45 -0
  86. data/lib/constantcontact.rb +124 -0
  87. data/spec/constantcontact/api_spec.rb +1560 -0
  88. data/spec/constantcontact/auth/oauth2_spec.rb +125 -0
  89. data/spec/constantcontact/components/contacts/address_spec.rb +18 -0
  90. data/spec/constantcontact/components/contacts/contact_list_spec.rb +18 -0
  91. data/spec/constantcontact/components/contacts/contact_spec.rb +18 -0
  92. data/spec/constantcontact/components/contacts/custom_field_spec.rb +18 -0
  93. data/spec/constantcontact/components/contacts/email_address_spec.rb +18 -0
  94. data/spec/constantcontact/sdk_spec.rb +24 -0
  95. data/spec/constantcontact/services/account_service_spec.rb +46 -0
  96. data/spec/constantcontact/services/activity_service_spec.rb +244 -0
  97. data/spec/constantcontact/services/base_service_spec.rb +50 -0
  98. data/spec/constantcontact/services/campaign_schedule_service_spec.rb +115 -0
  99. data/spec/constantcontact/services/campaign_tracking_service_spec.rb +131 -0
  100. data/spec/constantcontact/services/contact_service_spec.rb +111 -0
  101. data/spec/constantcontact/services/contact_tracking_service_spec.rb +131 -0
  102. data/spec/constantcontact/services/email_marketing_spec.rb +100 -0
  103. data/spec/constantcontact/services/event_spot_spec.rb +423 -0
  104. data/spec/constantcontact/services/library_service_spec.rb +252 -0
  105. data/spec/constantcontact/services/list_service_spec.rb +89 -0
  106. data/spec/constantcontact/webhooks/webhooks_spec.rb +26 -0
  107. metadata +243 -0
@@ -0,0 +1,50 @@
1
+ #
2
+ # result_set.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Components
9
+ class ResultSet
10
+ attr_accessor :results, :next
11
+
12
+
13
+ # Constructor to create a ResultSet from the results/meta response when performing a get on a collection
14
+ # @param [Array<Hash>] results - results array from request
15
+ # @param [Hash] meta - meta hash from request
16
+ def initialize(results, meta, component = nil)
17
+ @results = results
18
+ if component.present?
19
+ @component = component
20
+ end
21
+
22
+ if meta.has_key?('pagination') and meta['pagination'].has_key?('next_link')
23
+ @next_link = meta['pagination']['next_link']
24
+ @next = @next_link[@next_link.index('?'), @next_link.length]
25
+ end
26
+ end
27
+
28
+ def next_results
29
+ if @next_link.present? and @component.present?
30
+ url = Util::Config.get('endpoints.api_url') + @next_link
31
+ url = url.gsub(@next, "")
32
+ url = Services::BaseService.build_url(url, {
33
+ :next => @next.split("next=")[1]
34
+ })
35
+ response = RestClient.get(url, Services::BaseService.get_headers())
36
+ body = JSON.parse(response.body)
37
+
38
+ events = body['results'].collect do |event|
39
+ @component.create_summary(event)
40
+ end
41
+
42
+ return Components::ResultSet.new(events, body['meta'], @component)
43
+ else
44
+ return nil
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,28 @@
1
+ #
2
+ # bounce_activity.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Components
9
+ class BounceActivity < Component
10
+ attr_accessor :activity_type, :bounce_code, :bounce_description, :bounce_message, :bounce_date,
11
+ :contact_id, :email_address, :campaign_id
12
+
13
+
14
+ # Factory method to create a BounceActivity object from an array
15
+ # @param [Hash] props - properties to create object from
16
+ # @return [BounceActivity]
17
+ def self.create(props)
18
+ obj = BounceActivity.new
19
+ if props
20
+ props.each do |key, value|
21
+ obj.send("#{key}=", value) if obj.respond_to? key
22
+ end
23
+ end
24
+ obj
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # click_activity.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Components
9
+ class ClickActivity < Component
10
+ attr_accessor :activity_type, :campaign_id, :contact_id, :email_address, :link_id, :click_date
11
+
12
+ # Factory method to create a ClickActivity object from an array
13
+ # @param [Hash] props - properties to create object from
14
+ # @return [ClickActivity]
15
+ def self.create(props)
16
+ obj = ClickActivity.new
17
+ if props
18
+ props.each do |key, value|
19
+ obj.send("#{key}=", value) if obj.respond_to? key
20
+ end
21
+ end
22
+ obj
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # forward_activity.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Components
9
+ class ForwardActivity < Component
10
+ attr_accessor :activity_type, :campaign_id, :contact_id, :email_address, :forward_date
11
+
12
+ # Factory method to create a ForwardActivity object from an array
13
+ # @param [Hash] props - properties to create object from
14
+ # @return [ForwardActivity]
15
+ def self.create(props)
16
+ obj = ForwardActivity.new
17
+ if props
18
+ props.each do |key, value|
19
+ obj.send("#{key}=", value) if obj.respond_to? key
20
+ end
21
+ end
22
+ obj
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # open_activity.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Components
9
+ class OpenActivity < Component
10
+ attr_accessor :activity_type, :campaign_id, :email_address, :open_date, :contact_id
11
+
12
+ # Factory method to create an OpenActivity object from an array
13
+ # @param [Hash] props - properties to create object from
14
+ # @return [OpenActivity]
15
+ def self.create(props)
16
+ obj = OpenActivity.new
17
+ if props
18
+ props.each do |key, value|
19
+ obj.send("#{key}=", value) if obj.respond_to? key
20
+ end
21
+ end
22
+ obj
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ #
2
+ # send_activity.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Components
9
+ class SendActivity < Component
10
+ attr_accessor :activity_type, :campaign_id, :email_address, :contact_id, :send_date
11
+
12
+
13
+ # Factory method to create a SendActivity object from an array
14
+ # @param [Hash] props - properties to create object from
15
+ # @return [SendActivity]
16
+ def self.create(props)
17
+ obj = SendActivity.new
18
+ if props
19
+ props.each do |key, value|
20
+ obj.send("#{key}=", value)
21
+ end
22
+ end
23
+ obj
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # tracking_activity.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Components
9
+ class TrackingActivity < Component
10
+ attr_accessor :results, :next
11
+
12
+
13
+ # Constructor to create a TrackingActivity from the results/pagination response from getting a set of activities
14
+ # @param [Array] results - results array from a tracking endpoint
15
+ # @param [Hash] pagination - pagination hash returned from a tracking endpoint
16
+ # @return [TrackingActivity]
17
+ def initialize(results, pagination)
18
+ @results = results
19
+
20
+ if (pagination.has_key?('next'))
21
+ @next = pagination['next'][pagination['next'].index('&next=') + 6, pagination['next'].length]
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ #
2
+ # tracking_summary.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Components
9
+ class TrackingSummary < Component
10
+ attr_accessor :sends, :opens, :clicks, :forwards, :unsubscribes, :bounces, :spam_count
11
+
12
+
13
+ # Factory method to create a TrackingSummary object from an array
14
+ # @param [Hash] props - properties to create object from
15
+ # @return [TrackingSummary]
16
+ def self.create(props)
17
+ obj = TrackingSummary.new
18
+ if props
19
+ props.each do |key, value|
20
+ obj.send("#{key}=", value) if obj.respond_to? key
21
+ end
22
+ end
23
+ obj
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ #
2
+ # unsubscribe_activity.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Components
9
+ class UnsubscribeActivity < Component
10
+ attr_accessor :activity_type, :campaign_id, :contact_id, :email_address, :unsubscribe_date,
11
+ :unsubscribe_source, :unsubscribe_reason
12
+
13
+ # Factory method to create an UnsubscribeActivity object from an array
14
+ # @param [Hash] props - properties to create object from
15
+ # @return [UnsubscribeActivity]
16
+ def self.create(props)
17
+ obj = UnsubscribeActivity.new
18
+ if props
19
+ props.each do |key, value|
20
+ obj.send("#{key}=", value) if obj.respond_to? key
21
+ end
22
+ end
23
+ obj
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ #
2
+ # ctct_exception.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Exceptions
9
+ class CtctException < Exception
10
+ attr_accessor :errors
11
+
12
+ # Setter
13
+ # @param [Array<String>] errors
14
+ def set_errors(errors)
15
+ @errors = errors
16
+ end
17
+
18
+ # Getter
19
+ # @return [Array<String>]
20
+ def get_errors
21
+ @errors
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ #
2
+ # illegal_argument_exception.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Exceptions
9
+ class IllegalArgumentException < Exception; end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ #
2
+ # oauth2_exception.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Exceptions
9
+ class OAuth2Exception < Exception; end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ #
2
+ # webhooks_exception.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Exceptions
9
+ class WebhooksException < Exception; end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ #
2
+ # account_service.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Services
9
+ class AccountService < BaseService
10
+ class << self
11
+
12
+ # Get a summary of account information
13
+ # @return [AccountInfo]
14
+ def get_account_info()
15
+ url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.account_info')
16
+ url = build_url(url)
17
+ response = RestClient.get(url, get_headers())
18
+ Components::AccountInfo.create(JSON.parse(response.body))
19
+ end
20
+
21
+
22
+ # Get all verified email addresses associated with an account
23
+ # @param [Hash] params - hash of query parameters/values to append to the request
24
+ # @return [Array<VerifiedEmailAddress>]
25
+ def get_verified_email_addresses(params)
26
+ url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.account_verified_addresses')
27
+ url = build_url(url, params)
28
+ response = RestClient.get(url, get_headers())
29
+ email_addresses = []
30
+ JSON.parse(response.body).each do |email_address|
31
+ email_addresses << Components::VerifiedEmailAddress.create(email_address)
32
+ end
33
+ email_addresses
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,136 @@
1
+ #
2
+ # activity_service.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Services
9
+ class ActivityService < BaseService
10
+ class << self
11
+
12
+ # Get a set of activities
13
+ # @param [Hash] params - query parameters to be appended to the url
14
+ # @return [Array<Activity>]
15
+ def get_activities(params = {})
16
+ url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.activities')
17
+ url = build_url(url, params)
18
+ response = RestClient.get(url, get_headers())
19
+
20
+ activities = []
21
+ JSON.parse(response.body).each do |activity|
22
+ activities << Components::Activity.create(activity)
23
+ end
24
+
25
+ activities
26
+ end
27
+
28
+
29
+ # Get an array of activities
30
+ # @param [String] activity_id - Activity id
31
+ # @return [Activity]
32
+ def get_activity(activity_id)
33
+ url = Util::Config.get('endpoints.base_url') +
34
+ sprintf(Util::Config.get('endpoints.activity'), activity_id)
35
+ url = build_url(url)
36
+ response = RestClient.get(url, get_headers())
37
+ Components::Activity.create(JSON.parse(response.body))
38
+ end
39
+
40
+
41
+ # Create an Add Contacts Activity
42
+ # @param [AddContacts] add_contacts
43
+ # @return [Activity]
44
+ def create_add_contacts_activity(add_contacts)
45
+ url = Util::Config.get('endpoints.base_url') +
46
+ Util::Config.get('endpoints.add_contacts_activity')
47
+ url = build_url(url)
48
+ payload = add_contacts.to_json
49
+ response = RestClient.post(url, payload, get_headers())
50
+ Components::Activity.create(JSON.parse(response.body))
51
+ end
52
+
53
+
54
+ # Create an Add Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx
55
+ # @param [String] file_name - The name of the file (ie: contacts.csv)
56
+ # @param [String] contents - The content of the file
57
+ # @param [String] lists - Comma separated list of ContactList id's to add the contacts to
58
+ # @return [Activity]
59
+ def create_add_contacts_activity_from_file(file_name, contents, lists)
60
+ url = Util::Config.get('endpoints.base_url') +
61
+ Util::Config.get('endpoints.add_contacts_activity')
62
+ url = build_url(url)
63
+
64
+ payload = { :file_name => file_name, :lists => lists, :data => contents, :multipart => true }
65
+
66
+ response = RestClient.post(url, payload, get_headers())
67
+ Components::Activity.create(JSON.parse(response.body))
68
+ end
69
+
70
+
71
+ # Create a Clear Lists Activity
72
+ # @param [Array<lists>] lists - array of list id's to be cleared
73
+ # @return [Activity]
74
+ def add_clear_lists_activity(lists)
75
+ url = Util::Config.get('endpoints.base_url') +
76
+ Util::Config.get('endpoints.clear_lists_activity')
77
+ url = build_url(url)
78
+ payload = {'lists' => lists}.to_json
79
+ response = RestClient.post(url, payload, get_headers())
80
+ Components::Activity.create(JSON.parse(response.body))
81
+ end
82
+
83
+
84
+ # Create an Export Contacts Activity
85
+ # @param [ExportContacts] export_contacts
86
+ # @return [Activity]
87
+ def add_export_contacts_activity(export_contacts)
88
+ url = Util::Config.get('endpoints.base_url') +
89
+ Util::Config.get('endpoints.export_contacts_activity')
90
+ url = build_url(url)
91
+ payload = export_contacts.to_json
92
+ response = RestClient.post(url, payload, get_headers())
93
+ Components::Activity.create(JSON.parse(response.body))
94
+ end
95
+
96
+
97
+ # Create a Remove Contacts From Lists Activity
98
+ # @param [<Array>EmailAddress] email_addresses
99
+ # @param [<Array>RemoveFromLists] lists
100
+ # @return [Activity]
101
+ def add_remove_contacts_from_lists_activity(email_addresses, lists)
102
+ url = Util::Config.get('endpoints.base_url') +
103
+ Util::Config.get('endpoints.remove_from_lists_activity')
104
+ url = build_url(url)
105
+
106
+ payload = { 'import_data' => [], 'lists' => lists }
107
+ email_addresses.each do |email_address|
108
+ payload['import_data'] << { 'email_addresses' => [email_address] }
109
+ end
110
+ payload = payload.to_json
111
+
112
+ response = RestClient.post(url, payload, get_headers())
113
+ Components::Activity.create(JSON.parse(response.body))
114
+ end
115
+
116
+
117
+ # Create an Remove Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx
118
+ # @param [String] file_name - The name of the file (ie: contacts.csv)
119
+ # @param [String] contents - The content of the file
120
+ # @param [String] lists - Comma separated list of ContactList id' to add the contacts too
121
+ # @return [Activity]
122
+ def add_remove_contacts_from_lists_activity_from_file(file_name, contents, lists)
123
+ url = Util::Config.get('endpoints.base_url') +
124
+ Util::Config.get('endpoints.remove_from_lists_activity')
125
+ url = build_url(url)
126
+
127
+ payload = { :file_name => file_name, :lists => lists, :data => contents, :multipart => true }
128
+
129
+ response = RestClient.post(url, payload, get_headers())
130
+ Components::Activity.create(JSON.parse(response.body))
131
+ end
132
+
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,68 @@
1
+ #
2
+ # base_service.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Services
9
+ class BaseService
10
+ class << self
11
+ attr_accessor :api_key, :access_token
12
+
13
+ # Build a url from the base url and query parameters hash. Query parameters
14
+ # should not be URL encoded because this method will handle that
15
+ # @param [String] url - The base url
16
+ # @param [Hash] params - A hash with query parameters
17
+ # @return [String] - the url with query parameters hash
18
+ def build_url(url, params = nil)
19
+ if params.respond_to? :each
20
+ params.each do |key, value|
21
+ # Convert dates to CC date format
22
+ if value.respond_to? :iso8601
23
+ params[key] = value.iso8601
24
+ end
25
+
26
+ if key.to_s == 'next' && value.match(/^.*?next=(.*)$/)
27
+ params[key] = $1
28
+ end
29
+ end
30
+ else
31
+ params ||= {}
32
+ end
33
+
34
+ params['api_key'] = BaseService.api_key
35
+ url += '?' + Util::Helpers.http_build_query(params)
36
+ end
37
+
38
+ # Return required headers for making an http request with Constant Contact
39
+ # @param [String] content_type - The MIME type of the body of the request, default is 'application/json'
40
+ # @return [Hash] - authorization headers
41
+ def get_headers(content_type = 'application/json')
42
+ {
43
+ :content_type => content_type,
44
+ :accept => 'application/json',
45
+ :authorization => "Bearer #{BaseService.access_token}",
46
+ :user_agent => "AppConnect Ruby SDK v#{ConstantContact::SDK::VERSION} (#{RUBY_DESCRIPTION})",
47
+ :x_ctct_request_source => "sdk.ruby.#{ConstantContact::SDK::VERSION}"
48
+ }
49
+ end
50
+
51
+ protected
52
+
53
+
54
+ # returns the id of a ConstantContact component
55
+ def get_id_for(obj)
56
+ if obj.kind_of? ConstantContact::Components::Component
57
+ obj.id
58
+ elsif obj.kind_of? Hash
59
+ obj["id"]
60
+ else
61
+ obj
62
+ end
63
+ end
64
+
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,101 @@
1
+ #
2
+ # campaign_schedule_service.rb
3
+ # ConstantContact
4
+ #
5
+ # Copyright (c) 2013 Constant Contact. All rights reserved.
6
+
7
+ module ConstantContact
8
+ module Services
9
+ class CampaignScheduleService < BaseService
10
+ class << self
11
+
12
+ # Create a new schedule for a campaign
13
+ # @param [Integer] campaign_id - Campaign id to be scheduled
14
+ # @param [Schedule] schedule - Schedule to be created
15
+ # @return [Schedule]
16
+ def add_schedule(campaign_id, schedule)
17
+ url = Util::Config.get('endpoints.base_url') +
18
+ sprintf(Util::Config.get('endpoints.campaign_schedules'), campaign_id)
19
+ url = build_url(url)
20
+ payload = schedule.to_json
21
+ response = RestClient.post(url, payload, get_headers())
22
+ Components::Schedule.create(JSON.parse(response.body))
23
+ end
24
+
25
+
26
+ # Get a list of schedules for a campaign
27
+ # @param [Integer] campaign_id - Campaign id to be scheduled
28
+ # @return [Array<Schedule>]
29
+ def get_schedules(campaign_id)
30
+ url = Util::Config.get('endpoints.base_url') +
31
+ sprintf(Util::Config.get('endpoints.campaign_schedules'), campaign_id)
32
+ url = build_url(url)
33
+ response = RestClient.get(url, get_headers())
34
+ body = JSON.parse(response.body)
35
+
36
+ schedules = []
37
+ body.each do |schedule|
38
+ schedules << Components::Schedule.create(schedule)
39
+ end
40
+
41
+ schedules
42
+ end
43
+
44
+
45
+ # Get a specific schedule for a campaign
46
+ # @param [Integer] campaign_id - Campaign id to get a schedule for
47
+ # @param [Integer] schedule_id - Schedule id to retrieve
48
+ # @return [Schedule]
49
+ def get_schedule(campaign_id, schedule_id)
50
+ url = Util::Config.get('endpoints.base_url') +
51
+ sprintf(Util::Config.get('endpoints.campaign_schedule'), campaign_id, schedule_id)
52
+ url = build_url(url)
53
+ response = RestClient.get(url, get_headers())
54
+ Components::Schedule.create(JSON.parse(response.body))
55
+ end
56
+
57
+
58
+ # Delete a specific schedule for a campaign
59
+ # @param [Integer] campaign_id - Campaign id to delete a schedule for
60
+ # @param [Integer] schedule_id - Schedule id to delete
61
+ # @return [Boolean]
62
+ def delete_schedule(campaign_id, schedule_id)
63
+ url = Util::Config.get('endpoints.base_url') +
64
+ sprintf(Util::Config.get('endpoints.campaign_schedule'), campaign_id, schedule_id)
65
+ url = build_url(url)
66
+ response = RestClient.delete(url, get_headers())
67
+ response.code == 204
68
+ end
69
+
70
+
71
+ # Update a specific schedule for a campaign
72
+ # @param [Integer] campaign_id - Campaign id to be scheduled
73
+ # @param [Schedule] schedule - Schedule to retrieve
74
+ # @return [Schedule]
75
+ def update_schedule(campaign_id, schedule)
76
+ url = Util::Config.get('endpoints.base_url') +
77
+ sprintf(Util::Config.get('endpoints.campaign_schedule'), campaign_id, schedule.id)
78
+ url = build_url(url)
79
+ payload = schedule.to_json
80
+ response = RestClient.put(url, payload, get_headers())
81
+ Components::Schedule.create(JSON.parse(response.body))
82
+ end
83
+
84
+
85
+ # Send a test send of a campaign
86
+ # @param [Integer] campaign_id - Id of campaign to send test of
87
+ # @param [TestSend] test_send - Test send details
88
+ # @return [TestSend]
89
+ def send_test(campaign_id, test_send)
90
+ url = Util::Config.get('endpoints.base_url') +
91
+ sprintf(Util::Config.get('endpoints.campaign_test_sends'), campaign_id)
92
+ url = build_url(url)
93
+ payload = test_send.to_json
94
+ response = RestClient.post(url, payload, get_headers())
95
+ Components::TestSend.create(JSON.parse(response.body))
96
+ end
97
+
98
+ end
99
+ end
100
+ end
101
+ end