chargify_api_ares 1.1.0.pre → 1.1.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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.env +4 -0
  3. data/.gitignore +1 -0
  4. data/.rspec +1 -0
  5. data/.travis.yml +9 -0
  6. data/Gemfile +1 -1
  7. data/Gemfile.lock +7 -1
  8. data/HISTORY.md +9 -0
  9. data/README.md +1 -1
  10. data/chargify_api_ares.gemspec +4 -2
  11. data/examples/metadata.rb +58 -0
  12. data/examples/metafields.rb +32 -0
  13. data/lib/chargify_api_ares.rb +4 -2
  14. data/lib/chargify_api_ares/behaviors/inspectable.rb +33 -0
  15. data/lib/chargify_api_ares/behaviors/metadata.rb +61 -0
  16. data/lib/chargify_api_ares/behaviors/metafield.rb +30 -0
  17. data/lib/chargify_api_ares/resources/customer_metafield.rb +14 -1
  18. data/lib/chargify_api_ares/resources/invoice.rb +4 -4
  19. data/lib/chargify_api_ares/resources/subscription.rb +26 -3
  20. data/lib/chargify_api_ares/resources/subscription_metadata.rb +17 -0
  21. data/lib/chargify_api_ares/resources/subscription_metafield.rb +14 -1
  22. data/spec/cassettes/subscription/find.yml +266 -0
  23. data/spec/cassettes/subscription_metadata/create.yml +78 -0
  24. data/spec/cassettes/subscription_metadata/list-after-create.yml +77 -0
  25. data/spec/cassettes/subscription_metadata/list.yml +71 -0
  26. data/spec/factories.rb +1 -0
  27. data/spec/resources/customer_metafield_spec.rb +64 -0
  28. data/spec/resources/customer_spec.rb +4 -4
  29. data/spec/resources/subscription_metadata_spec.rb +54 -0
  30. data/spec/resources/subscription_metafield_spec.rb +63 -0
  31. data/spec/resources/subscription_spec.rb +27 -0
  32. data/spec/spec_helper.rb +14 -2
  33. metadata +78 -31
  34. data/lib/chargify_api_ares/metafield_xml_formatter.rb +0 -10
  35. data/lib/chargify_api_ares/resources/metafield.rb +0 -67
  36. data/spec/spec.opts +0 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01809c6f4c91b20f5829c4fdb5c2b08431e14289
4
- data.tar.gz: 1d3b7638b00c56e4ab7a135c284264d173069343
3
+ metadata.gz: 487c9d4aadf7ce63e428362757dd6b64b49d9821
4
+ data.tar.gz: 02b33591e024bb01088183d31e2c1fca4eefe0d3
5
5
  SHA512:
6
- metadata.gz: 6a84cbe5f8e4bb0d8fb999a5f9c033a1d903210205311213a93ff7e64705d9b6e71f71404c2637bbceaacfa68c67ff135e9c6a0014b5f023dac44b1774b158a6
7
- data.tar.gz: 0f7553535d73a840bca38e7cefa0550de4f747ecf311983bb5559c5b44e9d0d32a1df3a4a609eaa2924dca785a4a5a3ec620197193b82c0536abfdd18ab6c3d7
6
+ metadata.gz: be4cd1efd96a30df19cd7711e17f643e19fb2cd8fe3d28f1cf0f980a1036bf9964576184eed62d2d4f4207cfe4c91b1468a80a976ec0ecf4ad4c356797660245
7
+ data.tar.gz: f505f8cf81d1c16a66a60da6c087f8d39a47d6722a2db1bcebcf6b46c73f1edb2e94124c9e816ee27f807148aba7eaaa170153b03e3e85726f4ccfcf2b366114
data/.env ADDED
@@ -0,0 +1,4 @@
1
+ API_KEY='acme_test_key'
2
+ SUBDOMAIN='acme'
3
+ DOMAIN='chargify.dev'
4
+ PROTOCOL='http'
data/.gitignore CHANGED
@@ -6,3 +6,4 @@ pkg
6
6
  .bundle/
7
7
  bin/
8
8
  .rbenv-version
9
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --fail-fast --format d --color
data/.travis.yml CHANGED
@@ -1,7 +1,16 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.1.0
3
4
  - 2.0.0
4
5
  - 1.9.3
5
6
  - 1.9.2
6
7
  - 1.8.7
