hashrocket-my_emma 0.1.1

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/MIT_LICENSE ADDED
@@ -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.
data/README.textile ADDED
@@ -0,0 +1,7 @@
1
+ h1. my_emma
2
+
3
+ Description goes here.
4
+
5
+ h2. Copyright
6
+
7
+ Copyright (c) 2009 Hashrocket. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
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('jnunemaker-httparty', '>= 0.4.3')
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "my_emma #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/lib/core_ext.rb ADDED
@@ -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
data/lib/my_emma.rb ADDED
@@ -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', :query => 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
data/lib/rfc822.rb ADDED
@@ -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
data/my_emma.gemspec ADDED
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{my_emma}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jim Remsik", "Sandro Turriate"]
9
+ s.date = %q{2009-06-24}
10
+ s.email = %q{dev@hashrocket.com}
11
+ s.extra_rdoc_files = [
12
+ "README.textile"
13
+ ]
14
+ s.files = [
15
+ ".document",
16
+ ".gitignore",
17
+ "MIT_LICENSE",
18
+ "README.textile",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "lib/core_ext.rb",
22
+ "lib/httparty_ext.rb",
23
+ "lib/my_emma.rb",
24
+ "lib/my_emma/response.rb",
25
+ "lib/rfc822.rb",
26
+ "my_emma.gemspec",
27
+ "spec/my_emma/response_spec.rb",
28
+ "spec/my_emma_spec.rb",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.has_rdoc = true
32
+ s.homepage = %q{http://github.com/hashrocket/my_emma}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.2}
36
+ s.summary = %q{Ruby wrapper for the MyEmma Remote Signup API}
37
+ s.test_files = [
38
+ "spec/my_emma/response_spec.rb",
39
+ "spec/my_emma_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<jnunemaker-httparty>, [">= 0.4.3"])
49
+ else
50
+ s.add_dependency(%q<jnunemaker-httparty>, [">= 0.4.3"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<jnunemaker-httparty>, [">= 0.4.3"])
54
+ end
55
+ end
@@ -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, :query => 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, :query => hash_including(:username => 'someuser'))
45
+ MyEmma.signup('email@example.com')
46
+ end
47
+
48
+ it "adds additional parameters to the query" do
49
+ MyEmma.stub!(:credentials => {:username => 'someuser'})
50
+ MyEmma.should_receive(:post).with(anything, :query => 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,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashrocket-my_emma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jim Remsik
8
+ - Sandro Turriate
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-06-24 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: jnunemaker-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.3
25
+ version:
26
+ description:
27
+ email: dev@hashrocket.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - README.textile
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - MIT_LICENSE
38
+ - README.textile
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/core_ext.rb
42
+ - lib/httparty_ext.rb
43
+ - lib/my_emma.rb
44
+ - lib/my_emma/response.rb
45
+ - lib/rfc822.rb
46
+ - my_emma.gemspec
47
+ - spec/my_emma/response_spec.rb
48
+ - spec/my_emma_spec.rb
49
+ - spec/spec_helper.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/hashrocket/my_emma
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Ruby wrapper for the MyEmma Remote Signup API
76
+ test_files:
77
+ - spec/my_emma/response_spec.rb
78
+ - spec/my_emma_spec.rb
79
+ - spec/spec_helper.rb