hashrocket-mousetrap 0.1.1 → 0.2.0

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/Rakefile CHANGED
@@ -8,10 +8,12 @@ begin
8
8
  gem.summary = %Q{CheddarGetter API Client in Ruby}
9
9
  gem.description = %Q{CheddarGetter API Client in Ruby}
10
10
  gem.email = "jonlarkowski@gmail.com"
11
- gem.homepage = "http://github.com/l4rk/mousetrap"
12
- gem.authors = ["Jon Larkowski", "Sandro Turriate"]
13
- gem.add_development_dependency "rspec", '>= 1.2.8'
11
+ gem.homepage = "http://github.com/hashrocket/mousetrap"
12
+ gem.authors = ["Jon Larkowski", "Sandro Turriate", "Wolfram Arnold", "Corey Grusden"]
14
13
  gem.add_dependency 'httparty', '>= 0.4.4'
14
+ gem.add_development_dependency "activesupport", '>= 2.3.3'
15
+ gem.add_development_dependency "rspec", '>= 1.2.8'
16
+ gem.add_development_dependency 'thoughtbot-factory_girl', '>= 1.2.2'
15
17
  end
16
18
  rescue LoadError
17
19
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/lib/mousetrap.rb CHANGED
@@ -4,12 +4,16 @@ begin; require 'rubygems'; rescue LoadError; end
4
4
  require 'httparty'
5
5
 
6
6
  module Mousetrap
7
- autoload(:Resource, 'mousetrap/resource')
8
- autoload(:Customer, 'mousetrap/customer')
9
-
7
+ NO_BUSINESS_NEED = "No business need at this time."
8
+
9
+ autoload :Customer, 'mousetrap/customer'
10
+ autoload :Plan, 'mousetrap/plan'
11
+ autoload :Resource, 'mousetrap/resource'
12
+ autoload :Subscription, 'mousetrap/subscription'
13
+
10
14
  class << self
11
15
  attr_accessor :product_code
12
-
16
+
13
17
  def authenticate(user, password)
14
18
  Resource.basic_auth user, password
15
19
  end
@@ -1,11 +1,98 @@
1
1
  module Mousetrap
2
2
  class Customer < Resource
3
- def self.all
4
- get_resources 'customers'
3
+ attr_accessor \
4
+ :id,
5
+ :code,
6
+ :email,
7
+ :first_name,
8
+ :last_name,
9
+ :subscription
10
+
11
+ def subscription_attributes=(attributes)
12
+ self.subscription = Subscription.new attributes
5
13
  end
6
-
7
- def self.create(hash)
8
- post_resource 'customers', 'new', hash
14
+
15
+ def attributes
16
+ {
17
+ :id => id,
18
+ :code => code,
19
+ :email => email,
20
+ :first_name => first_name,
21
+ :last_name => last_name,
22
+ }
23
+ end
24
+
25
+ def attributes_for_api
26
+ a = self.class.attributes_for_api(attributes, new_record?)
27
+
28
+ if subscription
29
+ a[:subscription] = subscription.attributes_for_api
30
+ end
31
+
32
+ a
33
+ end
34
+
35
+ def destroy
36
+ self.class.delete_resource('customers', code) unless new_record?
37
+ end
38
+
39
+ def save
40
+ new? ? create : update
41
+ end
42
+
43
+ def self.create(attributes)
44
+ object = new(attributes)
45
+ response = object.save
46
+ returned_customer = build_resource_from response
47
+ object.id = returned_customer.id
48
+ # TODO: what other attrs to copy over?
49
+ object
50
+ end
51
+
52
+ def self.new_from_api(attributes)
53
+ customer = new(attributes_from_api(attributes))
54
+ customer.subscription = Subscription.new_from_api(
55
+ attributes['subscriptions']['subscription'])
56
+ customer
57
+ end
58
+
59
+
60
+ protected
61
+
62
+ def self.plural_resource_name
63
+ 'customers'
64
+ end
65
+
66
+ def self.singular_resource_name
67
+ 'customer'
68
+ end
69
+
70
+ def self.attributes_for_api(attributes, new_record = true)
71
+ mutated_hash = {
72
+ :email => attributes[:email],
73
+ :firstName => attributes[:first_name],
74
+ :lastName => attributes[:last_name],
75
+ }
76
+ mutated_hash.merge!(:code => attributes[:code]) if new_record
77
+ mutated_hash
78
+ end
79
+
80
+ def self.attributes_from_api(attributes)
81
+ {
82
+ :id => attributes['id'],
83
+ :code => attributes['code'],
84
+ :first_name => attributes['firstName'],
85
+ :last_name => attributes['lastName'],
86
+ :email => attributes['email']
87
+ }
88
+ end
89
+
90
+ def create
91
+ self.class.post_resource 'customers', 'new', attributes_for_api
92
+ end
93
+
94
+ def update
95
+ self.class.put_resource 'customers', 'edit-customer', code, attributes_for_api
9
96
  end
10
97
  end
11
98
  end
