gibbon 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gibbon might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f3bc3884010b6bc30397d25c746ca29f6b026cf
4
- data.tar.gz: acf67934d1a27976e15324502773b0029978f3a9
3
+ metadata.gz: f67c204c24ccb1e88465da9491083990cda98d84
4
+ data.tar.gz: d7ee83c9782fdd6a9664a0e0e1095f82fa7d14cd
5
5
  SHA512:
6
- metadata.gz: 72ddec2ab87bdb95d5bcb25f0728eeece2a70daaf49a990444c2dfd4b97c28e093c07b644a0538383684e05482977ec8eeb7c4d4210e7d5e25aa8bd2710a5835
7
- data.tar.gz: a20ba104983c405b478dad32c3cc507521c80dd3b36e39cf1d208201bf526a1bc02eec889b370ad946b39a4f83988db648ed8de01f58e1a2dd9dd30d44ea7b72
6
+ metadata.gz: 4f298cdbe7fc035c0be9078e9ce9c0baa37f95d97ddaba121c55be7e3b9d5e95227f0fc228448aeb9e31417d7222a0ed4f91c1b4b1a0bfb06c2c33879c154ac1
7
+ data.tar.gz: b0c0012892ca2d80bb1f6ea012f03d7586510fa9ed6c965d1179a04f79cb17003469be6c5af71197ecb236659de7df57bce228b50e52753aeae20751c1f5a1cf
data/.travis.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
- before_install: gem install bundler -v 1.5.1
3
- install: bundle install --retry=3
2
+ sudo: false
3
+ cache: bundler
4
4
  rvm:
5
5
  - 2.0.0
6
6
  - 2.1.5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## [Unreleased][unreleased]
2
2
 
3
+ ## [2.1.0] - 2015-10-12
4
+ - Upsert support
5
+
3
6
  ## [2.0.0] - 2015-7-28
4
7
  - Support for API 3.0. Usage syntax has changed. Please check out the readme.
5
8
  - Update MultiJSON dependency to 1.11.0
data/README.markdown CHANGED
@@ -29,7 +29,7 @@ You can set an individual request's timeout like this:
29
29
  gibbon.timeout = 10
30
30
 
