chimp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/LICENSE +19 -0
  2. data/README +22 -0
  3. data/chimp.gemspec +21 -0
  4. data/lib/chimp.rb +51 -0
  5. data/rakefile +5 -0
  6. data/test/chimp.rb +49 -0
  7. metadata +73 -0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010, 2011 Michel Martens, Damian Janowski and Cyril David
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,22 @@
1
+ CHIMP(1)
2
+
3
+ NAME
4
+ chimp -- Super light mailchimp client
5
+
6
+ SYNOPSIS
7
+ chimp = Chimp.connect("acbd18db4cc2f85cedef654fccc4a4d8-us4")
8
+
9
+ # Get all lists for your account
10
+ chimp.lists
11
+ # => {"total"=>1, "data"=>[{"id"=>"l654321", ...}]]}
12
+
13
+ # Add a new email to one of your lists identified by id
14
+ chimp.subscribe("foo@bar.com", "l654321")
15
+ # => "true"
16
+
17
+ # Unsubscribe an existing email
18
+ chimp.unsubscribe("foo@bar.com", "l654321")
19
+ # => "true"
20
+
21
+ INSTALLATION
22
+ $ gem install chimp
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "chimp"
3
+ s.version = "0.0.1"
4
+ s.summary = "Lightweight mailchimp client"
5
+ s.description = "For those who want something nimble."
6
+ s.authors = ["Cyril David"]
7
+ s.email = ["me@cyrildavid.com"]
8
+ s.homepage = "http://github.com/cyx/chimp"
9
+
10
+ s.files = Dir[
11
+ "LICENSE",
12
+ "README",
13
+ "rakefile",
14
+ "lib/**/*.rb",
15
+ "*.gemspec",
16
+ "test/*.*"
17
+ ]
18
+
19
+ s.add_development_dependency "dep"
20
+ s.add_development_dependency "cutest"
21
+ end
@@ -0,0 +1,51 @@
1
+ require "uri"
2
+ require "json"
3
+
4
+ class Chimp
5
+ URL = "https://%s.api.mailchimp.com/1.3/?method="
6
+
7
+ attr :url
8
+ attr :data
9
+
10
+ def self.connect(apikey)
11
+ dc = apikey[/-(.+)$/, 1]
12
+
13
+ new(apikey, dc)
14
+ end
15
+
16
+ def initialize(apikey, datacenter)
17
+ @url = URL % datacenter
18
+ @data = { apikey: apikey }
19
+ end
20
+
21
+ def lists
22
+ JSON(post("lists"))
23
+ end
24
+
25
+ def subscribe(email, list_id)
26
+ post("listSubscribe", email_address: email, id: list_id)
27
+ end
28
+
29
+ def unsubscribe(email, list_id)
30
+ post("listUnsubscribe", email_address: email, id: list_id)
31
+ end
32
+
33
+ private
34
+ def post(method, dict = {})
35
+ options = '-X POST --data "%s"' % payload(dict)
36
+
37
+ curl(url + method, options)
38
+ end
39
+
40
+ def payload(dict = {})
41
+ params = data.merge(dict)
42
+
43
+ params.map do |k, v|
44
+ "%s=%s" % [k, URI.encode_www_form_component(v)]
45
+ end.join("&")
46
+ end
47
+
48
+ def curl(url, options)
49
+ `curl #{options} #{url} 2>/dev/null`
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ desc "Run all tests"
2
+ task :test do
3
+ exec("cutest test/chimp.rb")
4
+ end
5
+ task :default => :test
@@ -0,0 +1,49 @@
1
+ require_relative "../lib/chimp"
2
+
3
+ test "connect" do
4
+ chimp = Chimp.connect("foo-us1")
5
+
6
+ assert_equal("https://us1.api.mailchimp.com/1.3/?method=", chimp.url)
7
+ assert_equal({ apikey: "foo-us1" }, chimp.data)
8
+ end
9
+
10
+ scope do
11
+ # minimalistic mocking :-)
12
+ class Chimp
13
+ def curl(url, options)
14
+ $curl = "curl #{options} #{url}"
15
+
16
+ return $curl_result if $curl_result
17
+ end
18
+ end
19
+
20
+ setup do
21
+ Chimp.connect("key-us1")
22
+ end
23
+
24
+ test "lists" do |chimp|
25
+ $curl_result = JSON.dump({ id: "12345" })
26
+
27
+ result = chimp.lists
28
+
29
+ assert_equal({ "id" => "12345" }, result)
30
+
31
+ curl = 'curl -X POST --data "apikey=key-us1" ' +
32
+ 'https://us1.api.mailchimp.com/1.3/?method=lists'
33
+
34
+ assert_equal curl, $curl
35
+ end
36
+
37
+ test "subscribe" do |chimp|
38
+ $curl_result = "OK"
39
+
40
+ result = chimp.subscribe("foo@bar.com", "654321")
41
+
42
+ assert_equal("OK", result)
43
+
44
+ curl = 'curl -X POST --data "apikey=key-us1&email_address=foo%40bar.com' +
45
+ '&id=654321" https://us1.api.mailchimp.com/1.3/?method=listSubscribe'
46
+
47
+ assert_equal curl, $curl
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chimp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cyril David
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dep
16
+ requirement: &70103702043580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70103702043580
25
+ - !ruby/object:Gem::Dependency
26
+ name: cutest
27
+ requirement: &70103702043020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70103702043020
36
+ description: For those who want something nimble.
37
+ email:
38
+ - me@cyrildavid.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - LICENSE
44
+ - README
45
+ - rakefile
46
+ - lib/chimp.rb
47
+ - chimp.gemspec
48
+ - test/chimp.rb
49
+ homepage: http://github.com/cyx/chimp
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.11
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Lightweight mailchimp client
73
+ test_files: []