7
8
  - ree
9
+ - jruby
10
+ - ruby-head
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: jruby
14
+ - rvm: ruby-head
15
+ fast_finish: true
16
+
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in chargify_api_ares.gemspec
4
4
  gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chargify_api_ares (1.0.5)
4
+ chargify_api_ares (1.1.0)
5
5
  activeresource (~> 3.2.16)
6
6
 
7
7
  GEM
@@ -19,6 +19,9 @@ GEM
19
19
  builder (3.0.4)
20
20
  coderay (1.0.8)
21
21
  diff-lcs (1.1.3)
22
+ dotenv (0.11.1)
23
+ dotenv-deployment (~> 0.0.2)
24
+ dotenv-deployment (0.0.2)
22
25
  factory_girl (2.6.4)
23
26
  activesupport (>= 2.3.9)
24
27
  faker (1.1.2)
@@ -56,12 +59,14 @@ GEM
56
59
  slop (3.4.3)
57
60
  terminal-table (1.4.5)
58
61
  thor (0.17.0)
62
+ vcr (2.9.2)
59
63
 
60
64
  PLATFORMS
61
65
  ruby
62
66
 
63
67
  DEPENDENCIES
64
68
  chargify_api_ares!
69
+ dotenv
65
70
  factory_girl (~> 2.6)
66
71
  faker (~> 1.1.2)
67
72
  fakeweb (~> 1.3.0)
@@ -71,3 +76,4 @@ DEPENDENCIES
71
76
  rake (~> 10.0.3)
72
77
  rb-fsevent (~> 0.9.2)
73
78
  rspec (~> 2.12.0)
79
+ vcr
data/HISTORY.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 1.1.0 / Aug 20 2014
2
+
3
+ * Added cancellation message option for subscription canceling
4
+ * restore 1.8.7 style hash syntax
5
+ * Add metafields and metadata
6
+ * Remove 'bank\_account' attribute from subscription upon save
7
+ * Update Gemfile source to https://rubygems.org due to Bundler deprecation
8
+ * Update payment\_profile on subscriptions to return credit\_card or bank\_account
9
+
1
10
  ## 1.0.5 / May 11 2014
2
11
 
3
12
  ### Backwards-incompatible changes
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  Chargify API wrapper for Ruby (using ActiveResource)
2
2
  ====================================================