31
31
  Now you can make requests using the resources defined in [MailChimp's docs](http://kb.mailchimp.com/api/resources). Resource IDs
32
- are specified inline and a `CRUD` (`create`, `retrieve`, `update`, or `delete`) verb initiates the request.
32
+ are specified inline and a `CRUD` (`create`, `retrieve`, `update`, `upsert`, or `delete`) verb initiates the request. `upsert` lets you update a record, if it exists, or insert it otherwise where supported by MailChimp's API.
33
33
 
34
34
  gibbon.lists.retrieve
35
35
 
@@ -60,45 +60,80 @@ Assuming you've set an `api_key` on Gibbon, you can conveniently make API calls
60
60
 
61
61
  You can also set the environment variable `MAILCHIMP_API_KEY` and Gibbon will use it when you create an instance:
62
62
 
63
- gb = Gibbon::Request.new
63
+ gibbon = Gibbon::Request.new
64
64
 
65
65
  MailChimp's [resource documentation](http://kb.mailchimp.com/api/resources) is a list of available resources. Substitute an underscore if
66
66
  a resource name contains a hyphen.
67
67
 
68
68
  ### Fetching Campaigns
69
69
 
70
-
71
70
  campaigns = gb.campaigns.retrieve
72
71
 
73
72
  ### Fetching Lists
74
73
 
75
74
  Similarly, to fetch your lists
76
75
 
77
- lists = gb.lists.retrieve
76
+ lists = gibbon.lists.retrieve
78
77
 
79
78
  ### More Advanced Examples
80
79
 
81
80
  List subscribers for a list:
82
81
 
83
- gb.lists(list_id).members.retrieve
82
+ gibbon.lists(list_id).members.retrieve
84
83
 
85
84
  Subscribe a member to a list:
86
85
 
87
- gb.lists(list_id).members.create(body: {email_address: "email_address", status: "subscribed", merge_fields: {FNAME: "First Name", LNAME: "Last Name"}})
86
+ gibbon.lists(list_id).members.create(body: {email_address: "email_address", status: "subscribed", merge_fields: {FNAME: "First Name", LNAME: "Last Name"}})
87
+
88
+ Note: You could call `upsert` instead of `create` in the example above to update the member if they already exist or subscribe them if they do not.
88
89
 
89
90
  You can also unsubscribe a member from a list:
90
91
 
91
- gb.lists(list_id).members(member_id).update(body: { status: "unsubscribed" })
92
+ gibbon.lists(list_id).members(member_id).update(body: { status: "unsubscribed" })
92
93
 
93
94
  Fetch the number of opens for a campaign
94
95
 
95
- email_stats = gb.reports("13e9a94053").retrieve["opens"]
96
+ email_stats = gibbon.reports(campaign_id).retrieve["opens"]
96
97
 
97
98
  Overriding Gibbon's API endpoint (i.e. if using an access token from OAuth and have the `api_endpoint` from the [metadata](http://apidocs.mailchimp.com/oauth2/)):
98
99
 
99
100
  Gibbon::Request.api_endpoint = "https://us1.api.mailchimp.com"
100
101
  Gibbon::Request.api_key = your_access_token_or_api_key
101
102
 
103
+ ### Interests
104
+
105
+ Interests are a little more complicated than other parts of the API, so here's an example of how you would set interests during at subscription time or update them later. The ID of the interests you want to opt in or out of must be known ahead of time so an example of how to find interest IDs is also included.
106
+
107
+ Subscribing a member to a list with specific interests up front:
108
+
109
+ g.lists(list_id).members.create(body: {email_address: user_email_address, status: "subscribed", interests: {some_interest_id: true, another_interest_id: true}})
110
+
111
+ Updating a list member's interests:
112
+
113
+ gibbon.lists(list_id).members(member_id).update(body: {interests: {some_interest_id: true, another_interest_id: false}})
114
+
115
+ So how do we get the interest IDs? When you query the API for a specific list member's information:
116
+
117
+ g.lists(list_id).members(member_id).retrieve
118
+
119
+ The response looks someting like this (unrelated things removed):
120
+
121
+ {"id"=>"...", "email_address"=>"...", ..., "interests"=>{"3def637141"=>true, "f7cc4ee841"=>false, "fcdc951b9f"=>false, "3daf3cf27d"=>true, "293a3703ed"=>false, "72370e0d1f"=>false, "d434d21a1c"=>false, "bdb1ff199f"=>false, "a54e78f203"=>false, "c4527fd018"=>false} ...}
122
+
123
+ The API returns a map of interest ID to boolean value. Now we to get interest details so we know what these interest IDs map to. Looking at [this doc page](http://kb.mailchimp.com/api/resources/lists/interest-categories/interests/lists-interests-collection), we need to do this:
124
+
125
+ g.lists(list_id).interest_categories.retrieve
126
+
127
+ To get a list of interest categories. That gives us something like:
128
+
129
+ {"list_id"=>"...", "categories"=>[{"list_id"=>"...", "id"=>"0ace7aa498", "title"=>"Food Preferences", ...}] ...}
130
+
131
+ In this case, we're interested in the ID of the "Food Preferences" interest, which is `0ace7aa498`. Now we can fetch the details for this interest group:
132
+
133
+ g.lists(list_id).interest_categories("0ace7aa498").interests.retrieve
134
+
135
+ That response gives the interest data, including the ID for the interests themselves, which we can use to update a list member's interests or set them when we call the API to subscribe her or him to a list.
136
+
102
137
  ### Error handling
103
138
 
104
139
  Gibbon raises an error when the API returns an error.
@@ -106,6 +141,52 @@ Gibbon raises an error when the API returns an error.
106
141
  Gibbon::MailChimpError has the following attributes: `title`, `detail`, `body`, `raw_body`, `status_code`. Some or all of these may not be
107
142
  available depending on the nature of the error.
108
143
 
144
+ ### Migrating from Gibbon 1.x
145
+
146
+ Gibbon 2.x has different syntax from version 1.x. This is because Gibbon maps to MailChimp's API and because version 3 of the API is quite different from version 2. First, the name of the primary class has changed from `API` to `Request`. And the way you pass an API key during initialization is different. A few examples below.
147
+
148
+ #### Initialization
149
+
150
+ Gibbon 1.x:
151
+
152
+ gibbon = Gibbon::API.new("your_api_key")
153
+
154
+ Gibbon 2.x:
155
+
156
+ gibbon = Gibbon::Request.new(api_key: "your_api_key")
157
+
158
+ MailChimp API 3 is a RESTful API, so Gibbon's syntax now requires a trailing call to a verb, as described above.
159
+
160
+ #### Fetching Lists
161
+
162
+ Gibbon 1.x:
163
+
164
+ gibbon.lists.list
165
+
166
+ Gibbon 2.x:
167
+
168
+ gibbon.lists.retrieve
169
+
170
+ #### Fetching List Members
171
+
172
+ Gibbon 1.x:
173
+
174
+ gibbon.lists.members({:id => list_id})
175
+
176
+ Gibbon 2.x:
177
+
178
+ gibbon.lists(list_id).members.retrieve
179
+
180
+ #### Subscribing a Member to a List
181
+
182
+ Gibbon 1.x:
183
+
184
+ gibbon.lists.subscribe({:id => list_id, :email => {:email => "email_address"}, :merge_vars => {:FNAME => "Bob", :LNAME => "Smith"}})
185
+
186
+ Gibbon 2.x:
187
+
188
+ gibbon.lists(list_id).members.create(body: {email_address: "email_address", status: "subscribed", merge_fields: {FNAME: "Bob", LNAME: "Smith"}})
189
+
109
190
  ##Thanks
110
191
 
111
192
  Thanks to everyone who has [contributed](https://github.com/amro/gibbon/contributors) to Gibbon's development.
data/gibbon.gemspec CHANGED
@@ -30,5 +30,6 @@ Gem::Specification.new do |s|
30
30
 
31
31
  s.add_development_dependency 'rake'
32
32
  s.add_development_dependency "rspec", "3.2.0"
33
+ s.add_development_dependency 'webmock', '~> 1.21.0'
33
34
 
34
35
  end
@@ -29,6 +29,19 @@ module Gibbon
29
29
  handle_error(e)
30
30
  end
31
31
  end
32
+
33
+ def put(params: nil, headers: nil, body: nil)
34
+ validate_api_key
35
+
36
+ begin
37
+ response = self.rest_client.put do |request|
38
+ configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
39
+ end
40
+ parse_response(response.body)
41
+ rescue => e
42
+ handle_error(e)
43
+ end
44
+ end
32
45
 
33
46
  def get(params: nil, headers: nil)
34
47
  validate_api_key
@@ -36,6 +36,12 @@ module Gibbon
36
36
  reset
37
37
  end
38
38
 
39
+ def upsert(params: nil, headers: nil, body: nil)
40
+ APIRequest.new(builder: self).put(params: params, headers: headers, body: body)
41
+ ensure
42
+ reset
43
+ end
44
+
39
45
  def retrieve(params: nil, headers: nil)
40
46
  APIRequest.new(builder: self).get(params: params, headers: headers)
41
47
  ensure
@@ -1,3 +1,3 @@
1
1
  module Gibbon
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'webmock/rspec'
3
+ require 'digest/md5'
4
+
5
+ describe Gibbon do
6
+ let(:api_key) { '1234-us1' }
7
+ let(:list_id) { 'testlist' }
8
+ let(:email) { 'john.doe@example.com' }
9
+ let(:member_id) { Digest::MD5.hexdigest(email) }
10
+
11
+ let(:request_body) do
12
+ {
13
+ email_address: email,
14
+ status: 'subscribed',
15
+ merge_fields: {FNAME: 'John', LNAME: 'Doe'}
16
+ }
17
+ end
18
+
19
+ it 'supports upsert request' do
20
+ stub_request(:put, "https://apikey:1234-us1@us1.api.mailchimp.com/3.0/lists/#{list_id}/members/#{member_id}")
21
+ .with(body: MultiJson.dump(request_body))
22
+ .to_return(status: 200)
23
+
24
+ Gibbon::Request.new(api_key: api_key)
25
+ .lists(list_id).members(member_id)
26
+ .upsert(body: request_body)
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gibbon
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amro Mousa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-29 00:00:00.000000000 Z
11
+ date: 2015-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.21.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.21.0
69
83
  description: A wrapper for MailChimp API 3.0
70
84
  email:
71
85
  - amromousa@gmail.com
@@ -89,6 +103,7 @@ files:
89
103
  - lib/gibbon/request.rb
90
104
  - lib/gibbon/version.rb
91
105
  - spec/gibbon/gibbon_spec.rb
106
+ - spec/gibbon/upsert_spec.rb
92
107
  - spec/spec_helper.rb
93
108
  homepage: http://github.com/amro/gibbon
94
109
  licenses:
@@ -119,4 +134,5 @@ specification_version: 4
119
134
  summary: A wrapper for MailChimp API 3.0
120
135
  test_files:
121
136
  - spec/gibbon/gibbon_spec.rb
137
+ - spec/gibbon/upsert_spec.rb
122
138
  - spec/spec_helper.rb