aweber 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/Gemfile +9 -0
  2. data/Gemfile.lock +28 -0
  3. data/LICENSE +20 -0
  4. data/README.textile +45 -0
  5. data/Rakefile +9 -0
  6. data/aweber.gemspec +55 -0
  7. data/examples/with_access_token.rb +17 -0
  8. data/examples/your_account.rb +26 -0
  9. data/lib/aweber.rb +67 -0
  10. data/lib/aweber/base.rb +49 -0
  11. data/lib/aweber/collection.rb +110 -0
  12. data/lib/aweber/oauth.rb +69 -0
  13. data/lib/aweber/resource.rb +93 -0
  14. data/lib/aweber/resources.rb +14 -0
  15. data/lib/aweber/resources/account.rb +14 -0
  16. data/lib/aweber/resources/broadcast.rb +29 -0
  17. data/lib/aweber/resources/click.rb +13 -0
  18. data/lib/aweber/resources/followup.rb +24 -0
  19. data/lib/aweber/resources/integration.rb +8 -0
  20. data/lib/aweber/resources/link.rb +12 -0
  21. data/lib/aweber/resources/list.rb +86 -0
  22. data/lib/aweber/resources/message.rb +20 -0
  23. data/lib/aweber/resources/open.rb +10 -0
  24. data/lib/aweber/resources/subscriber.rb +26 -0
  25. data/lib/aweber/resources/tracked_event.rb +13 -0
  26. data/lib/aweber/resources/web_form.rb +14 -0
  27. data/lib/aweber/resources/web_form_split_test.rb +11 -0
  28. data/lib/aweber/resources/web_form_split_test_component.rb +20 -0
  29. data/spec/base_spec.rb +23 -0
  30. data/spec/collection_spec.rb +40 -0
  31. data/spec/fixtures/account.json +8 -0
  32. data/spec/fixtures/accounts.json +13 -0
  33. data/spec/fixtures/campaign.json +23 -0
  34. data/spec/fixtures/campaigns.json +106 -0
  35. data/spec/fixtures/click.json +9 -0
  36. data/spec/fixtures/clicks.json +14 -0
  37. data/spec/fixtures/integration.json +8 -0
  38. data/spec/fixtures/integrations.json +45 -0
  39. data/spec/fixtures/link.json +10 -0
  40. data/spec/fixtures/links.json +75 -0
  41. data/spec/fixtures/list.json +11 -0
  42. data/spec/fixtures/lists.json +38 -0
  43. data/spec/fixtures/message.json +12 -0
  44. data/spec/fixtures/messages.json +29 -0
  45. data/spec/fixtures/open.json +8 -0
  46. data/spec/fixtures/opens.json +21 -0
  47. data/spec/fixtures/subscriber.json +16 -0
  48. data/spec/fixtures/subscribers.json +69 -0
  49. data/spec/fixtures/tracked_event.json +8 -0
  50. data/spec/fixtures/tracked_events.json +21 -0
  51. data/spec/fixtures/web_form.json +14 -0
  52. data/spec/fixtures/web_form_split_test.json +9 -0
  53. data/spec/fixtures/web_form_split_test_component.json +16 -0
  54. data/spec/fixtures/web_form_split_test_components.json +37 -0
  55. data/spec/fixtures/web_form_split_tests.json +41 -0
  56. data/spec/fixtures/web_forms.json +229 -0
  57. data/spec/oauth_spec.rb +96 -0
  58. data/spec/resource_spec.rb +61 -0
  59. data/spec/resources/account_spec.rb +13 -0
  60. data/spec/resources/campaign_spec.rb +14 -0
  61. data/spec/resources/click_spec.rb +14 -0
  62. data/spec/resources/integration_spec.rb +13 -0
  63. data/spec/resources/link_spec.rb +15 -0
  64. data/spec/resources/list_spec.rb +26 -0
  65. data/spec/resources/message_spec.rb +17 -0
  66. data/spec/resources/open_spec.rb +13 -0
  67. data/spec/resources/subscriber_spec.rb +25 -0
  68. data/spec/resources/tracked_event_spec.rb +14 -0
  69. data/spec/resources/web_form_spec.rb +19 -0
  70. data/spec/resources/web_form_split_test_component_spec.rb +20 -0
  71. data/spec/resources/web_form_split_test_spec.rb +14 -0
  72. data/spec/spec_helper.rb +52 -0
  73. metadata +247 -0
