Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Customer Metadata #110

Closed
wants to merge 14 commits into from
13 changes: 12 additions & 1 deletion examples/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
c.api_key = ENV['CHARGIFY_API_KEY']
end

##### Subscription Metadata

subscription = Chargify::Subscription.last

## Listing all of your customer metafields
## Listing all of this subscription's metadata
metadata = subscription.metadata
# => [#<Chargify::SubscriptionMetadata resource_id: 22, current_name: favorite color, name: favorite color, value: red>]

Expand Down Expand Up @@ -56,3 +58,12 @@

data
# => #<Chargify::SubscriptionMetadata resource_id: 22, current_name: job 1234, name: job 1234, value: 2014-08-21 02:10:46 UTC>

##### Customer Metadata

customer = Chargify::Customer.last

## Listing all of this customer's metadata
metadata = customer.metadata

# See above examples for subscription metadata; the same methods are available
1 change: 1 addition & 0 deletions lib/chargify_api_ares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'chargify_api_ares/resources/charge'
require 'chargify_api_ares/resources/component'
require 'chargify_api_ares/resources/coupon'
require 'chargify_api_ares/resources/customer_metadata'
require 'chargify_api_ares/resources/customer'
require 'chargify_api_ares/resources/event'
require 'chargify_api_ares/resources/migration'
Expand Down
13 changes: 13 additions & 0 deletions lib/chargify_api_ares/resources/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,18 @@ def payment_profiles(params = {})
params.merge!({:customer_id => self.id})
PaymentProfile.find(:all, :params => params)
end

def build_metadata(params = {})
CustomerMetadata.new(params.reverse_merge({:resource_id => self.id}))
end

def create_metadata(params = {})
CustomerMetadata.create(params.reverse_merge({:resource_id => self.id}))
end

def metadata(params={})
params.merge!({:resource_id => self.id})
CustomerMetadata.find(:all, :params => params)
end
end
end
17 changes: 17 additions & 0 deletions lib/chargify_api_ares/resources/customer_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Chargify
class CustomerMetadata < Base
include ::Chargify::Behaviors::Inspectable
include ::Chargify::Behaviors::Metadata

self.inspect_class = "resource_id: integer, current_name: string, name: string, value: string"
self.inspect_instance = Proc.new { |s| [[:resource_id, s.prefix_options[:resource_id]], [:current_name, s.current_name], [:name, s.name], [:value, s.value]] }
self.prefix = '/customers/:resource_id/'
self.endpoint_name = 'metadata'

schema do
attribute 'current_name', :string
attribute 'name', :string
attribute 'value', :string
end
end
end
24 changes: 23 additions & 1 deletion spec/remote/remote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@
context "metadata" do
before { subscription_to_pro }
let(:subscription) { Chargify::Subscription.last }
let(:customer) { Chargify::Customer.last }

describe 'listing metadata for a subscription' do
it 'returns a list of metadata' do
Expand All @@ -776,7 +777,7 @@
end
end

describe 'creating a piece of metadata' do
describe 'creating a piece of subscription metadata' do
it 'can create a new metadata' do
data = subscription.create_metadata(:name => 'favorite color', :value => 'red')

Expand All @@ -789,6 +790,27 @@
expect(list).to include(data)
end
end

describe 'listing metadata for a customer' do
it 'returns a list of metadata' do
list = customer.metadata
expect(list).to eql([])
end
end

describe 'creating a piece of customer metadata' do
it 'can create a new metadata' do
data = customer.create_metadata(:name => 'favorite color', :value => 'blue')

expect(data).to be_persisted
expect(data.name).to eql('favorite color')
expect(data.value).to eql('blue')

list = customer.metadata
expect(list.size).to eql(1)
expect(list).to include(data)
end
end
end

def clear_site_data
Expand Down
13 changes: 13 additions & 0 deletions spec/resources/customer_metadata_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe Chargify::CustomerMetadata do

describe '.inspect' do
specify { expect(described_class.inspect).to eql("Chargify::CustomerMetadata(resource_id: integer, current_name: string, name: string, value: string)") }
end

describe "#inspect" do
its(:inspect) { should eql("#<Chargify::CustomerMetadata resource_id: nil, current_name: nil, name: nil, value: nil>") }
end

end