locaweb-emailmarketing 0.0.1

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.
Files changed (60) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +4 -0
  4. data/README.md +133 -0
  5. data/Rakefile +1 -0
  6. data/lib/locaweb-emailmarketing.rb +12 -0
  7. data/lib/locaweb-emailmarketing/adapters.rb +1 -0
  8. data/lib/locaweb-emailmarketing/adapters/http_request_adapter.rb +38 -0
  9. data/lib/locaweb-emailmarketing/client.rb +60 -0
  10. data/lib/locaweb-emailmarketing/clients.rb +14 -0
  11. data/lib/locaweb-emailmarketing/clients/account_client.rb +22 -0
  12. data/lib/locaweb-emailmarketing/clients/base_client.rb +52 -0
  13. data/lib/locaweb-emailmarketing/clients/campaign_client.rb +13 -0
  14. data/lib/locaweb-emailmarketing/clients/contact_client.rb +12 -0
  15. data/lib/locaweb-emailmarketing/clients/contact_import_client.rb +19 -0
  16. data/lib/locaweb-emailmarketing/clients/custom_field_client.rb +12 -0
  17. data/lib/locaweb-emailmarketing/clients/domain_client.rb +12 -0
  18. data/lib/locaweb-emailmarketing/clients/list_client.rb +12 -0
  19. data/lib/locaweb-emailmarketing/clients/message_client.rb +13 -0
  20. data/lib/locaweb-emailmarketing/clients/report_client.rb +41 -0
  21. data/lib/locaweb-emailmarketing/clients/sender_client.rb +15 -0
  22. data/lib/locaweb-emailmarketing/clients/template_client.rb +12 -0
  23. data/lib/locaweb-emailmarketing/clients/unsubscribe_reason_client.rb +13 -0
  24. data/lib/locaweb-emailmarketing/lib/hash.rb +9 -0
  25. data/lib/locaweb-emailmarketing/libs.rb +1 -0
  26. data/lib/locaweb-emailmarketing/version.rb +5 -0
  27. data/locaweb-emailmarketing.gemspec +30 -0
  28. data/spec/fixtures/cassettes/accounts_all.yml +74 -0
  29. data/spec/fixtures/cassettes/accounts_get.yml +77 -0
  30. data/spec/fixtures/cassettes/accounts_update.yml +124 -0
  31. data/spec/fixtures/cassettes/custom_field_all.yml +150 -0
  32. data/spec/fixtures/cassettes/custom_field_create.yml +263 -0
  33. data/spec/fixtures/cassettes/custom_field_destroy.yml +187 -0
  34. data/spec/fixtures/cassettes/custom_field_get.yml +146 -0
  35. data/spec/fixtures/cassettes/custom_field_update.yml +265 -0
  36. data/spec/fixtures/cassettes/reports_bounces.yml +75 -0
  37. data/spec/fixtures/cassettes/reports_clicks.yml +74 -0
  38. data/spec/fixtures/cassettes/reports_links.yml +73 -0
  39. data/spec/fixtures/cassettes/reports_openings.yml +74 -0
  40. data/spec/fixtures/cassettes/reports_overview.yml +75 -0
  41. data/spec/fixtures/cassettes/reports_uniq_openings.yml +74 -0
  42. data/spec/locaweb-emailmarketing/adapters/http_request_adapter_spec.rb +63 -0
  43. data/spec/locaweb-emailmarketing/client_spec.rb +14 -0
  44. data/spec/locaweb-emailmarketing/clients/account_client_spec.rb +37 -0
  45. data/spec/locaweb-emailmarketing/clients/base_client_spec.rb +77 -0
  46. data/spec/locaweb-emailmarketing/clients/campaign_client_spec.rb +10 -0
  47. data/spec/locaweb-emailmarketing/clients/contact_client_spec.rb +10 -0
  48. data/spec/locaweb-emailmarketing/clients/contact_import_client_spec.rb +9 -0
  49. data/spec/locaweb-emailmarketing/clients/custom_field_client_spec.rb +10 -0
  50. data/spec/locaweb-emailmarketing/clients/domains_client_spec.rb +10 -0
  51. data/spec/locaweb-emailmarketing/clients/list_client_spec.rb +10 -0
  52. data/spec/locaweb-emailmarketing/clients/message_client_spec.rb +10 -0
  53. data/spec/locaweb-emailmarketing/clients/report_client_spec.rb +70 -0
  54. data/spec/locaweb-emailmarketing/clients/sender_client_spec.rb +9 -0
  55. data/spec/locaweb-emailmarketing/clients/template_client_spec.rb +10 -0
  56. data/spec/locaweb-emailmarketing/clients/unsubscribe_reason_client_spec.rb +10 -0
  57. data/spec/locaweb-emailmarketing/lib/hash_spec.rb +23 -0
  58. data/spec/spec_helper.rb +21 -0
  59. data/spec/support/base_client_examples.rb +100 -0
  60. metadata +193 -0
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in locaweb-emailmarketing.gemspec
4
+ gemspec
@@ -0,0 +1,133 @@
1
+ locaweb-emailmarketing
2
+ ======================
3
+
4
+ # Description
5
+
6
+ API client for [Locaweb Email Marketing](http://www.locaweb.com.br/produtos/email-marketing/planos.html)
7
+
8
+ Trial account: [http://emailmarketing.locaweb.com.br/trial](http://emailmarketing.locaweb.com.br/trial)
9
+
10
+ # How to use
11
+
12
+ ## Client
13
+ <pre>
14
+ client = Locaweb::Emailmarketing::Client.new auth_token: "TOKEN",
15
+ base_url: 'https://emailmarketing.locaweb.com.br/api/v1',
16
+ account_id: 'ID'
17
+ </pre>
18
+ ## Accounts
19
+ - list: client.accounts.all
20
+ - get: client.acccounts.get("ID")
21
+ - update: client.accounts.update("ID", hash_options)<br />
22
+ available options: return_path_domain
23
+
24
+ ## Campaigns
25
+ - list: client.campaigns.all
26
+ - get: client.campaigns.get("ID")
27
+ - create: client.campaigns.create(options)<br />
28
+ available options: name, description
29
+ - update: client.campaigns.update("ID", options)<br />
30
+ available options: name, description
31
+ - destroy: client.campaigns.destroy("ID")
32
+
33
+ ## Contact Imports
34
+ - list: client.contact_imports.all
35
+ - get: client.contact_imports.get("ID")
36
+ - create: client.contact_imports.create(options)<br />
37
+ available options: required: [:name, :list_tokens, :url], optional: [:description]
38
+
39
+ ## Contacts
40
+ - list: client.contacts.all
41
+ - get: client.contacts.get("ID")
42
+ - create: client.contacts.create(options)<br />
43
+ available options: required: [], optional: [:custom_fields, :list_tokens]
44
+ - update: client.contacts.update("ID", options)<br />
45
+ available options: required: [:email, :list_ids], optional: [:custom_fields]
46
+ - destroy: client.contacts.destroy("ID")
47
+
48
+ ## Custom Fields
49
+ - list: client.custom_fields.all
50
+ - get: client.custom_fields.get("ID")
51
+ - create: client.custom_fields.create(options)<br />
52
+ available options: name, type
53
+ - update: client.custom_fields.update("ID", options)<br />
54
+ available options: name
55
+ - destroy: client.custom_fields.destroy("ID")
56
+
57
+ ## Domains
58
+ - list: client.domains.all
59
+ - get: client.domains.get("ID")
60
+ - create: client.domains.create(options)<br />
61
+ available options: required: [:name]
62
+ - update: client.domains.update("ID", options)<br />
63
+ available options: required: [:name, :default]
64
+ - destroy: client.domains.destroy("ID")
65
+
66
+ ## Lists
67
+ - list: client.lists.all
68
+ - get: client.lists.get("ID")
69
+ - create: client.lists.create(options)<br />
70
+ available options: name
71
+ - update: client.lists.update("ID", options)<br />
72
+ available options: name
73
+ - destroy: client.lists.destroy("ID")
74
+
75
+ ## Messages
76
+ - list: client.messages.all
77
+ - get: client.messages.get("ID")
78
+ - create: client.messages.create(options)<br />
79
+ Example:
80
+ <pre>
81
+ client.messages.create({
82
+ domain_id: "50f8e28abf8d79f935000002",
83
+ html_body: "lala popo",
84
+ list_ids: ["50f95cbd38a582091f002907"],
85
+ name: "lala",
86
+ sender_name: "name",
87
+ sender: "trial@emailmarketing.locaweb.com.br",
88
+ subject: "teste lala popo",
89
+ scheduled_to: "2013-01-20"
90
+ })
91
+ </pre>
92
+ - update: client.messages.update("ID", options)<br />
93
+ Example:
94
+ <pre>
95
+ client.messages.update("ID", {subject: "lalapopo2"})
96
+ </pre>
97
+ - destroy: client.messages.destroy("ID")
98
+
99
+ More info: [http://wiki.locaweb.com.br/pt-br/APIs_Novo_Email_Marketing/Gerenciar_Mensagens](http://wiki.locaweb.com.br/pt-br/APIs_Novo_Email_Marketing/Gerenciar_Mensagens)
100
+
101
+ ## Reports
102
+ - overview: client.messages.overview("ID")
103
+ - openings: client.messages.openings("ID")
104
+ - uniq_openings: client.messages.uniq_openings("ID")
105
+ - links: client.messages.links("ID")
106
+ - clicks: client.messages.clicks("ID")
107
+ - bounces: client.messages.bounces("ID")
108
+
109
+ More info: [http://wiki.locaweb.com.br/pt-br/APIs_Novo_Email_Marketing/Gerenciar_Relatorios](http://wiki.locaweb.com.br/pt-br/APIs_Novo_Email_Marketing/Gerenciar_Relatorios)
110
+
111
+ ## Senders
112
+ - list: client.senders.all
113
+ - get: client.senders.get("ID")
114
+ - create: client.senders.create(options)<br />
115
+ available options: email
116
+ - destroy: client.senders.destroy("ID")
117
+
118
+ ## Unsubscribe Reasons
119
+ - list: client.unsubscribe_reasons.all
120
+ - get: client.unsubscribe_reasons.get("ID")
121
+ - create: client.unsubscribe_reasons.create(options)<br />
122
+ available options: name
123
+ - update: client.unsubscribe_reasons.update("ID", options)<br />
124
+ available options: name
125
+ - destroy: client.unsubscribe_reasons.destroy("ID")
126
+
127
+ ## Other
128
+
129
+ ### Pagination
130
+ To return a specif page, pass param to method <i>all</i> like this:
131
+ <pre>
132
+ client.contacts.all(4)
133
+ </pre>
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,12 @@
1
+ require 'json'
2
+ require 'active_support/core_ext'
3
+ require "locaweb-emailmarketing/libs"
4
+ require "locaweb-emailmarketing/adapters"
5
+ require "locaweb-emailmarketing/version"
6
+ require "locaweb-emailmarketing/clients"
7
+
8
+ module Locaweb
9
+ module Emailmarketing
10
+ # Your code goes here...
11
+ end
12
+ end
@@ -0,0 +1 @@
1
+ require "locaweb-emailmarketing/adapters/http_request_adapter"
@@ -0,0 +1,38 @@
1
+ require 'rest_client'
2
+
3
+ module Locaweb
4
+ module Emailmarketing
5
+ class HttpRequestAdapter
6
+ def initialize(options)
7
+ options.assert_required_keys(required: [:auth_token, :base_url])
8
+ @options = options
9
+ end
10
+
11
+ def get uri
12
+ begin
13
+ JSON RestClient.get "#{@options[:base_url]}/#{uri}",
14
+ { "X-Auth-Token" => @options[:auth_token], accept: :json }
15
+ rescue RestClient::ResourceNotFound
16
+ nil
17
+ end
18
+ end
19
+
20
+ def put uri, attributes
21
+ RestClient.put "#{@options[:base_url]}/#{uri}",
22
+ attributes.to_json,
23
+ { "X-Auth-Token" => @options[:auth_token], content_type: :json }
24
+ end
25
+
26
+ def post uri, attributes
27
+ RestClient.post "#{@options[:base_url]}/#{uri}",
28
+ attributes.to_json,
29
+ { "X-Auth-Token" => @options[:auth_token], content_type: :json }
30
+ end
31
+
32
+ def delete uri
33
+ RestClient.delete "#{@options[:base_url]}/#{uri}",
34
+ { "X-Auth-Token" => @options[:auth_token], accept: :json }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,60 @@
1
+ module Locaweb
2
+ module Emailmarketing
3
+ class Client
4
+ def initialize(options = {})
5
+ options.assert_required_keys(required: [:account_id, :auth_token, :base_url])
6
+ @account_id = options[:account_id]
7
+ options.delete(:account_id)
8
+ @http_request_adapter = HttpRequestAdapter.new options
9
+ end
10
+
11
+ def accounts
12
+ @account ||= AccountClient.new @http_request_adapter
13
+ end
14
+
15
+ def custom_fields
16
+ @custom_fields ||= CustomFieldClient.new @http_request_adapter, @account_id
17
+ end
18
+
19
+ def campaigns
20
+ @campaigns ||= CampaignClient.new @http_request_adapter, @account_id
21
+ end
22
+
23
+ def contact_imports
24
+ @contact_imports ||= ContactImportClient.new @http_request_adapter, @account_id
25
+ end
26
+
27
+ def lists
28
+ @lists ||= ListClient.new @http_request_adapter, @account_id
29
+ end
30
+
31
+ def contacts
32
+ @contacts ||= ContactClient.new @http_request_adapter, @account_id
33
+ end
34
+
35
+ def domains
36
+ @domains ||= DomainClient.new @http_request_adapter, @account_id
37
+ end
38
+
39
+ def messages
40
+ @messages ||= MessageClient.new @http_request_adapter, @account_id
41
+ end
42
+
43
+ def reports
44
+ @reports ||= ReportClient.new @http_request_adapter, @account_id
45
+ end
46
+
47
+ def senders
48
+ @senders ||= SenderClient.new @http_request_adapter, @account_id
49
+ end
50
+
51
+ def unsubscribe_reasons
52
+ @unsubscribe_reasons ||= UnsubscribeReasonClient.new @http_request_adapter, @account_id
53
+ end
54
+
55
+ def templates
56
+ @templates ||= TemplateClient.new @http_request_adapter, @account_id
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,14 @@
1
+ require "locaweb-emailmarketing/client"
2
+ require "locaweb-emailmarketing/clients/base_client"
3
+ require 'locaweb-emailmarketing/clients/account_client'
4
+ require 'locaweb-emailmarketing/clients/campaign_client'
5
+ require 'locaweb-emailmarketing/clients/contact_client'
6
+ require 'locaweb-emailmarketing/clients/contact_import_client'
7
+ require 'locaweb-emailmarketing/clients/custom_field_client'
8
+ require 'locaweb-emailmarketing/clients/domain_client'
9
+ require 'locaweb-emailmarketing/clients/list_client'
10
+ require 'locaweb-emailmarketing/clients/message_client'
11
+ require 'locaweb-emailmarketing/clients/report_client'
12
+ require 'locaweb-emailmarketing/clients/sender_client'
13
+ require 'locaweb-emailmarketing/clients/unsubscribe_reason_client'
14
+ require 'locaweb-emailmarketing/clients/template_client'
@@ -0,0 +1,22 @@
1
+ module Locaweb
2
+ module Emailmarketing
3
+ class AccountClient
4
+ def initialize(http_request_adapter)
5
+ @http_request_adapter = http_request_adapter
6
+ end
7
+
8
+ def all
9
+ @http_request_adapter.get "accounts"
10
+ end
11
+
12
+ def get(id)
13
+ @http_request_adapter.get "accounts/#{id}"
14
+ end
15
+
16
+ def update(id, attributes = {})
17
+ attributes.assert_required_keys(optional: [:return_path_domain])
18
+ @http_request_adapter.put "accounts/#{id}", account: attributes
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ module Locaweb
2
+ module Emailmarketing
3
+ class BaseClient
4
+ def initialize(http_request_adapter, account_id)
5
+ @http_request_adapter = http_request_adapter
6
+ @account_id = account_id
7
+ @resource_name = nil #in plural form, ex: campaigns
8
+ @required_keys_to_update = {optional: [], required: []}
9
+ @required_keys_to_create = {optional: [], required: []}
10
+ end
11
+
12
+ def all(page=1)
13
+ url = "accounts/#{@account_id}/#{@resource_name}"
14
+ url = "#{url}?page=#{page}" if page != 1
15
+ @http_request_adapter.get url
16
+ end
17
+
18
+ def get(id)
19
+ @http_request_adapter.get "accounts/#{@account_id}/#{@resource_name}/#{id}"
20
+ end
21
+
22
+ def create(attributes)
23
+ attributes.assert_required_keys(@required_keys_to_create)
24
+ response_json = JSON @http_request_adapter.post "accounts/#{@account_id}/#{@resource_name}", {@resource_name.singularize.to_sym => attributes}
25
+ response_json["id"]
26
+ end
27
+
28
+ def update(id, attributes)
29
+ attributes.assert_required_keys(@required_keys_to_update)
30
+ @http_request_adapter.put "accounts/#{@account_id}/#{@resource_name}/#{id}", {@resource_name.singularize.to_sym => attributes}
31
+ end
32
+
33
+ def destroy(id)
34
+ @http_request_adapter.delete "accounts/#{@account_id}/#{@resource_name}/#{id}"
35
+ end
36
+
37
+ private
38
+
39
+ def required_keys_to_update(required_keys_to_update)
40
+ @required_keys_to_update = required_keys_to_update
41
+ end
42
+
43
+ def required_keys_to_create(required_keys_to_create)
44
+ @required_keys_to_create = required_keys_to_create
45
+ end
46
+
47
+ def resource_name(resource_name)
48
+ @resource_name = resource_name
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,13 @@
1
+ module Locaweb
2
+ module Emailmarketing
3
+ class CampaignClient < BaseClient
4
+ def initialize(http_request_adapter, account_id)
5
+ super
6
+ resource_name "campaigns"
7
+ required_keys = {required: [:name], optional: [:description]}
8
+ required_keys_to_update required_keys
9
+ required_keys_to_create required_keys
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Locaweb
2
+ module Emailmarketing
3
+ class ContactClient < BaseClient
4
+ def initialize(http_request_adapter, account_id)
5
+ super
6
+ resource_name "contacts"
7
+ required_keys_to_update({required: [], optional: [:custom_fields, :list_tokens]})
8
+ required_keys_to_create({required: [:email, :list_ids], optional: [:custom_fields]})
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module Locaweb
2
+ module Emailmarketing
3
+ class ContactImportClient < BaseClient
4
+ def initialize(http_request_adapter, account_id)
5
+ super
6
+ resource_name "contact_imports"
7
+ required_keys_to_create ({ required: [:name, :list_tokens, :url], optional: [:description] })
8
+ end
9
+
10
+ def update(*args)
11
+ raise(NotImplementedError, "Unavailable action to this resource")
12
+ end
13
+
14
+ def destroy(*args)
15
+ raise(NotImplementedError, "Unavailable action to this resource")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module Locaweb
2
+ module Emailmarketing
3
+ class CustomFieldClient < BaseClient
4
+ def initialize(http_request_adapter, account_id)
5
+ super
6
+ resource_name "custom_fields"
7
+ required_keys_to_update({ required: [:name] })
8
+ required_keys_to_create({ required: [:name, :type] })
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Locaweb
2
+ module Emailmarketing
3
+ class DomainClient < BaseClient
4
+ def initialize(http_request_adapter, account_id)
5
+ super
6
+ resource_name "domains"
7
+ required_keys_to_update({required: [:name, :default], optional: []})
8
+ required_keys_to_create({required: [:name], optional: []})
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Locaweb
2
+ module Emailmarketing
3
+ class ListClient < BaseClient
4
+ def initialize(http_request_adapter, account_id)
5
+ super
6
+ @resource_name = "lists"
7
+ @required_keys_to_update = { required: [:name], optional: [:description] }
8
+ @required_keys_to_create = @required_keys_to_update
9
+ end
10
+ end
11
+ end
12
+ end