@@ -0,0 +1,69 @@
1
+ module AWeber
2
+ class OAuth
3
+ extend Forwardable
4
+
5
+ def_delegators :access_token, :get, :post, :put, :delete
6
+
7
+ attr_accessor :callback_url
8
+
9
+ def initialize(consumer_token, consumer_secret, options={})
10
+ @consumer_token = consumer_token
11
+ @consumer_secret = consumer_secret
12
+ @callback_url = options[:callback_url]
13
+ end
14
+
15
+ def consumer
16
+ @consumer ||= ::OAuth::Consumer.new(@consumer_token, @consumer_secret, {
17
+ :site => AWeber.auth_endpoint,
18
+ :request_token_path => "/#{AWeber::AUTH_VERSION}/oauth/request_token",
19
+ :authorize_path => "/#{AWeber::AUTH_VERSION}/oauth/authorize",
20
+ :access_token_path => "/#{AWeber::AUTH_VERSION}/oauth/access_token",
21
+ :scheme => :query_string
22
+ })
23
+ end
24
+
25
+ # Retrieve a Request Token or simply return it, if it already exists.
26
+ #
27
+ # @param [Hash] options OAuth parameters. +:oauth_callback+ for example
28
+ #
29
+ def request_token(options={})
30
+ @request_token ||= consumer.get_request_token(options)
31
+ end
32
+
33
+ # Get an Access Token from a Request Token using the
34
+ # +verifier+ code.
35
+ #
36
+ # @param [String] verifier Verification code retrieved from
37
+ # user authentication
38
+ #
39
+ def authorize_with_verifier(verifier)
40
+ token = request_token.get_access_token(:oauth_verifier => verifier)
41
+ @access_token_key = token.token
42
+ @access_token_secret = token.secret
43
+ access_token
44
+ end
45
+
46
+ def access_token
47
+ @access_token ||= ::OAuth::AccessToken.new(consumer, @access_token_key,
48
+ @access_token_secret)
49
+ end
50
+
51
+ # Authorize with an Access Token key and secret you've previously
52
+ # retrieved via a user authentication.
53
+ #
54
+ # @param [String] access_token_key Access Token key
55
+ # @param [String] access_token_secret Access Token secret
56
+ #
57
+ def authorize_with_access(access_token_key, access_token_secret)
58
+ @access_token_key = access_token_key
59
+ @access_token_secret = access_token_secret
60
+ end
61
+
62
+ # Remove any active token instances and start over
63
+ #
64
+ def clear_tokens
65
+ @request_token = nil
66
+ @access_token = nil
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,93 @@
1
+ module AWeber
2
+ # Base class for all entries. All resources have the following attributes:
3
+ #
4
+ # - +id+
5
+ # - +http_etag+ (aliased as +etag+)
6
+ # - +self_link+ (aliased as +link+)
7
+ # - +resource_type_link+ (aliased as +resource_type+)
8
+ #
9
+ # Resources also have attributes based on their the type of resource it is.
10
+ # To see a full list of attributes per resource, visit
11
+ # http://labs.aweber.com/docs/reference/1.0/reference.
12
+ #
13
+ class Resource
14
+ extend Forwardable
15
+ include Comparable
16
+
17
+ def_delegators :client, :get
18
+
19
+ class << self
20
+ # Works the same as +alias_method+, but for attributes created via
21
+ # +attr*+ methods.
22
+ #
23
+ def alias_attribute(alias_, attribute)
24
+ alias_method alias_, attribute
25
+ end
26
+
27
+ def has_one(name)
28
+ define_method(name) do
29
+ ivar = instance_variable_get("@#{name}")
30
+ return ivar if ivar
31
+
32
+ resource_link = instance_variable_get("@#{name}_link")
33
+ klass = AWeber.get_class(:"#{name}s")
34
+ collection = klass.new(client, get(resource_link))
35
+ instance_variable_set("@#{name}", collection)
36
+ end
37
+ end
38
+
39
+ # Creates a lazy loaded method which will retrieve and create a
40
+ # collection of +name+ of objects.
41
+ #
42
+ # @param [Symbol] name name of the collection method
43
+ # @return [AWeber::Collection] Collection of +name+ objects
44
+ #
45
+ # @example
46
+ # class Account
47
+ # attr_reader :lists_collection_link
48
+ # collection :lists
49
+ # end
50
+ #
51
+ # #=> Account.new(...).lists #=> <AWeber::Collection>
52
+ #
53
+ def has_many(name)
54
+ define_method(name) do
55
+ ivar = instance_variable_get("@#{name}")
56
+ return ivar if ivar
57
+
58
+ resource_link = instance_variable_get("@#{name}_collection_link")
59
+ klass = AWeber.get_class(name)
60
+ collection = Collection.new(client, klass, get(resource_link))
61
+ instance_variable_set("@#{name}", collection)
62
+ end
63
+ end
64
+ end
65
+
66
+ attr_reader :id
67
+ attr_reader :http_etag
68
+ attr_reader :self_link
69
+ attr_reader :resource_type_link
70
+
71
+ alias_attribute :etag, :http_etag
72
+ alias_attribute :link, :self_link
73
+ alias_attribute :resource_type, :resource_type_link
74
+
75
+ def initialize(client, data={})
76
+ @client = client
77
+ data.each do |key, value|
78
+ instance_variable_set("@#{key}", value) if respond_to? key
79
+ end
80
+ end
81
+
82
+ def <=>(other)
83
+ @id <=> other.id
84
+ end
85
+
86
+ private
87
+
88
+ def client
89
+ @client
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,14 @@
1
+ require "aweber/resources/account"
2
+ require "aweber/resources/broadcast"
3
+ require "aweber/resources/click"
4
+ require "aweber/resources/followup"
5
+ require "aweber/resources/link"
6
+ require "aweber/resources/list"
7
+ require "aweber/resources/message"
8
+ require "aweber/resources/open"
9
+ require "aweber/resources/subscriber"
10
+ require "aweber/resources/tracked_event"
11
+ require "aweber/resources/integration"
12
+ require "aweber/resources/web_form"
13
+ require "aweber/resources/web_form_split_test"
14
+ require "aweber/resources/web_form_split_test_component"
@@ -0,0 +1,14 @@
1
+ module AWeber
2
+ module Resources
3
+ class Account < Resource
4
+ attr_reader :lists_collection_link
5
+ attr_reader :integrations_collection_link
6
+
7
+ has_many :lists
8
+ has_many :integrations
9
+
10
+ alias_attribute :etag, :http_etag
11
+ alias_attribute :uri, :self_link
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ module AWeber
2
+ module Resources
3
+ class Broadcast < Resource
4
+ attr_reader :click_tracking_enabled
5
+ attr_reader :content_type
6
+ attr_reader :is_archived
7
+ attr_reader :scheduled_at
8
+ attr_reader :sent_at
9
+ attr_reader :spam_assassin_score
10
+ attr_reader :subject
11
+ attr_reader :total_clicks
12
+ attr_reader :total_opens
13
+ attr_reader :total_sent
14
+ attr_reader :total_spam_complaints
15
+ attr_reader :total_undelivered
16
+ attr_reader :total_unsubscribes
17
+ attr_reader :twitter_account_link
18
+
19
+ attr_reader :links_collection_link
20
+ attr_reader :messages_collection_link
21
+
22
+ has_many :links
23
+ has_many :messages
24
+
25
+ alias_attribute :is_archived?, :is_archived
26
+ alias_attribute :is_click_tracking_enabled?, :click_tracking_enabled
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module AWeber
2
+ module Resources
3
+ class Click < Resource
4
+ attr_reader :event_time
5
+ attr_reader :is_earliest
6
+ attr_reader :subscriber_link
7
+
8
+ has_one :subscriber
9
+
10
+ alias_attribute :is_earliest?, :is_earliest
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ module AWeber
2
+ module Resources
3
+ class Followup < Resource
4
+ attr_reader :click_tracking_enabled
5
+ attr_reader :content_type
6
+ attr_reader :message_interval
7
+ attr_reader :message_number
8
+ attr_reader :spam_assassin_score
9
+ attr_reader :subject
10
+ attr_reader :total_clicked
11
+ attr_reader :total_opened
12
+ attr_reader :total_sent
13
+ attr_reader :total_spam_complaints
14
+ attr_reader :total_undelivered
15
+ attr_reader :total_unsubscribes
16
+
17
+ attr_reader :links_collection_link
18
+ attr_reader :messages_collection_link
19
+
20
+ has_many :links
21
+ has_many :messages
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,8 @@
1
+ module AWeber
2
+ module Resources
3
+ class Integration < Resource
4
+ attr_reader :login
5
+ attr_reader :service_name
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module AWeber
2
+ module Resources
3
+ class Link < Resource
4
+ attr_reader :total_clicks
5
+ attr_reader :total_unique_clicks
6
+ attr_reader :url
7
+ attr_reader :clicks_collection_link
8
+
9
+ has_many :clicks
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,86 @@
1
+ module AWeber
2
+ module Resources
3
+ class List < Resource
4
+ FOLLOWUP_TYPE_LINK = File.join(AWeber.api_url, "#followup_campaign")
5
+ BROADCAST_TYPE_LINK = File.join(AWeber.api_url, "#broadcast_campaign")
6
+
7
+ attr_reader :name
8
+
9
+ attr_reader :campaigns_collection_link
10
+ attr_reader :subscribers_collection_link
11
+ attr_reader :web_forms_collection_link
12
+ attr_reader :web_form_split_tests_collection_link
13
+
14
+ has_many :subscribers
15
+ has_many :web_forms
16
+ has_many :web_form_split_tests
17
+
18
+ def initialize(*args)
19
+ super(*args)
20
+ @campaigns = {}
21
+ @followups = nil
22
+ @broadcasts = nil
23
+ end
24
+
25
+ def campaigns
26
+ return @campaigns unless @campaigns == {}
27
+ create_followups
28
+ create_broadcasts
29
+ populate_campaigns
30
+ @campaigns
31
+ end
32
+
33
+ def broadcasts
34
+ campaigns if @broadcasts.nil?
35
+ @broadcasts
36
+ end
37
+
38
+ def followups
39
+ campaigns if @followups.nil?
40
+ @followups
41
+ end
42
+
43
+ private
44
+
45
+ def create_followups
46
+ followups = _entries.select { |entry| is_followup? entry }
47
+ @followups = Collection.new(client, Followup, {
48
+ "entries" => followups,
49
+ "total_size" => followups.length,
50
+ "resource_type_link" => FOLLOWUP_TYPE_LINK
51
+ })
52
+ end
53
+
54
+ def create_broadcasts
55
+ broadcasts = _entries.select { |entry| is_broadcast? entry }
56
+ @broadcasts = Collection.new(client, Broadcast, {
57
+ "entries" => broadcasts,
58
+ "total_size" => broadcasts.length,
59
+ "resource_type_link" => BROADCAST_TYPE_LINK
60
+ })
61
+ end
62
+
63
+ def populate_campaigns
64
+ @followups.each { |id, followup| @campaigns[id] = followup }
65
+ @broadcasts.each { |id, broadcast| @campaigns[id] = broadcast }
66
+ end
67
+
68
+ def is_followup?(entry)
69
+ entry["resource_type_link"].split("#").last =~ /^followup/
70
+ end
71
+
72
+ def is_broadcast?(entry)
73
+ entry["resource_type_link"].split("#").last =~ /^broadcast/
74
+ end
75
+
76
+ def _campaigns
77
+ @_campaigns ||= get(@campaigns_collection_link)
78
+ end
79
+
80
+ def _entries
81
+ _campaigns["entries"]
82
+ end
83
+
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,20 @@
1
+ module AWeber
2
+ module Resources
3
+ class Message < Resource
4
+ attr_reader :event_time
5
+ attr_reader :last_opened
6
+ attr_reader :total_opens
7
+
8
+ attr_reader :subscriber_link
9
+ attr_reader :tracked_events_collection_link
10
+ attr_reader :opens_collection_link
11
+
12
+ has_one :subscriber
13
+
14
+ has_many :tracked_events
15
+ has_many :opens
16
+
17
+ alias_attribute :events, :tracked_events
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ module AWeber
2
+ module Resources
3
+ class Open < Resource
4
+ attr_reader :event_time
5
+ attr_reader :subscriber_link
6
+
7
+ has_one :subscriber
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,26 @@
1
+ module AWeber
2
+ module Resources
3
+ class Subscriber < Resource
4
+ attr_reader :ad_tracking
5
+ attr_reader :email
6
+ attr_reader :ip_address
7
+ attr_reader :is_verified
8
+ attr_reader :last_followup_sent_at
9
+ attr_reader :misc_notes
10
+ attr_reader :name
11
+ attr_reader :status
12
+ attr_reader :subscribed_at
13
+ attr_reader :subscription_method
14
+ attr_reader :subscription_url
15
+ attr_reader :unsubscribed_at
16
+ attr_reader :verified_at
17
+
18
+ attr_reader :last_followup_sent_link
19
+
20
+ has_one :last_followup_sent
21
+
22
+ alias_attribute :is_verified?, :is_verified
23
+ alias_attribute :notes, :misc_notes
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ module AWeber
2
+ module Resources
3
+ class TrackedEvent < Resource
4
+ attr_reader :event_time
5
+ attr_reader :type
6
+ attr_reader :subscriber_link
7
+
8
+ has_one :subscriber
9
+
10
+ alias_attribute :time, :event_time
11
+ end
12
+ end
13
+ end