3
- [![build status](https://secure.travis-ci.org/chargify/chargify_api_ares.png)](http://travis-ci.org/chargify/chargify_api_ares)
3
+ [![build status](https://secure.travis-ci.org/chargify/chargify_api_ares.png)](http://travis-ci.org/chargify/chargify_api_ares) [![Code Climate](https://codeclimate.com/github/chargify/chargify_api_ares/badges/gpa.svg)](https://codeclimate.com/github/chargify/chargify_api_ares)
4
4
 
5
5
  **Note:** we have bumped to v1.0.0 to indicate a backwards incompatible change to the responses from `Chargify::Subscription.charge` and `Chargify::Subscription.migrate`. Please see the HISTORY.md for more information.
6
6
 
@@ -4,8 +4,8 @@ Gem::Specification.new do |s|
4
4
  s.rubygems_version = '1.3.7'
5
5
 
6
6
  s.name = 'chargify_api_ares'
7
- s.version = '1.1.0.pre'
8
- s.date = '2014-08-08'
7
+ s.version = '1.1.0'
8
+ s.date = '2014-08-20'
9
9
  s.summary = 'A Chargify API wrapper for Ruby using ActiveResource'
10
10
  s.description = ''
11
11
  s.authors = ["Michael Klett", "Nathan Verni", "Jeremy W. Rowe", "Eric Farkas"]
@@ -29,4 +29,6 @@ Gem::Specification.new do |s|
29
29
  s.add_development_dependency('growl', '~> 1.0.3')
30
30
  s.add_development_dependency('rb-fsevent', '~> 0.9.2')
31
31
  s.add_development_dependency('pry')
32
+ s.add_development_dependency('vcr')
33
+ s.add_development_dependency('dotenv')
32
34
  end
@@ -0,0 +1,58 @@
1
+ require 'chargify_api_ares'
2
+
3
+ Chargify.configure do |c|
4
+ c.subdomain = ENV['CHARGIFY_SUBDOMAIN']
5
+ c.api_key = ENV['CHARGIFY_API_KEY']
6
+ end
7
+
8
+ subscription = Chargify::Subscription.last
9
+
10
+ ## Listing all of your customer metafields
11
+ metadata = subscription.metadata
12
+ # => [#<Chargify::SubscriptionMetadata resource_id: 22, current_name: favorite color, name: favorite color, value: red>]
13
+
14
+ ## creating new metadata
15
+ subscription.create_metadata name: 'shoe size', value: '11w'
16
+ # => #<Chargify::SubscriptionMetadata resource_id: 22, current_name: shoe size, name: shoe size, value: 11w>
17
+ subscription.metadata
18
+ # => [#<Chargify::SubscriptionMetadata resource_id: 22, current_name: favorite color, name: favorite color, value: red>,
19
+ # #<Chargify::SubscriptionMetadata resource_id: 22, current_name: shoe size, name: shoe size, value: 11w>]]
20
+
21
+ ## updating metadata value
22
+ data = subscription.metadata.detect { |d| d.name == "shoe size" }
23
+ # => #<Chargify::SubscriptionMetadata resource_id: 22, current_name: shoe size, name: shoe size, value: 11w>
24
+ data.value = '10'
25
+ data.save # => true
26
+ data
27
+ # => #<Chargify::SubscriptionMetadata resource_id: 22, current_name: shoe size, name: shoe size, value: 10>
28
+
29
+ ## updating metadata name
30
+ data.name = 'height'
31
+ data.save # => true
32
+ data
33
+ # => #<Chargify::SubscriptionMetadata resource_id: 22, current_name: height, name: height, value: 10>
34
+
35
+ subscription.metadata
36
+ # => [#<Chargify::SubscriptionMetadata resource_id: 22, current_name: favorite color, name: favorite color, value: red>,
37
+ #<Chargify::SubscriptionMetadata resource_id: 22, current_name: height, name: height, value: 10>]]
38
+
39
+ # note - current_name is for internal tracking, and should not be changed. If you want to update the name of a metadata use the name field.
40
+ # - metadata can be created or updated by using create_metadata on the subscription. This is so you don't have to do a look up if you know
41
+ # the metadata name a head of time.
42
+
43
+ # if you wish to make a new metadata without saving the record until later you can use the build_metadata method
44
+ # building a metadata
45
+
46
+ data = subscription.build_metadata
47
+ # => #<Chargify::SubscriptionMetadata resource_id: 22, current_name: nil, name: nil, value: nil>
48
+ data.persisted? # => false
49
+
50
+ # do some internal work... and it returns results that you want to store it with a subscription
51
+
52
+ data.name = internal_work.computed_name
53
+ data.value = Time.now
54
+ data.save # => true
55
+ data.persisted? # => true
56
+
57
+ data
58
+ # => #<Chargify::SubscriptionMetadata resource_id: 22, current_name: job 1234, name: job 1234, value: 2014-08-21 02:10:46 UTC>
@@ -0,0 +1,32 @@
1
+ require 'chargify_api_ares'
2
+
3
+ Chargify.configure do |c|
4
+ c.subdomain = ENV['CHARGIFY_SUBDOMAIN']
5
+ c.api_key = ENV['CHARGIFY_API_KEY']
6
+ end
7
+
8
+ ## Listing all of your customer metafields
9
+ metafields = Chargify::CustomerMetafield.all
10
+ # => [#<Chargify::CustomerMetafield current_name: favorite color, name: favorite color>]
11
+
12
+ ## Creating a new customer metafield
13
+ field = Chargify::CustomerMetafield.create name: 'test'
14
+ # => #<Chargify::CustomerMetafield current_name: test, name: test>
15
+ field.persisted? # => true
16
+
17
+ ## Updating a persisted metafield
18
+ field.name = 'new name'
19
+ field.on_csv_export = true
20
+ field.on_hosted_pages = 'all'
21
+ field.save # => true
22
+
23
+ field.on_csv_export? # => true
24
+ field.on_hosted_pages # => ["all"]
25
+
26
+ ## Listing all of your subscription metafields
27
+ metafields = Chargify::SubscriptionMetafield.all
28
+ # => [#<Chargify::SubscriptionMetafield current_name: favorite color, name: favorite color>]
29
+
30
+ ## Creating a new customer metafield
31
+ field = Chargify::SubscriptionMetafield.create name: 'internal info'
32
+ # => #<Chargify::SubscriptionMetafield current_name: internal info, name: internal info>
@@ -1,7 +1,9 @@
1
1
  require 'active_resource'
2
2
  require 'chargify_api_ares/config'
3
+ require 'chargify_api_ares/behaviors/inspectable'
4
+ require 'chargify_api_ares/behaviors/metadata'
5
+ require 'chargify_api_ares/behaviors/metafield'
3
6
  require 'chargify_api_ares/response_helper'
4
- require 'chargify_api_ares/metafield_xml_formatter'
5
7
  require 'chargify_api_ares/resources/base'
6
8
  require 'chargify_api_ares/resources/charge'
7
9
  require 'chargify_api_ares/resources/component'
@@ -15,12 +17,12 @@ require 'chargify_api_ares/resources/product_family'
15
17
  require 'chargify_api_ares/resources/renewal_preview'
16
18
  require 'chargify_api_ares/resources/site'
17
19
  require 'chargify_api_ares/resources/statement'
20
+ require 'chargify_api_ares/resources/subscription_metadata'
18
21
  require 'chargify_api_ares/resources/subscription'
19
22
  require 'chargify_api_ares/resources/transaction'
20
23
  require 'chargify_api_ares/resources/usage'
21
24
  require 'chargify_api_ares/resources/webhook'
22
25
  require 'chargify_api_ares/resources/invoice'
23
- require 'chargify_api_ares/resources/metafield'
24
26
  require 'chargify_api_ares/resources/customer_metafield'
25
27
  require 'chargify_api_ares/resources/subscription_metafield'
26
28
 
@@ -0,0 +1,33 @@
1
+ module Chargify
2
+ module Behaviors
3
+ module Inspectable
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ def inspect
9
+ vals = self.class.inspect_instance.call(self)
10
+ vals = vals.map{|a| "#{a[0]}: #{nil_or_value(a[1])}"}.join(", ")
11
+ "#<#{self.class.name} #{vals}>"
12
+ end
13
+
14
+ private
15
+
16
+ def nil_or_value(value)
17
+ value || 'nil'
18
+ end
19
+
20
+ module ClassMethods
21
+ def inspect_instance=(value); @inspect_instance = value; end
22
+ def inspect_instance; @inspect_instance; end
23
+
24
+ def inspect_class=(value); @inspect_class = value; end
25
+ def inspect_class; @inspect_class; end
26
+
27
+ def inspect
28
+ "#{self.name}(#{self.inspect_class})"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,61 @@
1
+ module Chargify
2
+ module Behaviors
3
+ module Metadata
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ def destroy
9
+ connection.delete("#{element_path}?current_name=#{URI.encode(current_name)}")
10
+ end
11
+
12
+ def resource_id=(value)
13
+ self.prefix_options = {:resource_id => value}
14
+ end
15
+
16
+ def element_path(id = nil, pre_options = {}, query_options = {})
17
+ collection_path(prefix_options)
18
+ end
19
+
20
+ def convert_attributes(attributes)
21
+ attributes.is_a?(Array) ? attributes.first : attributes
22
+ end
23
+
24
+ def load_attributes_from_response(response)
25
+ if (response_code_allows_body?(response.code) &&
26
+ (response['Content-Length'].nil? || response['Content-Length'] != "0") &&
27
+ !response.body.nil? && response.body.strip.size > 0)
28
+ attributes = self.class.format.decode(response.body)
29
+
30
+ # This is a major hack, but has to be done for metadata / metafields because we return
31
+ # an array instead of a single object
32
+ attributes = convert_attributes(attributes)
33
+
34
+ resource = load(attributes, true)
35
+ resource.current_name = resource.name
36
+ @persisted = true
37
+ end
38
+ end
39
+
40
+ module ClassMethods
41
+ def endpoint_name=(name)
42
+ (class << self; self; end).send(:define_method, :resource) do; name; end
43
+ (class << self; self; end).send(:define_method, :element_name) do; name; end
44
+ end
45
+
46
+ def instantiate_collection(collection, prefix_options = {})
47
+ collection[self.resource].collect! { |record| instantiate_record(record, prefix_options) }
48
+ end
49
+
50
+ def instantiate_record(record, prefix_options = {})
51
+ record = record.is_a?(Array) ? record.first : record
52
+ new(record, true).tap do |resource|
53
+ resource.prefix_options = prefix_options
54
+ resource.current_name = resource.name
55
+ end
56
+ end
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,30 @@
1
+ module Chargify
2
+ module Behaviors
3
+ module Metafield
4
+ def on_csv_export?
5
+ scope.csv == "1" || scope.csv == true
6
+ end
7
+
8
+ def on_csv_export=(value)
9
+ value = (value == true || value == '1') ? '1' : '0'
10
+ scope.csv = value
11
+ end
12
+
13
+ def on_hosted_pages?
14
+ scope.hosted.any?
15
+ end
16
+
17
+ def on_hosted_pages=(*products)
18
+ scope.hosted = Array(products).flatten.map(&:to_s)
19
+ end
20
+
21
+ def on_hosted_pages
22
+ scope.hosted
23
+ end
24
+
25
+ def reload
26
+ raise NotImplementedError, 'Metafields do not support loading of a single record'
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,18 @@
1
1
  module Chargify
2
- class CustomerMetafield < Metafield
2
+ class CustomerMetafield < Base
3
+ include ::Chargify::Behaviors::Inspectable
4
+ include ::Chargify::Behaviors::Metafield
5
+ include ::Chargify::Behaviors::Metadata
6
+
3
7
  self.prefix = "/customers/"
8
+
9
+ self.inspect_class = "current_name: string, name: string, scope: { hosted: [], csv: boolean }"
10
+ self.inspect_instance = Proc.new {|s| [[:current_name, s.current_name], [:name, s.name]] }
11
+ self.endpoint_name = 'metafields'
12
+
13
+ schema do
14
+ attribute 'current_name', :string
15
+ attribute 'name', :string
16
+ end
4
17
  end
5
18
  end
@@ -2,19 +2,19 @@ module Chargify
2
2
  class Invoice < Base
3
3
 
4
4
  def self.find_by_invoice_id(id)
5
- find(:first, params: {id: id})
5
+ find(:first, {:params => {:id => id}})
6
6
  end
7
7
 
8
8
  def self.find_by_subscription_id(id)
9
- find(:all, params: {subscription_id: id})
9
+ find(:all, {:params => {:subscription_id => id}})
10
10
  end
11
11
 
12
12
  def self.unpaid_from_subscription(subscription_id)
13
- find(:all, params: {subscription_id: subscription_id, state: "unpaid"})
13
+ find(:all, {:params => {:subscription_id => subscription_id, :state => "unpaid"}})
14
14
  end
15
15
 
16
16
  def self.unpaid
17
- find(:all, params: {state: "unpaid"})
17
+ find(:all, {:params => {:state => "unpaid"}})
18
18
  end
19
19
  end
20
20
  end
@@ -12,11 +12,30 @@ module Chargify
12
12
  self.attributes.delete('customer')
13
13
  self.attributes.delete('product')
14
14
  self.attributes.delete('credit_card')
15
+ self.attributes.delete('bank_account')
15
16
  super
16
17
  end
17
18
 
18
- def cancel
19
- destroy
19
+ def cancel(cancellation_message = nil)
20
+ if cancellation_message.nil?
21
+ destroy
22
+ else
23
+ #Destory does not support body, must work around it to send verb DELETE
24
+ self.connection.post(element_path, {:cancellation_message => cancellation_message}.to_xml(:root => :subscription), self.class.headers.merge({'X-Http-Method-Override' => 'DELETE'}))
25
+ end
26
+ end
27
+
28
+ def build_metadata(params = {})
29
+ SubscriptionMetadata.new(params.reverse_merge({:resource_id => self.id}))
30
+ end
31
+
32
+ def create_metadata(params = {})
33
+ SubscriptionMetadata.create(params.reverse_merge({:resource_id => self.id}))
34
+ end
35
+
36
+ def metadata(params={})
37
+ params.merge!({:resource_id => self.id})
38
+ SubscriptionMetadata.find(:all, :params => params)
20
39
  end
21
40
 
22
41
  def component(id)
@@ -34,7 +53,11 @@ module Chargify
34
53
  end
35
54
 
36
55
  def payment_profile
37
- self.respond_to?('credit_card') ? credit_card : nil
56
+ if self.respond_to?('credit_card')
57
+ credit_card
58
+ elsif self.respond_to?('bank_account')
59
+ bank_account
60
+ end
38
61
  end
39
62
 
40
63
  # Perform a one-time charge on an existing subscription.