@@ -0,0 +1,27 @@
1
+ module Mousetrap
2
+ class Plan < Resource
3
+ attr_accessor \
4
+ :id,
5
+ :code,
6
+ :name
7
+
8
+
9
+ protected
10
+
11
+ def self.plural_resource_name
12
+ 'plans'
13
+ end
14
+
15
+ def self.singular_resource_name
16
+ 'plan'
17
+ end
18
+
19
+ def self.attributes_from_api(attributes)
20
+ {
21
+ :id => attributes['id'],
22
+ :code => attributes['code'],
23
+ :name => attributes['name'],
24
+ }
25
+ end
26
+ end
27
+ end
@@ -4,12 +4,115 @@ module Mousetrap
4
4
  headers 'User-Agent' => 'Mousetrap Ruby Client'
5
5
  base_uri 'https://cheddargetter.com'
6
6
 
7
+ def initialize(hash={})
8
+ hash.each do |key, value|
9
+ self.send("#{key}=", value)
10
+ end
11
+ end
12
+
13
+ def self.[](code)
14
+ response = get_resource plural_resource_name, code
15
+ build_resource_from response
16
+ end
17
+
18
+ def self.all
19
+ response = get_resources plural_resource_name
20
+ build_resources_from response
21
+ end
22
+
23
+ def self.create(attributes = {})
24
+ raise NotImplementedError, NO_BUSINESS_NEED
25
+ end
26
+
27
+ def self.delete(code)
28
+ raise NotImplementedError, NO_BUSINESS_NEED
29
+ end
30
+
31
+ def self.exists?(code)
32
+ raise NotImplementedError, NO_BUSINESS_NEED
33
+ end
34
+
35
+ def destroy
36
+ raise NotImplementedError, NO_BUSINESS_NEED
37
+ end
38
+
39
+ def exists?(code)
40
+ raise NotImplementedError, NO_BUSINESS_NEED
41
+ end
42
+
43
+ def new?
44
+ id.nil?
45
+ end
46
+
47
+ alias new_record? new?
48
+
49
+ def self.new_from_api(attributes)
50
+ new(attributes_from_api(attributes))
51
+ end
52
+
53
+ def save
54
+ raise NotImplementedError, NO_BUSINESS_NEED
55
+ end
56
+
57
+
58
+ protected
59
+
60
+ def self.delete_resource(resource, code)
61
+ path = "/xml/#{resource}/delete/productCode/#{Mousetrap.product_code}/code/#{code}"
62
+ post path
63
+ end
64
+
65
+ def self.get_resource(resource, code)
66
+ path = "/xml/#{resource}/get/productCode/#{Mousetrap.product_code}/code/#{code}"
67
+ get path
68
+ end
69
+
7
70
  def self.get_resources(resource)
8
- get "/xml/#{resource}/get/productCode/#{Mousetrap.product_code}"
71
+ path = "/xml/#{resource}/get/productCode/#{Mousetrap.product_code}"
72
+ get path
73
+ end
74
+
75
+ def self.post_resource(resource, action, attributes)
76
+ path = "/xml/#{resource}/#{action}/productCode/#{Mousetrap.product_code}"
77
+ post path, :body => attributes
78
+ end
79
+
80
+ def self.put_resource(resource, action, resource_code, attributes)
81
+ path = "/xml/#{resource}/#{action}/productCode/#{Mousetrap.product_code}/code/#{resource_code}"
82
+ post path, :body => attributes
83
+ end
84
+
85
+ def self.plural_resource_name
86
+ raise 'You must implement self.plural_resource_name in your subclass.'
87
+ end
88
+
89
+ def self.singular_resource_name
90
+ raise 'You must implement self.singular_resource_name in your subclass.'
91
+ end
92
+
93
+ def self.build_resource_from(response)
94
+ resource_attributes = extract_resources(response)
95
+ new_from_api(resource_attributes)
96
+ end
97
+
98
+ def self.build_resources_from(response)
99
+ resources = []
100
+
101
+ response_resources = extract_resources(response)
102
+
103
+ if response_resources.is_a?(Array)
104
+ extract_resources(response).each do |resource_attributes|
105
+ resources << new_from_api(resource_attributes)
106
+ end
107
+ else
108
+ resources << new_from_api(response_resources)
109
+ end
110
+
111
+ resources
9
112
  end
10
113
 
11
- def self.post_resource(resource, action, hash)
12
- post "/xml/#{resource}/#{action}/productCode/#{Mousetrap.product_code}", :body => hash
114
+ def self.extract_resources(response)
115
+ response[plural_resource_name][singular_resource_name]
13
116
  end
14
117
  end
15
118
  end
