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
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+ gem "oauth"
3
+ gem "json_pure"
4
+
5
+ group :development do
6
+ gem "fakeweb"
7
+ gem "rspec", "2.0.0.beta.22"
8
+ gem "yard", "~> 0.6.1"
9
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ fakeweb (1.3.0)
6
+ json_pure (1.4.6)
7
+ oauth (0.4.4)
8
+ rspec (2.0.0.beta.22)
9
+ rspec-core (= 2.0.0.beta.22)
10
+ rspec-expectations (= 2.0.0.beta.22)
11
+ rspec-mocks (= 2.0.0.beta.22)
12
+ rspec-core (2.0.0.beta.22)
13
+ rspec-expectations (2.0.0.beta.22)
14
+ diff-lcs (>= 1.1.2)
15
+ rspec-mocks (2.0.0.beta.22)
16
+ rspec-core (= 2.0.0.beta.22)
17
+ rspec-expectations (= 2.0.0.beta.22)
18
+ yard (0.6.3)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ fakeweb
25
+ json_pure
26
+ oauth
27
+ rspec (= 2.0.0.beta.22)
28
+ yard (~> 0.6.1)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 AWeber Communications, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ h1. AWeber API Gem
2
+
3
+ Official AWeber API gem.
4
+
5
+ h2. Installation
6
+
7
+ * @gem install aweber@
8
+
9
+ h2. Quick Start
10
+
11
+ First, go to "http://labs.aweber.com":http://labs.aweber.com and sign up for a free AWeber Labs account. This is how you register apps and get OAuth keys.
12
+
13
+ bc. # Register an app and use it's Consumer key and secret:
14
+ oauth = AWeber::OAuth.new("consumer_key", "consumer_secret")
15
+ # Go to the URL the following outputs.
16
+ oauth.request_token.authorize_url
17
+ # Authorize your account and copy the verification code.
18
+ oauth.authorize_with_verifier("verification_code")
19
+ aweber = AWeber::Base.new(oauth)
20
+ account = aweber.account
21
+ account.lists # Start getting data
22
+
23
+ Take a look at "examples":http://github.com/aweber/aweber-ruby/tree/master/examples/ for more examples.
24
+
25
+ h2. Getting Data
26
+
27
+ Every piece of data from AWeber's API stems from an Account object. From there you access associated resources in the same way you would an ActiveRecord accosication:
28
+
29
+ bc. account.lists #=> #<AWeber::Collection(AWeber::Resource::List)>
30
+ account.lists[1] #=> #<AWeber::Resource::List>
31
+ account.lists[1].web_forms #=> #<AWeber::Collection(AWeber::Resource::WebForm)>
32
+
33
+ h2. Note on Patches/Pull Requests
34
+
35
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
36
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
37
+ * Fork the project
38
+ * Start a feature/bugfix branch
39
+ * Commit and push until you are happy with your contribution
40
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
41
+ * Please try not to mess with the Rakefile or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
42
+
43
+ h2. Copyright
44
+
45
+ Copyright (c) 2010 AWeber Communications, Inc. See LICENSE for more detail.
@@ -0,0 +1,9 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new(:spec) do |s|
3
+ s.rspec_opts = ["--color"]
4
+ end
5
+
6
+ require 'yard'
7
+ YARD::Rake::YardocTask.new(:yard)
8
+
9
+ task :default => :spec
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "aweber"
4
+ s.version = "1.0.0"
5
+ s.platform = Gem::Platform::RUBY
6
+ s.summary = "Ruby interface to AWeber's API"
7
+ s.description = "Ruby interface to AWeber's API"
8
+
9
+ s.author = "AWeber Communications, Inc."
10
+ s.email = "help@aweber.com"
11
+ s.homepage = "http://github.com/aweber/AWeber-API-Ruby-Library"
12
+
13
+ s.require_path = "lib"
14
+ s.files = [
15
+ "aweber.gemspec",
16
+ "examples/with_access_token.rb",
17
+ "examples/your_account.rb",
18
+ "Gemfile",
19
+ "Gemfile.lock",
20
+ "lib/aweber.rb",
21
+ "lib/aweber/base.rb",
22
+ "lib/aweber/collection.rb",
23
+ "lib/aweber/oauth.rb",
24
+ "lib/aweber/resource.rb",
25
+ "lib/aweber/resources.rb",
26
+ "lib/aweber/resources/account.rb",
27
+ "lib/aweber/resources/broadcast.rb",
28
+ "lib/aweber/resources/click.rb",
29
+ "lib/aweber/resources/followup.rb",
30
+ "lib/aweber/resources/integration.rb",
31
+ "lib/aweber/resources/link.rb",
32
+ "lib/aweber/resources/list.rb",
33
+ "lib/aweber/resources/message.rb",
34
+ "lib/aweber/resources/open.rb",
35
+ "lib/aweber/resources/subscriber.rb",
36
+ "lib/aweber/resources/tracked_event.rb",
37
+ "lib/aweber/resources/web_form.rb",
38
+ "lib/aweber/resources/web_form_split_test.rb",
39
+ "lib/aweber/resources/web_form_split_test_component.rb",
40
+ "LICENSE",
41
+ "Rakefile",
42
+ "README.textile",
43
+ ]
44
+
45
+ s.test_files = Dir["spec/**/*"]
46
+ s.extra_rdoc_files = ["LICENSE", "README.textile"]
47
+
48
+ s.add_dependency "oauth"
49
+ s.add_dependency "json_pure"
50
+
51
+ s.add_development_dependency "fakeweb"
52
+ s.add_development_dependency "rspec", ">= 2.0.0"
53
+ s.add_development_dependency "yard", "~> 0.6.1"
54
+ end
55
+
@@ -0,0 +1,17 @@
1
+ # Getting started with saved Access Tokens
2
+ # ========================================
3
+ @oauth = AWeber::OAuth.new("consumer_key", "consumer_secret")
4
+
5
+ # Use your saved Access Token token and secret to authorize.
6
+ #
7
+ # This is also how you'd authorize your users' accounts after
8
+ # they've authorized your app to access their data for the
9
+ # first time.
10
+ @oauth.authorize_with_access("token", "secret")
11
+
12
+ # Go on your merry way.
13
+ @aweber = AWeber::Base.new(@oauth)
14
+ @account = @aweber.account
15
+ @account.lists
16
+ @account.lists[1].web_forms
17
+ # etc
@@ -0,0 +1,26 @@
1
+ # Getting started with your own AWeber Account
2
+ # ============================================
3
+
4
+ # First, go to http://labs.aweber.com, get a free AWeber Labs
5
+ # account and create an App.
6
+ @oauth = AWeber::OAuth.new("consumer_key", "consumer_secret")
7
+
8
+ # Go to URL outputed by the following, authorize your account
9
+ # and copy the verification code
10
+ @oauth.request_token.authorize_url
11
+ @oauth.authorize_with_verifier("verification_code")
12
+
13
+ # Save your Access Tokens for later use, you won't need
14
+ # to do all this once you have it. Check out
15
+ # with_access_tokens.rb for an example of this.
16
+ #
17
+ # DATASTORE << @oauth.access_token.token
18
+ # DATASTORE << @oauth.access_token.secret
19
+
20
+ @aweber = AWeber::Base.new(@oauth)
21
+ # An account is the root resource for all AWeber data
22
+ @account = @aweber.account
23
+
24
+ @account.lists
25
+ @account.lists[1].web_forms
26
+ # etc
@@ -0,0 +1,67 @@
1
+ require "forwardable"
2
+ require "oauth"
3
+ require "json/pure"
4
+
5
+ module AWeber
6
+ API_VERSION = "1.0".freeze
7
+ AUTH_VERSION = "1.0".freeze
8
+
9
+ # Used for +has_many+ and +has_one+ relationships.
10
+ #
11
+ INFLECTIONS = {
12
+ :accounts => :Account,
13
+ :clicks => :Click,
14
+ :links => :Link,
15
+ :lists => :List,
16
+ :messages => :Message,
17
+ :opens => :Open,
18
+ :subscribers => :Subscriber,
19
+ :tracked_events => :TrackedEvent,
20
+ :integrations => :Integration,
21
+ :web_forms => :WebForm,
22
+ :components => :WebFormSplitTestComponent,
23
+ :web_form_split_tests => :WebFormSplitTest,
24
+ :last_followup_sents => :Followup
25
+ }
26
+
27
+ class << self
28
+ # @param [String] Base URL of the API server
29
+ #
30
+ attr_accessor :api_endpoint
31
+
32
+ # @param [String] Base URL of the Auth server
33
+ #
34
+ attr_accessor :auth_endpoint
35
+
36
+ def api_url
37
+ File.join api_endpoint, API_VERSION
38
+ end
39
+
40
+ def auth_url
41
+ File.join auth_endpoint, AUTH_VERSION
42
+ end
43
+
44
+ # Retrieves the Resource class based on the
45
+ # +INFLECTIONS+ map and +name+.
46
+ #
47
+ # @param [Symbol] name Collection name
48
+ #
49
+ def get_class(name)
50
+ Resources.const_get(INFLECTIONS[name])
51
+ end
52
+ end
53
+
54
+ @api_endpoint = "https://api.aweber.com"
55
+ @auth_endpoint = "https://auth.aweber.com"
56
+
57
+ class OAuthError < Exception; end
58
+ class NotFoundError < Exception; end
59
+ class UnknownRequestError < Exception; end
60
+ end
61
+
62
+ $LOAD_PATH << File.dirname(__FILE__) unless $LOAD_PATH.include?(File.dirname(__FILE__))
63
+ require "aweber/oauth"
64
+ require "aweber/base"
65
+ require "aweber/resource"
66
+ require "aweber/resources"
67
+ require "aweber/collection"
@@ -0,0 +1,49 @@
1
+ module AWeber
2
+ class Base
3
+
4
+ def initialize(oauth)
5
+ @oauth = oauth
6
+ end
7
+
8
+ def account
9
+ accounts.first.last
10
+ end
11
+
12
+ private
13
+
14
+ def get(uri)
15
+ response = oauth.get(expand(uri))
16
+ handle_errors(response, uri)
17
+ parse(response) if response
18
+ end
19
+
20
+ def handle_errors(response, uri)
21
+ if response.is_a? Net::HTTPNotFound
22
+ raise NotFoundError, "Invalid resource uri.", caller
23
+ elsif response && response.body == "NotAuthorizedError"
24
+ raise OAuthError, "Could not authorize OAuth credentials.", caller
25
+ end
26
+ end
27
+
28
+ def accounts
29
+ @accounts ||= Collection.new(self, Resources::Account, get("/accounts"))
30
+ end
31
+
32
+ def expand(uri)
33
+ parsed = URI.parse(uri)
34
+ url = []
35
+ url << AWeber.api_endpoint unless parsed.host
36
+ url << API_VERSION unless parsed.path.include? API_VERSION
37
+ url << uri
38
+ File.join(*url)
39
+ end
40
+
41
+ def parse(response)
42
+ JSON.parse(response.body)
43
+ end
44
+
45
+ def oauth
46
+ @oauth
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,110 @@
1
+ module AWeber
2
+ # Collection objects are groups of Resources. Collections imitate
3
+ # regular Hashes in most ways. You can access Resource by +id+ via the
4
+ # +[]+ method.
5
+ #
6
+ # lists #=> #<AWeber::Collection ...>
7
+ # lists[1] #=> #<AWeber::Resources::List @id=1 ...>
8
+ #
9
+ # Also like Hashes, you can iterate over all of it's entries
10
+ #
11
+ # lists.each { |id, list| puts list.name }
12
+ #
13
+ # Collections support dynamic +find_by_*+ methods, where +*+ is an
14
+ # attribute and the first and only parameter is the value.
15
+ #
16
+ # lists.find_by_name("testlist")
17
+ # #=> #<AWeber::Resources::List @id=123, @name="testlist" ...>
18
+ #
19
+ # +find_by_*+ methods will also return a Hash of entries if more than
20
+ # one matches the criteria. The hash will be keyed by resource ID.
21
+ #
22
+ # messages.find_by_total_opens(0)
23
+ # #=> { "45697" => #<Message>, "12345" => #<Message> }
24
+ #
25
+ # Collections are paginated in groups of 20.
26
+ #
27
+ class Collection < Resource
28
+ include Enumerable
29
+
30
+ attr_reader :entries
31
+ attr_reader :next_collection_link
32
+ attr_reader :prev_collection_link
33
+ attr_reader :resource_type_link
34
+ attr_reader :total_size
35
+
36
+ alias_method :size, :total_size
37
+ alias_method :length, :total_size
38
+
39
+ # @param [AWeber::Base] client Instance of AWeber::Base
40
+ # @param [Class] klass Class to create entries of
41
+ # @param [Hash] data JSON decoded response data Hash
42
+ #
43
+ def initialize(client, klass, data={})
44
+ super client, data
45
+ @client = client
46
+ @klass = klass
47
+ @entries = {}
48
+ create_entries(data["entries"])
49
+ @_entries = @entries.to_a
50
+ end
51
+
52
+ def [](id)
53
+ @entries[id] ||= fetch_entry(id)
54
+ end
55
+
56
+ def each
57
+ (1..@total_size).each { |n| yield get_entry(n) }
58
+ end
59
+
60
+ def inspect
61
+ "#<AW::Collection(#{@klass.to_s}) size=\"#{size}\">"
62
+ end
63
+
64
+ private
65
+
66
+ def create_entries(entries)
67
+ entries.each { |entry| @entries[entry["id"]] = @klass.new(client, entry) }
68
+ end
69
+
70
+ def get_entry(n)
71
+ @_entries += fetch_next_group if needs_next_group?(n)
72
+ @_entries[n-1]
73
+ end
74
+
75
+ def fetch_entry(id)
76
+ @klass.new(client, get(File.join(base_path, id.to_s)))
77
+ end
78
+
79
+ def fetch_next_group(amount=20)
80
+ path = "#{ base_path }?ws.start=#{ @_entries.size }&ws.size=#{ amount }"
81
+ self.class.new(client, @klass, get(path)).entries.to_a
82
+ end
83
+
84
+ def needs_next_group?(current_index)
85
+ current_index == @_entries.size && current_index != @total_size
86
+ end
87
+
88
+ def base_path
89
+ URI.parse(@next_collection_link).path
90
+ end
91
+
92
+ def find_by(_attr, *args)
93
+ matches = select { |id, e| e.send(_attr) == args.first }
94
+ matches = matches.first.last if matches.size == 1
95
+ matches
96
+ end
97
+
98
+ def method_missing(method, *args)
99
+ method.to_s.scan /^find_by_(.+)/ do |_attr|
100
+ return find_by(_attr.first, *args)
101
+ end
102
+ super
103
+ end
104
+
105
+ def client
106
+ @client
107
+ end
108
+
109
+ end
110
+ end