my_emma 0.1.2

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Hashrocket
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,42 @@
1
+ h1. my_emma
2
+
3
+ p. Post requests to MyEmma.com's Remote Signup API.
4
+
5
+ h2. Installation
6
+
7
+ <pre>sudo gem install hashrocket-my_emma</pre>
8
+
9
+ h2. Usage
10
+
11
+ <pre><code>
12
+ >> MyEmma.credentials = {
13
+ :emma_account_id => '9999',
14
+ :signup_post => '8888',
15
+ :username => 'user',
16
+ :password => 'pass'
17
+ }
18
+ >> response = MyEmma.signup("test@example.com")
19
+ => #<MyEmma::Response:0x11ba840 @response="1", @code=1>
20
+ >> response.success?
21
+ => true
22
+ >> response.failed?
23
+ => false
24
+ >> response.added_member?
25
+ => true
26
+
27
+ >> response = MyEmma.signup("test@example.com")
28
+ => #<MyEmma::Response:0x11ba840 @response="2", @code=2>
29
+ >> response.updated_member?
30
+ => true
31
+
32
+ >> MyEmma.signup("joe@example.com", {:emma_member_name_first => "Joe", :emma_member_name_last => "Piscopo"})
33
+ => #<MyEmma::Response:0x11809b0 @response="1", @code=1>
34
+
35
+ # Assign member to group with an ID of 12345
36
+ >> MyEmma.signup("groupie@example.com", "group[12345]" => 1)
37
+ => <MyEmma::Response:0x113a6f4 @response="1", @code=1>
38
+ </code></pre>
39
+
40
+ h2. Copyright
41
+
42
+ Copyright (c) 2009 Hashrocket. See MIT_LICENSE for details.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "my_emma"
8
+ gem.summary = %Q{Ruby wrapper for the MyEmma Remote Signup API}
9
+ gem.email = "dev@hashrocket.com"
10
+ gem.homepage = "http://github.com/hashrocket/my_emma"
11
+ gem.authors = ["Jim Remsik", "Sandro Turriate"]
12
+ gem.add_dependency('httparty', '0.4.5')
13
+ gem.add_development_dependency('rspec', '1.2.9')
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+ task :default => :spec
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ if File.exist?('VERSION.yml')
38
+ config = YAML.load(File.read('VERSION.yml'))
39
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "my_emma #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
49
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,47 @@
1
+ # Copied from ActiveSupport 2.3.2
2
+ class Hash
3
+ # Return a new hash with all keys converted to strings.
4
+ def stringify_keys
5
+ inject({}) do |options, (key, value)|
6
+ options[key.to_s] = value
7
+ options
8
+ end
9
+ end
10
+
11
+ # Destructively convert all keys to strings.
12
+ def stringify_keys!
13
+ keys.each do |key|
14
+ self[key.to_s] = delete(key)
15
+ end
16
+ self
17
+ end
18
+
19
+ # Return a new hash with all keys converted to symbols.
20
+ def symbolize_keys
21
+ inject({}) do |options, (key, value)|
22
+ options[(key.to_sym rescue key) || key] = value
23
+ options
24
+ end
25
+ end
26
+
27
+ # Destructively convert all keys to symbols.
28
+ def symbolize_keys!
29
+ self.replace(self.symbolize_keys)
30
+ end
31
+
32
+ alias_method :to_options, :symbolize_keys
33
+ alias_method :to_options!, :symbolize_keys!
34
+
35
+ # Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
36
+ # Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
37
+ # as keys, this will fail.
38
+ #
39
+ # ==== Examples
40
+ # { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
41
+ # { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
42
+ # { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
43
+ def assert_valid_keys(*valid_keys)
44
+ unknown_keys = keys - [valid_keys].flatten
45
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ HTTParty::Request.class_eval do
2
+ class << self
3
+ attr_accessor :debug
4
+ end
5
+ self.debug = false
6
+
7
+ def perform_with_debug
8
+ if self.class.debug
9
+ puts "HTTParty making #{http_method::METHOD} request to:"
10
+ puts uri
11
+ end
12
+ perform_without_debug
13
+ end
14
+
15
+ alias_method :perform_without_debug, :perform
16
+ alias_method :perform, :perform_with_debug
17
+ end
@@ -0,0 +1,45 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'rubygems'
3
+ require 'core_ext'
4
+ require 'httparty'
5
+ require 'httparty_ext'
6
+ require 'rfc822'
7
+
8
+ class MyEmma
9
+ autoload :Response, 'my_emma/response'
10
+
11
+ include HTTParty
12
+ base_uri "https://app.e2ma.net"
13
+
14
+ class Error < RuntimeError; end
15
+
16
+ REQUIRED_CREDENTIALS = [
17
+ 'emma_account_id',
18
+ 'signup_post',
19
+ 'username',
20
+ 'password',
21
+ ].sort.freeze
22
+
23
+ def self.signup(email, parameters={})
24
+ if email_valid?(email)
25
+ parameters.merge! credentials.merge(:emma_member_email => email)
26
+ Response.new post('/app/view:RemoteSignup', :body => parameters)
27
+ end
28
+ end
29
+
30
+ def self.credentials=(h={})
31
+ h.stringify_keys!
32
+ raise Error, "Missing required credential(s): #{(REQUIRED_CREDENTIALS - h.keys.sort).inspect}" unless h.keys.sort == REQUIRED_CREDENTIALS
33
+ @@credentials = h
34
+ end
35
+
36
+ def self.credentials
37
+ @@credentials ||= {}
38
+ end
39
+
40
+ private
41
+
42
+ def self.email_valid?(email)
43
+ email && email =~ RFC822::EmailAddress
44
+ end
45
+ end
@@ -0,0 +1,34 @@
1
+ class MyEmma
2
+ class Response
3
+ attr_reader :response, :code
4
+ alias to_s response
5
+
6
+ def initialize(response)
7
+ @response = response
8
+ @code = response.to_i
9
+ end
10
+
11
+ def failed?
12
+ code <= 0
13
+ end
14
+
15
+ def success?
16
+ code > 0
17
+ end
18
+
19
+ RESPONSES = {
20
+ :added_member? => 1,
21
+ :updated_member? => 2,
22
+ :member_exists? => 3,
23
+ :authentication_failed? => -1,
24
+ :add_member_failed? => -2,
25
+ :update_member_failed? => -3
26
+ }
27
+
28
+ RESPONSES.each do |name, error_code|
29
+ define_method name do
30
+ code == error_code
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # RFC822 Email Address Regex
3
+ # --------------------------
4
+ #
5
+ # Originally written by Cal Henderson
6
+ # c.f. http://iamcal.com/publish/articles/php/parsing_email/
7
+ #
8
+ # Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
9
+ #
10
+ # Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
11
+ # http://creativecommons.org/licenses/by-sa/2.5/
12
+ # ro@me.com =~ RFC822::EmailAddress
13
+ module RFC822
14
+ EmailAddress = begin
15
+ qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
16
+ dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
17
+ atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
18
+ '\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
19
+ quoted_pair = '\\x5c[\\x00-\\x7f]'
20
+ domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
21
+ quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
22
+ domain_ref = atom
23
+ sub_domain = "(?:#{domain_ref}|#{domain_literal})"
24
+ word = "(?:#{atom}|#{quoted_string})"
25
+ domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
26
+ local_part = "#{word}(?:\\x2e#{word})*"
27
+ addr_spec = "#{local_part}\\x40#{domain}"
28
+ pattern = /\A#{addr_spec}\z/
29
+ end
30
+ end
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{my_emma}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jim Remsik", "Sandro Turriate"]
12
+ s.date = %q{2009-11-12}
13
+ s.email = %q{dev@hashrocket.com}
14
+ s.extra_rdoc_files = [
15
+ "README.textile"
16
+ ]
17
+ s.files = [
18
+ ".document",
19
+ ".gitignore",
20
+ "MIT_LICENSE",
21
+ "README.textile",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/core_ext.rb",
25
+ "lib/httparty_ext.rb",
26
+ "lib/my_emma.rb",
27
+ "lib/my_emma/response.rb",
28
+ "lib/rfc822.rb",
29
+ "my_emma.gemspec",
30
+ "spec/my_emma/response_spec.rb",
31
+ "spec/my_emma_spec.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/hashrocket/my_emma}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{Ruby wrapper for the MyEmma Remote Signup API}
39
+ s.test_files = [
40
+ "spec/my_emma/response_spec.rb",
41
+ "spec/my_emma_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<httparty>, ["= 0.4.5"])
51
+ s.add_development_dependency(%q<rspec>, ["= 1.2.9"])
52
+ else
53
+ s.add_dependency(%q<httparty>, ["= 0.4.5"])
54
+ s.add_dependency(%q<rspec>, ["= 1.2.9"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<httparty>, ["= 0.4.5"])
58
+ s.add_dependency(%q<rspec>, ["= 1.2.9"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,63 @@
1
+ require File.join(File.dirname(__FILE__), "/../spec_helper.rb")
2
+
3
+ describe MyEmma::Response do
4
+ describe "#initialize" do
5
+ it "instantiates with a response code" do
6
+ expect{ MyEmma::Response.new('1') }.should_not raise_error
7
+ end
8
+
9
+ it "transforms the response code to an integer" do
10
+ response = MyEmma::Response.new('1')
11
+ response.code.should == 1
12
+ end
13
+ end
14
+
15
+ describe "successful response" do
16
+ it "succeeded" do
17
+ response = MyEmma::Response.new('1')
18
+ response.success?.should be_true
19
+ end
20
+
21
+ it "added a member" do
22
+ response = MyEmma::Response.new('1')
23
+ response.added_member?.should be_true
24
+ end
25
+
26
+ it "updated a member" do
27
+ response = MyEmma::Response.new('2')
28
+ response.updated_member?.should be_true
29
+ end
30
+
31
+ it "indicates member already exists" do
32
+ response = MyEmma::Response.new('3')
33
+ response.member_exists?.should be_true
34
+ end
35
+ end
36
+
37
+ describe "failure" do
38
+ it "failed" do
39
+ response = MyEmma::Response.new('-1')
40
+ response.failed?.should be_true
41
+ end
42
+
43
+ it "authentication" do
44
+ response = MyEmma::Response.new('-1')
45
+ response.authentication_failed?.should be_true
46
+ end
47
+
48
+ it "member add failed" do
49
+ response = MyEmma::Response.new('-2')
50
+ response.add_member_failed?.should be_true
51
+ end
52
+
53
+ it "member update failed" do
54
+ response = MyEmma::Response.new('-3')
55
+ response.update_member_failed?.should be_true
56
+ end
57
+
58
+ it "non-integer response is a failure" do
59
+ response = MyEmma::Response.new('<html>blah</html>')
60
+ response.failed?.should be_true
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,55 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper.rb")
2
+
3
+ describe MyEmma do
4
+ it "defines the host" do
5
+ MyEmma.base_uri.should == "https://app.e2ma.net"
6
+ end
7
+
8
+ describe "setting credentials" do
9
+ it "requires signup_post, emma_account_id, username, password" do
10
+ lambda {
11
+ MyEmma.credentials = {}
12
+ }.should raise_error(MyEmma::Error)
13
+ end
14
+ end
15
+
16
+ describe "#signup" do
17
+ it "posts to the RemoteSignup endpoint" do
18
+ MyEmma.should_receive(:post).with('/app/view:RemoteSignup', anything)
19
+ MyEmma.signup('test@example.com')
20
+ end
21
+
22
+ it "posts the email to the endpoint" do
23
+ MyEmma.should_receive(:post).with(anything, :body => hash_including(:emma_member_email => 'test@example.com'))
24
+ MyEmma.signup('test@example.com')
25
+ end
26
+
27
+ it "returns nil if email is invalid" do
28
+ MyEmma.signup('invalid_email_address').should be_nil
29
+ MyEmma.signup(nil).should be_nil
30
+ end
31
+
32
+ it "returns an integer on success" do
33
+ MyEmma.stub!(:post => 1)
34
+ MyEmma.signup("me@example.com").success?.should be_true
35
+ end
36
+
37
+ it "returns a negative integer on failure" do
38
+ MyEmma.stub!(:post => -1)
39
+ MyEmma.signup("me@example.com").failed?.should be_true
40
+ end
41
+
42
+ it "adds credentials to the post request" do
43
+ MyEmma.stub!(:credentials => {:username => 'someuser'})
44
+ MyEmma.should_receive(:post).with(anything, :body => hash_including(:username => 'someuser'))
45
+ MyEmma.signup('email@example.com')
46
+ end
47
+
48
+ it "adds additional parameters to the body" do
49
+ MyEmma.stub!(:credentials => {:username => 'someuser'})
50
+ MyEmma.should_receive(:post).with(anything, :body => hash_including(:username => 'someuser', :first_name => "John", :last_name => "Smith", :emma_member_email => 'email@example.com'))
51
+ MyEmma.signup('email@example.com', :first_name => "John", :last_name => "Smith")
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'my_emma'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: my_emma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Jim Remsik
8
+ - Sandro Turriate
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-11-12 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: httparty
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - "="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.4.5
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ type: :development
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "="
33
+ - !ruby/object:Gem::Version
34
+ version: 1.2.9
35
+ version:
36
+ description:
37
+ email: dev@hashrocket.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - README.textile
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - MIT_LICENSE
48
+ - README.textile
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/core_ext.rb
52
+ - lib/httparty_ext.rb
53
+ - lib/my_emma.rb
54
+ - lib/my_emma/response.rb
55
+ - lib/rfc822.rb
56
+ - my_emma.gemspec
57
+ - spec/my_emma/response_spec.rb
58
+ - spec/my_emma_spec.rb
59
+ - spec/spec_helper.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/hashrocket/my_emma
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.5
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Ruby wrapper for the MyEmma Remote Signup API
88
+ test_files:
89
+ - spec/my_emma/response_spec.rb
90
+ - spec/my_emma_spec.rb
91
+ - spec/spec_helper.rb