@@ -0,0 +1,81 @@
1
+ module Mousetrap
2
+ class Subscription < Resource
3
+ # Attributes we send _to_ the API.
4
+ attr_accessor \
5
+ :plan_code,
6
+ :billing_first_name,
7
+ :billing_last_name,
8
+ :credit_card_number,
9
+ :credit_card_expiration_month,
10
+ :credit_card_expiration_year,
11
+ :billing_zip_code,
12
+
13
+ :customer_code, # belongs to customer
14
+
15
+ # Attributes that come _from_ the API.
16
+ :id,
17
+ :canceled_at,
18
+ :created_at,
19
+ :credit_card_expiration_date,
20
+ :credit_card_last_four_digits,
21
+ :credit_card_type
22
+
23
+ # TODO: not sure if .all or .[] will work
24
+
25
+ def attributes
26
+ {
27
+ :id => id,
28
+ :plan_code => plan_code,
29
+ :billing_first_name => billing_first_name,
30
+ :billing_last_name => billing_last_name,
31
+ :credit_card_number => credit_card_number,
32
+ :credit_card_expiration_month => credit_card_expiration_month,
33
+ :credit_card_expiration_year => credit_card_expiration_year,
34
+ :billing_zip_code => billing_zip_code,
35
+ }
36
+ end
37
+
38
+ def attributes_for_api
39
+ self.class.attributes_for_api(attributes)
40
+ end
41
+
42
+ def save
43
+ mutated_attributes = attributes_for_api(attributes)
44
+ self.class.put_resource('customers', 'edit-subscription', customer_code, mutated_attributes)
45
+ end
46
+
47
+
48
+ protected
49
+
50
+ def self.plural_resource_name
51
+ 'subscriptions'
52
+ end
53
+
54
+ def self.singular_resource_name
55
+ 'subscription'
56
+ end
57
+
58
+ def self.attributes_for_api(attributes)
59
+ {
60
+ :planCode => attributes[:plan_code],
61
+ :ccFirstName => attributes[:billing_first_name],
62
+ :ccLastName => attributes[:billing_last_name],
63
+ :ccNumber => attributes[:credit_card_number],
64
+ :ccExpMonth => attributes[:credit_card_expiration_month],
65
+ :ccExpYear => attributes[:credit_card_expiration_year],
66
+ :ccZip => attributes[:billing_zip_code],
67
+ }
68
+ end
69
+
70
+ def self.attributes_from_api(attributes)
71
+ {
72
+ :id => attributes['id'],
73
+ :canceled_at => attributes['canceledDatetime'],
74
+ :created_at => attributes['createdDatetime'],
75
+ :credit_card_expiration_date => attributes['ccExpirationDate'],
76
+ :credit_card_last_four_digits => attributes['ccLastFour'],
77
+ :credit_card_type => attributes['ccType'],
78
+ }
79
+ end
80
+ end
81
+ end
data/mousetrap.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mousetrap}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Jon Larkowski", "Sandro Turriate"]
12
- s.date = %q{2009-08-25}
11
+ s.authors = ["Jon Larkowski", "Sandro Turriate", "Wolfram Arnold", "Corey Grusden"]
12
+ s.date = %q{2009-09-01}
13
13
  s.description = %q{CheddarGetter API Client in Ruby}
14
14
  s.email = %q{jonlarkowski@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -24,27 +24,39 @@ Gem::Specification.new do |s|
24
24
  "VERSION",
25
25
  "lib/mousetrap.rb",
26
26
  "lib/mousetrap/customer.rb",
27
+ "lib/mousetrap/plan.rb",
27
28
  "lib/mousetrap/resource.rb",
29
+ "lib/mousetrap/subscription.rb",
28
30
  "mousetrap.gemspec",
31
+ "spec/factories.rb",
29
32
  "spec/integration/settings.example.yml",
30
33
  "spec/integration/smoke_test.rb",
34
+ "spec/integration/spike.rb",
31
35
  "spec/mousetrap/customer_spec.rb",
36
+ "spec/mousetrap/plan_spec.rb",
32
37
  "spec/mousetrap/resource_spec.rb",
38
+ "spec/mousetrap/subscription_spec.rb",
33
39
  "spec/mousetrap_spec.rb",
34
40
  "spec/spec.opts",
35
- "spec/spec_helper.rb"
41
+ "spec/spec_helper.rb",
42
+ "spec/support/random_data.rb"
36
43
  ]
37
- s.homepage = %q{http://github.com/l4rk/mousetrap}
44
+ s.homepage = %q{http://github.com/hashrocket/mousetrap}
38
45
  s.rdoc_options = ["--charset=UTF-8"]
39
46
  s.require_paths = ["lib"]
40
47
  s.rubygems_version = %q{1.3.5}
41
48
  s.summary = %q{CheddarGetter API Client in Ruby}
42
49
  s.test_files = [
43
- "spec/integration/smoke_test.rb",
50
+ "spec/factories.rb",
51
+ "spec/integration/smoke_test.rb",
52
+ "spec/integration/spike.rb",
44
53
  "spec/mousetrap/customer_spec.rb",
54
+ "spec/mousetrap/plan_spec.rb",
45
55
  "spec/mousetrap/resource_spec.rb",
56
+ "spec/mousetrap/subscription_spec.rb",
46
57
  "spec/mousetrap_spec.rb",
47
- "spec/spec_helper.rb"
58
+ "spec/spec_helper.rb",
59
+ "spec/support/random_data.rb"
48
60
  ]
49
61
 
50
62
  if s.respond_to? :specification_version then
@@ -52,14 +64,20 @@ Gem::Specification.new do |s|
52
64
  s.specification_version = 3
53
65
 
54
66
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
55
- s.add_development_dependency(%q<rspec>, [">= 1.2.8"])
56
67
  s.add_runtime_dependency(%q<httparty>, [">= 0.4.4"])
68
+ s.add_development_dependency(%q<activesupport>, [">= 2.3.3"])
69
+ s.add_development_dependency(%q<rspec>, [">= 1.2.8"])
70
+ s.add_development_dependency(%q<thoughtbot-factory_girl>, [">= 1.2.2"])
57
71
  else
58
- s.add_dependency(%q<rspec>, [">= 1.2.8"])
59
72
  s.add_dependency(%q<httparty>, [">= 0.4.4"])
73
+ s.add_dependency(%q<activesupport>, [">= 2.3.3"])
74
+ s.add_dependency(%q<rspec>, [">= 1.2.8"])
75
+ s.add_dependency(%q<thoughtbot-factory_girl>, [">= 1.2.2"])
60
76
  end
61
77
  else
62
- s.add_dependency(%q<rspec>, [">= 1.2.8"])
63
78
  s.add_dependency(%q<httparty>, [">= 0.4.4"])
79
+ s.add_dependency(%q<activesupport>, [">= 2.3.3"])
80
+ s.add_dependency(%q<rspec>, [">= 1.2.8"])
81
+ s.add_dependency(%q<thoughtbot-factory_girl>, [">= 1.2.2"])
64
82
  end
65
83
  end
data/spec/factories.rb ADDED
@@ -0,0 +1,22 @@
1
+ Factory.define :new_customer, :class => Mousetrap::Customer, :default_strategy => :stub do |f|
2
+ f.email { random_email_address }
3
+ f.first_name { random_string }
4
+ f.last_name { random_string }
5
+ f.code { |me| me.email }
6
+ f.add_attribute :id, nil
7
+ f.subscription_attributes { Factory.attributes_for :subscription }
8
+ end
9
+
10
+ Factory.define :existing_customer, :parent => :new_customer, :default_strategy => :stub do |f|
11
+ f.add_attribute :id, '2d1244e8-e338-102c-a92d-40402145ee8b'
12
+ end
13
+
14
+ Factory.define :subscription, :class => Mousetrap::Subscription, :default_strategy => :stub do |f|
15
+ f.plan_code 'TEST'
16
+ f.billing_first_name { random_string }
17
+ f.billing_last_name { random_string }
18
+ f.credit_card_number '4111111111111111'
19
+ f.credit_card_expiration_month '12'
20
+ f.credit_card_expiration_year '2012'
21
+ f.billing_zip_code '90210'
22
+ end
@@ -1,4 +1,6 @@
1
- # Replace these with your CheddarGetter credentials.
1
+ # Replace user and password with your CheddarGetter credentials.
2
2
  user: 'lark@example.com'
3
3
  password: 'abc123'
4
- product_code: 'my_product_code'
4
+
5
+ # Go create a product in the CheddarGetter web admin interface named "mousetrap_test".
6
+ product_code: 'mousetrap_test'
@@ -1,36 +1,77 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require 'pp'
3
-
4
- describe "The System" do
5
- before do
6
- settings = YAML.load_file(File.dirname(__FILE__) + '/settings.yml')
7
- Mousetrap.authenticate(settings['user'], settings['password'])
8
- Mousetrap.product_code = settings['product_code']
9
- end
10
-
11
- it 'Customer.all' do
12
- puts Mousetrap::Customer.all.to_yaml
13
- end
14
-
15
- it "Customer.create" do
16
- email = random_email_address
17
-
18
- attributes = {
19
- 'code' => email,
20
- 'firstName' => 'Example',
21
- 'lastName' => 'Customer',
22
- 'email' => email,
23
- 'subscription' => {
24
- 'planCode' => 'TEST',
25
- 'ccFirstName' => 'Jon',
26
- 'ccLastName' => 'Larkowski',
27
- 'ccNumber' => '4111111111111111',
28
- 'ccExpiration' => '12-2012',
29
- 'ccZip' => '90210'
30
- }
31
- }
32
-
33
- customer = Mousetrap::Customer.create attributes
34
- puts customer
35
- end
36
- end
1
+ require File.dirname(__FILE__) + '/../../lib/mousetrap'
2
+ require 'yaml'
3
+ require 'activesupport'
4
+
5
+ # Requires supporting files with custom matchers and macros, etc,
6
+ # in ./support/ and its subdirectories.
7
+ Dir["#{File.dirname(__FILE__)}/../support/**/*.rb"].each {|f| require f}
8
+
9
+ settings = YAML.load_file(File.dirname(__FILE__) + '/settings.yml')
10
+ Mousetrap.authenticate(settings['user'], settings['password'])
11
+ Mousetrap.product_code = settings['product_code']
12
+
13
+
14
+
15
+ all_customers = Mousetrap::Customer.all
16
+ puts all_customers.inspect
17
+ puts all_customers.to_yaml
18
+
19
+ all_customers.each { |c| c.destroy }
20
+
21
+ all_customers = Mousetrap::Customer.all
22
+ puts all_customers.inspect
23
+ puts all_customers.to_yaml
24
+
25
+ __END__
26
+
27
+ code = 'maasdxgliu@example.com'
28
+ c = Mousetrap::Customer[code]
29
+ puts c.to_yaml
30
+ c.destroy
31
+
32
+ puts '-' * 80
33
+ c = Mousetrap::Customer[code]
34
+ puts c.to_yaml
35
+
36
+
37
+
38
+
39
+
40
+ email = random_email_address
41
+ attributes = {
42
+ 'code' => email,
43
+ 'firstName' => 'Example',
44
+ 'lastName' => 'Customer',
45
+ 'email' => email,
46
+ 'subscription' => {
47
+ 'planCode' => 'TEST',
48
+ 'ccFirstName' => 'Jon',
49
+ 'ccLastName' => 'Larkowski',
50
+ 'ccNumber' => '4111111111111111',
51
+ 'ccExpiration' => '12-2012',
52
+ 'ccZip' => '90210'
53
+ }
54
+ }
55
+
56
+ customer = Mousetrap::Customer.create attributes
57
+ puts customer
58
+
59
+
60
+
61
+
62
+ puts Mousetrap::Plan.all.to_yaml
63
+ puts Mousetrap::Customer['maasdxgliu@example.com'].to_yaml
64
+ puts Mousetrap::Plan['TEST'].to_yaml
65
+ puts Mousetrap::Customer.all.to_yaml
66
+
67
+
68
+ customers_hash = Mousetrap::Customer['maasdxgliu@example.com']
69
+ customer_hash = customers_hash['customers']['customer'].slice 'firstName', 'lastName', 'email', 'code'
70
+ customer = Mousetrap::Customer.new customer_hash
71
+
72
+ customer.first_name = random_string
73
+ puts customer.save!
74
+
75
+ customer_hash = Mousetrap::Customer['maasdxgliu@example.com']
76
+ puts customer_hash.to_yaml
77
+
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__) + '/../../lib/mousetrap'
2
+ require 'activesupport'
3
+ require 'factory_girl'
4
+ require 'ruby-debug'
5
+ require 'yaml'
6
+
7
+ Dir["#{File.dirname(__FILE__)}/../support/**/*.rb"].each {|f| require f}
8
+
9
+ settings = YAML.load_file(File.dirname(__FILE__) + '/settings.yml')
10
+ Mousetrap.authenticate(settings['user'], settings['password'])
11
+ Mousetrap.product_code = settings['product_code']
12
+
13
+ def plans
14
+ all_plans = Mousetrap::Plan.all
15
+ puts all_plans.to_yaml
16
+
17
+ test_plan = Mousetrap::Plan['TEST']
18
+ puts test_plan.to_yaml
19
+ end
20
+
21
+
22
+ def customers
23
+ all_customers = Mousetrap::Customer.all
24
+ puts all_customers.to_yaml
25
+ all_customers.each { |c| c.destroy }
26
+
27
+ customer = Factory :new_customer
28
+ customer.save
29
+
30
+ api_customer = Mousetrap::Customer[customer.code]
31
+ puts api_customer.to_yaml
32
+
33
+ all_customers = Mousetrap::Customer.all
34
+ puts all_customers.to_yaml
35
+ end
36
+
37
+ customers
38
+
39
+
40
+
41
+
42
+
43
+ __END__
44
+
45
+
46
+
47
+ all_customers = Mousetrap::Customer.all
48
+ puts all_customers.inspect
49
+ puts all_customers.to_yaml
50
+
51
+
52
+ code = 'maasdxgliu@example.com'
53
+ c = Mousetrap::Customer[code]
54
+ puts c.to_yaml
55
+ c.destroy
56
+
57
+ puts '-' * 80
58
+ c = Mousetrap::Customer[code]
59
+ puts c.to_yaml
60
+
61
+
62
+
63
+
64
+
65
+ email = random_email_address
66
+ attributes = {
67
+ 'code' => email,
68
+ 'firstName' => 'Example',
69
+ 'lastName' => 'Customer',
70
+ 'email' => email,
71
+ 'subscription' => {
72
+ 'planCode' => 'TEST',
73
+ 'ccFirstName' => 'Jon',
74
+ 'ccLastName' => 'Larkowski',
75
+ 'ccNumber' => '4111111111111111',
76
+ 'ccExpiration' => '12-2012',
77
+ 'ccZip' => '90210'
78
+ }
79
+ }
80
+
81
+ customer = Mousetrap::Customer.create attributes
82
+ puts customer
83
+
84
+
85
+
86
+
87
+ customers_hash = Mousetrap::Customer['maasdxgliu@example.com']
88
+ customer_hash = customers_hash['customers']['customer'].slice 'firstName', 'lastName', 'email', 'code'
89
+ customer = Mousetrap::Customer.new customer_hash
90
+
91
+ customer.first_name = random_string
92
+ puts customer.save!
93
+
@@ -1,19 +1,126 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe Mousetrap::Customer do
4
- subject { Mousetrap::Customer }
5
-
6
- describe '.all' do
7
- it 'gets customers resources' do
8
- subject.should_receive(:get_resources).with('customers')
9
- subject.all
10
- end
4
+ def customer_attributes_for_api(customer)
5
+ {
6
+ :firstName => customer.first_name,
7
+ :lastName => customer.last_name,
8
+ :email => customer.email,
9
+ :code => customer.code,
10
+ :subscription => {
11
+ :planCode => customer.subscription.plan_code,
12
+ :ccFirstName => customer.subscription.billing_first_name,
13
+ :ccLastName => customer.subscription.billing_last_name,
14
+ :ccNumber => customer.subscription.credit_card_number,
15
+ :ccExpiration => customer.subscription.credit_card_expiration,
16
+ :ccZip => customer.subscription.billing_zip_code,
17
+ }
18
+ }
11
19
  end
12
-
20
+
13
21
  describe '.create' do
14
- it "creates a customer" do
15
- subject.should_receive(:post_resource).with('customers', 'new', 'some_hash')
16
- subject.create('some_hash')
22
+ before do
23
+ @customer_hash = Factory.attributes_for :new_customer
24
+ @customer = Mousetrap::Customer.new @customer_hash
25
+ @customer.stub(:save)
26
+ Mousetrap::Customer.stub(:new => @customer)
27
+ Mousetrap::Customer.stub(:build_resource_from => stub(:id => 0))
28
+ end
29
+
30
+ it 'instantiates a customer with a hash of attributes' do
31
+ Mousetrap::Customer.should_receive(:new).with(@customer_hash).and_return(@customer)
32
+ Mousetrap::Customer.create(@customer_hash)
33
+ end
34
+
35
+ it 'saves the new customer instance' do
36
+ @customer.should_receive(:save)
37
+ Mousetrap::Customer.create(@customer_hash)
38
+ end
39
+
40
+ it 'sets the id of the newly created customer' do
41
+ Mousetrap::Customer.stub(:build_resource_from => stub(:id => 1))
42
+ @customer.should_receive(:id=).with(1)
43
+ Mousetrap::Customer.create(@customer_hash)
44
+ end
45
+
46
+ it 'returns an instance of Mousetrap::Customer' do
47
+ Mousetrap::Customer.create(@customer_hash).should be_instance_of(Mousetrap::Customer)
48
+ end
49
+ end
50
+
51
+ describe ".new" do
52
+ subject do
53
+ Mousetrap::Customer.new \
54
+ :first_name => 'Jon',
55
+ :last_name => 'Larkowski',
56
+ :email => 'lark@example.com',
57
+ :code => 'asfkhw0'
58
+ end
59
+
60
+ it { should be_instance_of(Mousetrap::Customer) }
61
+ it { should be_new_record }
62
+
63
+ describe "sets" do
64
+ it 'first_name' do
65
+ subject.first_name.should == 'Jon'
66
+ end
67
+
68
+ it 'last_name' do
69
+ subject.last_name.should == 'Larkowski'
70
+ end
71
+
72
+ it 'email' do
73
+ subject.email.should == 'lark@example.com'
74
+ end
75
+
76
+ it 'code' do
77
+ subject.code.should == 'asfkhw0'
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '#destroy' do
83
+ context "for existing records" do
84
+ it 'destroys' do
85
+ customer = Factory :existing_customer
86
+ Mousetrap::Customer.should_receive(:delete_resource).with('customers', customer.code)
87
+ customer.destroy
88
+ end
89
+ end
90
+
91
+ context "for new records" do
92
+ it "does nothing" do
93
+ customer = Factory :new_customer
94
+ Mousetrap::Customer.should_not_receive(:delete_resource)
95
+ customer.destroy
96
+ end
97
+ end
98
+ end
99
+
100
+ describe '#save' do
101
+ context "for existing records" do
102
+ before do
103
+ @customer = Factory :existing_customer
104
+ end
105
+
106
+ it 'posts to edit-customer action' do
107
+ attributes_for_api = customer_attributes_for_api(@customer)
108
+
109
+ # We don't send code for existing API resources.
110
+ attributes_for_api.delete(:code)
111
+
112
+ @customer.class.should_receive(:put_resource).with(
113
+ 'customers', 'edit-customer', @customer.code, attributes_for_api)
114
+ @customer.save
115
+ end
116
+ end
117
+
118
+ context "for new records" do
119
+ it 'calls create' do
120
+ customer = Factory :new_customer
121
+ customer.should_receive(:create)
122
+ customer.save
123
+ end
17
124
  end
18
125
  end
19
126
  end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Mousetrap::Plan do
4
+ # name: Test
5
+ # billingFrequencyQuantity: "1"
6
+ # code: TEST
7
+ # recurringChargeAmount: "42.00"
8
+ # createdDatetime: "2009-08-25T04:24:34+00:00"
9
+ # id: 5fbb9a84-e27f-102c-a92d-40402145ee8b
10
+ # isActive: "1"
11
+ # billingFrequency: monthly
12
+ # description: Test
13
+ # trialDays: "0"
14
+ # setupChargeCode: TEST_SETUP
15
+ # recurringChargeCode: TEST_RECURRING
16
+ # billingFrequencyUnit: months
17
+ # setupChargeAmount: "0.00"
18
+ # billingFrequencyPer: month
19
+ end
@@ -1,12 +1,37 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
+ module Mousetrap
4
+ class Widget < Resource
5
+ attr_accessor :id
6
+ attr_accessor :code
7
+
8
+
9
+ protected
10
+
11
+ def self.attributes_from_api(attributes)
12
+ {
13
+ :id => attributes['id'],
14
+ :code => attributes['code'],
15
+ }
16
+ end
17
+
18
+ def self.plural_resource_name
19
+ 'widgets'
20
+ end
21
+
22
+ def self.singular_resource_name
23
+ 'widget'
24
+ end
25
+ end
26
+ end
27
+
3
28
  describe Mousetrap::Resource do
4
29
  before do
5
30
  Mousetrap.product_code = 'my_product_code'
6
31
  end
7
-
32
+
8
33
  subject { Mousetrap::Resource }
9
-
34
+
10
35
  describe "class" do
11
36
  it "sets the base URI" do
12
37
  subject.default_options[:base_uri].should == 'https://cheddargetter.com'
@@ -17,6 +42,57 @@ describe Mousetrap::Resource do
17
42
  end
18
43
  end
19
44
 
45
+ describe ".[]" do
46
+ before do
47
+ customer_hash = Factory.attributes_for :existing_customer
48
+ customer_hash = HashWithIndifferentAccess.new customer_hash
49
+ @code = customer_hash[:code]
50
+ @server_response_hash = { 'widgets' => { 'widget' => customer_hash } }
51
+ Mousetrap::Widget.stub(:get_resource => @server_response_hash)
52
+ end
53
+
54
+ it "gets a resource with widget code" do
55
+ Mousetrap::Widget.should_receive(:get_resource).with('widgets', @code).and_return(@server_response_hash)
56
+ Mousetrap::Widget[@code]
57
+ end
58
+
59
+ context "returned widget instance" do
60
+ subject { Mousetrap::Widget[@code] }
61
+ it { should be_instance_of(Mousetrap::Widget) }
62
+ it { should_not be_new_record }
63
+ it { subject.code.should == @code }
64
+ end
65
+ end
66
+
67
+ describe '.all' do
68
+ it 'gets widgets resources' do
69
+ Mousetrap::Widget.stub(:build_resources_from)
70
+ Mousetrap::Widget.should_receive(:get_resources).with('widgets')
71
+ Mousetrap::Widget.all
72
+ end
73
+
74
+ it 'builds widget resources' do
75
+ response = stub
76
+ Mousetrap::Widget.stub(:get_resources => response)
77
+ Mousetrap::Widget.should_receive(:build_resources_from).with(response)
78
+ Mousetrap::Widget.all
79
+ end
80
+ end
81
+
82
+ describe ".delete_resource" do
83
+ it "gets /xml/<resource>/delete/productCode/<my_product_code>/code/<resource_code>" do
84
+ subject.should_receive(:post).with('/xml/widgets/delete/productCode/my_product_code/code/some_resource_code')
85
+ subject.delete_resource 'widgets', 'some_resource_code'
86
+ end
87
+ end
88
+
89
+ describe ".get_resource" do
90
+ it "gets /xml/<resource>/get/productCode/<my_product_code>/code/<resource_code>" do
91
+ subject.should_receive(:get).with('/xml/widgets/get/productCode/my_product_code/code/some_resource_code')
92
+ subject.get_resource 'widgets', 'some_resource_code'
93
+ end
94
+ end
95
+
20
96
  describe ".get_resources" do
21
97
  it "gets /xml/<resource>/get/productCode/<my_product_code>" do
22
98
  subject.should_receive(:get).with('/xml/widgets/get/productCode/my_product_code')
@@ -30,4 +106,13 @@ describe Mousetrap::Resource do
30
106
  subject.post_resource 'widgets', 'some_action', 'some_hash'
31
107
  end
32
108
  end
109
+
110
+ describe ".put_resource" do
111
+ it "put to /xml/<resource>/<action>/productCode/<product_code>/code/<resource_code>" do
112
+ subject.should_receive(:post).with(
113
+ '/xml/widgets/some_action/productCode/my_product_code/code/some_widget_code',
114
+ :body => 'some_hash')
115
+ subject.put_resource 'widgets', 'some_action', 'some_widget_code', 'some_hash'
116
+ end
117
+ end
33
118
  end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Mousetrap::Subscription do
4
+ # subscription:
5
+ # ccExpirationDate: "2010-01-31T00:00:00+00:00"
6
+ # gatewayToken:
7
+ # createdDatetime: "2009-08-27T15:55:51+00:00"
8
+ # ccType: visa
9
+ # id: 46ad3f1c-e472-102c-a92d-40402145ee8b
10
+ # ccLastFour: "1111"
11
+ # canceledDatetime:
12
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,25 +3,21 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'mousetrap'
4
4
  require 'spec'
5
5
  require 'spec/autorun'
6
+ require 'factory_girl'
7
+ require 'active_support'
6
8
 
7
- def random_email_address
8
- "#{random_string}@example.com"
9
- end
10
-
11
- def random_string
12
- random_alpha_string_of_length(10)
13
- end
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
14
12
 
15
- def random_alpha_string_of_length(length)
16
- letters = *'a'..'z'
17
- random_string_for_uniqueness = ''
18
- length.times { random_string_for_uniqueness += letters[rand(letters.size - 1)]}
19
- random_string_for_uniqueness
20
- end
13
+ module Mousetrap
14
+ class Resource
15
+ def self.get(*args)
16
+ raise 'You must stub this, or should_receive at a higher level.'
17
+ end
21
18
 
22
- def random_number_string_of_length(length)
23
- numbers = *'0'..'9'
24
- random_string_for_uniqueness = ''
25
- length.times { random_string_for_uniqueness += numbers[rand(numbers.size - 1)]}
26
- random_string_for_uniqueness
19
+ def self.post(*args)
20
+ raise 'You must stub this, or should_receive at a higher level.'
21
+ end
22
+ end
27
23
  end
@@ -0,0 +1,21 @@
1
+ def random_email_address
2
+ "#{random_string}@example.com"
3
+ end
4
+
5
+ def random_string
6
+ random_alpha_string_of_length(10)
7
+ end
8
+
9
+ def random_alpha_string_of_length(length)
10
+ letters = *'a'..'z'
11
+ random_string_for_uniqueness = ''
12
+ length.times { random_string_for_uniqueness += letters[rand(letters.size - 1)]}
13
+ random_string_for_uniqueness
14
+ end
15
+
16
+ def random_number_string_of_length(length)
17
+ numbers = *'0'..'9'
18
+ random_string_for_uniqueness = ''
19
+ length.times { random_string_for_uniqueness += numbers[rand(numbers.size - 1)]}
20
+ random_string_for_uniqueness
21
+ end
metadata CHANGED
@@ -1,18 +1,40 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashrocket-mousetrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Larkowski
8
8
  - Sandro Turriate
9
+ - Wolfram Arnold
10
+ - Corey Grusden
9
11
  autorequire:
10
12
  bindir: bin
11
13
  cert_chain: []
12
14
 
13
- date: 2009-08-25 00:00:00 -07:00
15
+ date: 2009-09-01 00:00:00 -07:00
14
16
  default_executable:
15
17
  dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: httparty
20
+ type: :runtime
21
+ version_requirement:
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.4
27
+ version:
28
+ - !ruby/object:Gem::Dependency
29
+ name: activesupport
30
+ type: :development
31
+ version_requirement:
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.3.3
37
+ version:
16
38
  - !ruby/object:Gem::Dependency
17
39
  name: rspec
18
40
  type: :development
@@ -24,14 +46,14 @@ dependencies:
24
46
  version: 1.2.8
25
47
  version:
26
48
  - !ruby/object:Gem::Dependency
27
- name: httparty
28
- type: :runtime
49
+ name: thoughtbot-factory_girl
50
+ type: :development
29
51
  version_requirement:
30
52
  version_requirements: !ruby/object:Gem::Requirement
31
53
  requirements:
32
54
  - - ">="
33
55
  - !ruby/object:Gem::Version
34
- version: 0.4.4
56
+ version: 1.2.2
35
57
  version:
36
58
  description: CheddarGetter API Client in Ruby
37
59
  email: jonlarkowski@gmail.com
@@ -50,18 +72,24 @@ files:
50
72
  - VERSION
51
73
  - lib/mousetrap.rb
52
74
  - lib/mousetrap/customer.rb
75
+ - lib/mousetrap/plan.rb
53
76
  - lib/mousetrap/resource.rb
77
+ - lib/mousetrap/subscription.rb
54
78
  - mousetrap.gemspec
79
+ - spec/factories.rb
55
80
  - spec/integration/settings.example.yml
56
81
  - spec/integration/smoke_test.rb
82
+ - spec/integration/spike.rb
57
83
  - spec/mousetrap/customer_spec.rb
84
+ - spec/mousetrap/plan_spec.rb
58
85
  - spec/mousetrap/resource_spec.rb
86
+ - spec/mousetrap/subscription_spec.rb
59
87
  - spec/mousetrap_spec.rb
60
88
  - spec/spec.opts
61
89
  - spec/spec_helper.rb
90
+ - spec/support/random_data.rb
62
91
  has_rdoc: false
63
- homepage: http://github.com/l4rk/mousetrap
64
- licenses:
92
+ homepage: http://github.com/hashrocket/mousetrap
65
93
  post_install_message:
66
94
  rdoc_options:
67
95
  - --charset=UTF-8
@@ -82,13 +110,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
110
  requirements: []
83
111
 
84
112
  rubyforge_project:
85
- rubygems_version: 1.3.5
113
+ rubygems_version: 1.2.0
86
114
  signing_key:
87
115
  specification_version: 3
88
116
  summary: CheddarGetter API Client in Ruby
89
117
  test_files:
118
+ - spec/factories.rb
90
119
  - spec/integration/smoke_test.rb
120
+ - spec/integration/spike.rb
91
121
  - spec/mousetrap/customer_spec.rb
122
+ - spec/mousetrap/plan_spec.rb
92
123
  - spec/mousetrap/resource_spec.rb
124
+ - spec/mousetrap/subscription_spec.rb
93
125
  - spec/mousetrap_spec.rb
94
126
  - spec/spec_helper.rb
127
+ - spec/support/random_data.rb