charger 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,24 +1,44 @@
1
1
  # Charger
2
2
 
3
- TODO: Write a gem description
3
+ A different gem to interact with Chargify's API.
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'charger'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install charger
7
+ ```rb
8
+ gem 'charger'
9
+ ```
18
10
 
19
11
  ## Usage
20
12
 
21
- TODO: Write usage instructions here
13
+ If you are using Rails, you could do the following
14
+
15
+ ```rb
16
+ # config/initializers/charger.rb
17
+ Charger.configure do |config|
18
+ config.site = {
19
+ subdomain: ENV['CHARGIFY_URL'],
20
+ api_key: ENV['CHARGIFY_TOKEN']
21
+ }
22
+ end
23
+
24
+ # Some other file
25
+ mrr = 0.0
26
+ at_risk_mrr = 0.0
27
+
28
+ Charger::Subscription.all.each do |subscription|
29
+ if subscription.live?
30
+ mrr += subscription.mrr
31
+ end
32
+ if subscription.problem?
33
+ at_risk_mrr += subscription.mrr
34
+ end
35
+ end
36
+
37
+ puts "MRR: #{mrr}"
38
+ puts "At Risk MRR: #{at_risk_mrr}"
39
+ ```
40
+
41
+ For more information on what is available, you'll need to check the source code.
22
42
 
23
43
  ## Contributing
24
44
 
@@ -1,31 +1,59 @@
1
- require "charger/version"
2
-
3
1
  module Charger
4
2
  class << self
5
- # Your api key assigned to you buy them
6
- @@api_key = 'notset'
3
+ # The hash of configurations
4
+ @@configurations = {}
7
5
 
8
- # The chargify site
9
- @@subdomain = 'notset'
6
+ # The current client
7
+ @@current = nil
10
8
 
11
9
  def configure
12
10
  yield self
13
11
  end
14
12
  end
15
13
 
16
- def self.api_key
17
- @@api_key
14
+ # Add a configuration to Charger
15
+ #
16
+ # @param [Hash] params
17
+ # @return [void]
18
+ def self.site= params={}
19
+ return unless params[:subdomain] || params[:api_key]
20
+ config = Configuration.new(params)
21
+ @@configurations[config.subdomain] = config
22
+ @@current = config
23
+ end
24
+
25
+ # Switches between configurations. Useful if you want to connect to multiple
26
+ # chargify accounts at once.
27
+ #
28
+ # @return [Boolean]
29
+ def self.switch subdomain
30
+ @@current = @@configurations[subdomain]
31
+ true
32
+ rescue
33
+ false
18
34
  end
19
35
 
20
- def self.api_key= api_key
21
- @@api_key = api_key
36
+ # Clears all configurations
37
+ #
38
+ # @return [void]
39
+ def self.clear
40
+ @@configurations = Hash.new
41
+ @@current = nil
22
42
  end
23
43
 
24
- def self.subdomain
25
- @@subdomain
44
+ # @return [Charger::Client]
45
+ def self.client
46
+ @@current.client
47
+ rescue
48
+ nil
26
49
  end
27
50
 
28
- def self.subdomain= subdomain
29
- @@subdomain = subdomain
51
+ def self.configurations
52
+ @@configurations
30
53
  end
54
+
55
+ def self.current
56
+ @@current
57
+ end
58
+
31
59
  end
@@ -3,9 +3,9 @@ module Charger
3
3
  class Client
4
4
  attr_accessor :api_key, :subdomain
5
5
 
6
- def initialize params={}
7
- @api_key = params[:api_key] ? params[:api_key] : Charger.api_key
8
- @subdomain = params[:subdomain] ? params[:subdomain] : Charger.subdomain
6
+ def initialize api_key, subdomain
7
+ @api_key = api_key
8
+ @subdomain = subdomain
9
9
  end
10
10
 
11
11
  def get resource, headers={}
@@ -1,10 +1,7 @@
1
1
  module Charger
2
2
 
3
3
  class Component
4
- include Virtus
5
- extend ActiveModel::Naming
6
- include ActiveModel::Conversion
7
- include ActiveModel::Validations
4
+ include Resource
8
5
 
9
6
  attribute :id, Integer
10
7
  attribute :name, String
@@ -43,7 +40,6 @@ module Charger
43
40
  end
44
41
 
45
42
  def self.find_by_product_family_id id
46
- client = Client.new
47
43
  components = []
48
44
 
49
45
  client.get("product_families/#{id}/components").each do |data|
@@ -61,7 +57,6 @@ module Charger
61
57
  end
62
58
 
63
59
  def self.find product_family_id, id
64
- client = Client.new
65
60
  new(client.get("product_families/#{product_family_id}/components/#{id}")['component'])
66
61
  end
67
62
  end
@@ -0,0 +1,17 @@
1
+ module Charger
2
+
3
+ class Configuration
4
+ include Virtus
5
+
6
+ attribute :api_key, String
7
+ attribute :subdomain, String
8
+
9
+ # Retrieves this Configuration's client
10
+ #
11
+ # @return [Charger::Client]
12
+ def client
13
+ Client.new(api_key, subdomain)
14
+ end
15
+ end
16
+
17
+ end
@@ -1,10 +1,7 @@
1
1
  module Charger
2
2
 
3
3
  class CreditCard
4
- include Virtus
5
- extend ActiveModel::Naming
6
- include ActiveModel::Conversion
7
- include ActiveModel::Validations
4
+ include Resource
8
5
 
9
6
  VAULTS = %w{cancellation_message trust_commerce payment_express beanstream braintree1}
10
7
 
@@ -1,10 +1,7 @@
1
1
  module Charger
2
2
 
3
3
  class Customer
4
- include Virtus
5
- extend ActiveModel::Naming
6
- include ActiveModel::Conversion
7
- include ActiveModel::Validations
4
+ include Resource
8
5
 
9
6
  attribute :id, Integer
10
7
  attribute :first_name, String
@@ -0,0 +1,77 @@
1
+ module Charger
2
+
3
+ class Event
4
+ include Resource
5
+
6
+ attribute :id, Integer
7
+ attribute :key, String
8
+ attribute :message, String
9
+ attribute :subscription_id, Integer
10
+ attribute :event_specific_data, Hash
11
+ attribute :created_at, DateTime
12
+
13
+ def subscription force=false
14
+ @subscription = nil if force
15
+ @subscription ||= Subscription.find(subscription_id)
16
+ end
17
+
18
+ def subscription= val
19
+ @subscription = val
20
+ end
21
+
22
+ # Grabs ALL of the events. Note this is a LONG running task especially if
23
+ # your account has a lot of events. You can shorten it up by passing in some
24
+ # optional parameters.
25
+ #
26
+ # Parameters Available:
27
+ # * per_page
28
+ # * since_id
29
+ # * max_id
30
+ # * direction - default is `desc`
31
+ #
32
+ # @param [Hash] params
33
+ # @return [Array<Event>]
34
+ def self.all params={}
35
+ params = { per_page: 200, direction: 'desc' }.merge(params)
36
+ events = []
37
+ num = 1
38
+ loop do
39
+ params[:page] = num
40
+ response = page(params)
41
+ break if response.empty?
42
+ num += 1
43
+ events += response
44
+ puts num
45
+ end
46
+ events
47
+ end
48
+
49
+ # Gets a single page of events
50
+ # Parameters available:
51
+ # * page
52
+ # * per_page
53
+ # * since_id
54
+ # * max_id
55
+ # * direction - `asc` and `desc` are valid
56
+ # @param [Hash] params
57
+ def self.page params={}
58
+ params = { page: 1, per_page: 25, direction: 'desc' }.merge(params)
59
+ events = []
60
+ client.get('events', params: params).each do |data|
61
+ events << new(data['event'])
62
+ end
63
+ events
64
+ end
65
+
66
+ def self.find_by_subscription_id sub, params={}
67
+ params = { per_page: 200, direction: 'desc' }.merge(params)
68
+ events = []
69
+ client.get("subscriptions/#{sub}/events", params).each do |data|
70
+ events << new(data['event'])
71
+ end
72
+ events
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -1,10 +1,7 @@
1
1
  module Charger
2
2
 
3
3
  class LineItem
4
- include Virtus
5
- extend ActiveModel::Naming
6
- include ActiveModel::Conversion
7
- include ActiveModel::Validations
4
+ include Resource
8
5
 
9
6
  attribute :subscription_id, Integer
10
7
  attribute :component_id, Integer
@@ -12,10 +9,9 @@ module Charger
12
9
  attribute :unit_name, String
13
10
  attribute :kind, String
14
11
 
15
- # @param [Integer] the id of the subscription
12
+ # @param [Integer] id the id of the subscription
16
13
  # @return [Array<LineItem>]
17
14
  def self.find_by_subscription_id id
18
- client = Client.new
19
15
  items = []
20
16
 
21
17
  client.get("subscriptions/#{id}/components").each do |data|
@@ -1,10 +1,7 @@
1
1
  module Charger
2
2
 
3
3
  class Product
4
- include Virtus
5
- extend ActiveModel::Naming
6
- include ActiveModel::Conversion
7
- include ActiveModel::Validations
4
+ include Resource
8
5
 
9
6
  attribute :id, Integer
10
7
  attribute :price_in_cents, Integer
@@ -30,7 +27,6 @@ module Charger
30
27
  attribute :archived_at, DateTime
31
28
 
32
29
  def self.find_by_product_family_id id
33
- client = Client.new
34
30
  products = []
35
31
  client.get("product_families/#{id}/products") do |data|
36
32
  products << new(data['product'])
@@ -41,6 +37,10 @@ module Charger
41
37
  def persisted?
42
38
  !!id
43
39
  end
40
+
41
+ def self.client
42
+ Charger.client
43
+ end
44
44
  end
45
45
 
46
46
  end
@@ -1,10 +1,7 @@
1
1
  module Charger
2
2
 
3
3
  class ProductFamily
4
- include Virtus
5
- extend ActiveModel::Naming
6
- include ActiveModel::Conversion
7
- include ActiveModel::Validations
4
+ include Resource
8
5
 
9
6
  attribute :id, Integer
10
7
  attribute :name, String
@@ -12,6 +9,9 @@ module Charger
12
9
  attribute :accounting_code, String
13
10
  attribute :description, String
14
11
 
12
+ validates :name, presence: true
13
+ validates :handle, presence: true
14
+
15
15
  # @param [Boolean] force will cause this to un-cache the results
16
16
  def products force=false
17
17
  @products = nil if force
@@ -0,0 +1,17 @@
1
+ module Charger
2
+
3
+ module Request
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ def self.client
8
+ Charger.client
9
+ end
10
+
11
+ def client
12
+ Charger.client
13
+ end
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,13 @@
1
+ module Charger
2
+ module Resource
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ include Virtus
7
+ extend ActiveModel::Naming
8
+ include ActiveModel::Conversion
9
+ include ActiveModel::Validations
10
+ include Request
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ module Charger
2
+ class Statement
3
+ include Resource
4
+
5
+ attribute :id, Integer
6
+ attribute :subscription_id, Integer
7
+ attribute :opened_at, DateTime
8
+ attribute :closed_at, DateTime
9
+ attribute :settled_at, DateTime
10
+ attribute :text_view, String
11
+ attribute :basic_html_view, String
12
+ attribute :html_view, String
13
+ attribute :future_payments, String
14
+ attribute :starting_balance_in_cents, Integer
15
+ attribute :ending_balance_in_cents, Integer
16
+ attribute :customer_first_name, String
17
+ attribute :customer_last_name, String
18
+ attribute :customer_organization, String
19
+ attribute :customer_shipping_address, String
20
+ attribute :customer_shipping_address_2, String
21
+ attribute :customer_shipping_city, String
22
+ attribute :customer_shipping_state, String
23
+ attribute :customer_shipping_country, String
24
+ attribute :customer_shipping_zip, String
25
+ attribute :customer_billing_address, String
26
+ attribute :customer_billing_address_2, String
27
+ attribute :customer_billing_city, String
28
+ attribute :customer_billing_state, String
29
+ attribute :customer_billing_country, String
30
+ attribute :customer_billing_zip, String
31
+ attribute :transactions, Array[Transaction]
32
+ attribute :events, Array[Event]
33
+ attribute :created_at, DateTime
34
+ attribute :updated_at, DateTime
35
+
36
+ def subscription
37
+ @subscription ||= Subscription.find(subscription_id)
38
+ end
39
+
40
+ def subscription= sub
41
+ @subscription = sub
42
+ end
43
+
44
+ def self.find id
45
+ new(client.get("statements/#{id}")['statement'])
46
+ end
47
+
48
+ def self.find_by_subscription_id id
49
+ statements = []
50
+ client.get("subscriptions/#{id}/statements").each do |data|
51
+ statements << new(data['statement'])
52
+ end
53
+ statements
54
+ end
55
+ end
56
+ end
@@ -1,10 +1,7 @@
1
1
  module Charger
2
2
 
3
3
  class Subscription
4
- include Virtus
5
- extend ActiveModel::Naming
6
- include ActiveModel::Conversion
7
- include ActiveModel::Validations
4
+ include Resource
8
5
 
9
6
  STATES = %w{trialing trial_ended assessing active soft_failure past_due suspended canceled unpaid expired}
10
7
 
@@ -15,7 +12,6 @@ module Charger
15
12
  attribute :current_period_ends_at, DateTime
16
13
  attribute :next_assessment_at, DateTime
17
14
  attribute :trial_started_at, DateTime
18
- attribute :trial_started_at, DateTime
19
15
  attribute :trial_ended_at, DateTime
20
16
  attribute :activated_at, DateTime
21
17
  attribute :expires_at, DateTime
@@ -43,7 +39,6 @@ module Charger
43
39
  # not found.
44
40
  # @return [Subscription]
45
41
  def self.find id
46
- client = Client.new
47
42
  new(client.get("subscriptions/#{id}")['subscription'])
48
43
  end
49
44
 
@@ -52,7 +47,6 @@ module Charger
52
47
  #
53
48
  # @return [Array<Subscription>]
54
49
  def self.all
55
- client = Client.new
56
50
  subscriptions = []
57
51
 
58
52
  num = 1
@@ -72,7 +66,6 @@ module Charger
72
66
  # @param [Integer] limit max is 200 and default is 20
73
67
  # @return [Array<Subscription>]
74
68
  def self.page num=1, limit=20
75
- client = Client.new
76
69
  subscriptions = []
77
70
  client.get("subscriptions", params: {page: num, per_page: limit}).each do |data|
78
71
  subscriptions << new(data['subscription'])
@@ -137,6 +130,26 @@ module Charger
137
130
  !!id
138
131
  end
139
132
 
133
+ def activated_on? date
134
+ return false unless activated?
135
+ activated_between?(date.beginning_of_day, date.end_of_day)
136
+ end
137
+
138
+ def activated_between? a, b
139
+ return false unless activated?
140
+ activated_at > a && activated_at < b
141
+ end
142
+
143
+ def canceled_on? date
144
+ return false unless canceled?
145
+ canceled_between?(date.beginning_of_day, date.end_of_day)
146
+ end
147
+
148
+ def canceled_between? a, b
149
+ return false unless canceled?
150
+ canceled_at > a && canceled_at < b
151
+ end
152
+
140
153
  def total
141
154
  sum = product.price_in_cents.to_f / 100.0
142
155
  line_items.each do |item|
@@ -153,6 +166,15 @@ module Charger
153
166
  end
154
167
  end
155
168
 
169
+ def events force=false
170
+ @events = nil if force
171
+ @events ||= Event.find_by_subscription_id(id)
172
+ @events.each do |event|
173
+ event.subscription = self
174
+ end
175
+ @events
176
+ end
177
+
156
178
  # @param [Boolean] force will cause this to un-cache the results
157
179
  def line_items force=false
158
180
  @line_items = nil if force
@@ -162,6 +184,24 @@ module Charger
162
184
  end
163
185
  @line_items
164
186
  end
187
+
188
+ def transactions force=false
189
+ @transactions = nil if force
190
+ @transactions ||= Transaction.find_by_subscription_id(id)
191
+ @transactions.each do |transaction|
192
+ transaction.subscription = self
193
+ end
194
+ @transactions
195
+ end
196
+
197
+ def statements force=false
198
+ @statements = nil if force
199
+ @statements ||= Statement.find_by_subscription_id(id)
200
+ @statements.each do |statement|
201
+ statement.subscription = self
202
+ end
203
+ @statements
204
+ end
165
205
  end
166
206
 
167
207
  end
@@ -0,0 +1,93 @@
1
+ module Charger
2
+ class Transaction
3
+ include Resource
4
+
5
+ TYPES = %W{charge refund payment credit payment_authorization info adjustment}
6
+
7
+ attribute :transaction_type, String
8
+ attribute :id, Integer
9
+ attribute :amount_in_cents, Integer
10
+ attribute :created_at, DateTime
11
+ attribute :ending_balance_in_cents, Integer
12
+ attribute :memo, String
13
+ attribute :subscription_id, Integer
14
+ attribute :product_id, Integer
15
+ attribute :success, Boolean
16
+ attribute :payment_id, Integer
17
+ attribute :kind, String
18
+ attribute :gateway_transaction_id, String
19
+
20
+ def subscription
21
+ @subscription ||= Subscription.find(subscription_id)
22
+ end
23
+
24
+ def subscription= sub
25
+ @subscription = sub
26
+ end
27
+
28
+ def reload
29
+ return false if id.nil?
30
+ self.attributes = client.get("transactions/#{id}")['transaction']
31
+ true
32
+ end
33
+
34
+ def self.find id
35
+ new(client.get("transactions/#{id}")['transaction'])
36
+ end
37
+
38
+ def self.page params={}
39
+ params = {:page => 1, :per_page => 20}.merge(params)
40
+ transactions = []
41
+ client.get("transactions", params: params).each do |data|
42
+ transactions << new(data['transaction'])
43
+ end
44
+ transactions
45
+ end
46
+
47
+ def self.all
48
+ transactions = []
49
+ params = {page: 1, per_page: 200}
50
+
51
+ loop do
52
+ response = page(params)
53
+ break if response.empty?
54
+ params[:page] += 1
55
+ transactions += response
56
+ end
57
+
58
+ transactions
59
+ end
60
+
61
+ # Finds a set of transactions for a subscription
62
+ #
63
+ # Optional Parameters:
64
+ # * kinds[] : An array of transaction types, see above
65
+ # * since_id : Returns transactions with an id greater than or equal to
66
+ # the one specified
67
+ # * max_id : Returns transactions with an id less than or equal to the one
68
+ # specified
69
+ # * since_date : (format YYYY-MM-DD) Returns transactions with a
70
+ # created_at date greater than or equal to the one specified
71
+ # * until_date : (format YYYY-MM-DD) Returns transactions with a
72
+ # created_at date less than or equal to the one specified
73
+ # * page : default is 1
74
+ # * per_page : default is 20
75
+ #
76
+ # @param [Integer] id
77
+ # @param [Hash] params See optional params
78
+ # @return [Array<Transaction>]
79
+ def self.find_by_subscription_id id, params={}
80
+ params = {:page => 1, :per_page => 20}.merge(params)
81
+ transactions = []
82
+ loop do
83
+ response = client.get("subscriptions/#{id}/transactions", params: params)
84
+ break if response.empty?
85
+ response.each do |data|
86
+ transactions << new(data['transaction'])
87
+ end
88
+ params[:page] += 1
89
+ end
90
+ transactions
91
+ end
92
+ end
93
+ end
@@ -1,3 +1,3 @@
1
1
  module Charger
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: charger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-18 00:00:00.000000000 Z
12
+ date: 2013-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: virtus
@@ -192,15 +192,21 @@ files:
192
192
  - lib/charger/component/on_off.rb
193
193
  - lib/charger/component/price.rb
194
194
  - lib/charger/component/quantity_based.rb
195
+ - lib/charger/configuration.rb
195
196
  - lib/charger/credit_card.rb
196
197
  - lib/charger/customer.rb
198
+ - lib/charger/event.rb
197
199
  - lib/charger/line_item.rb
198
200
  - lib/charger/line_item/metered.rb
199
201
  - lib/charger/line_item/on_off.rb
200
202
  - lib/charger/line_item/quantity_based.rb
201
203
  - lib/charger/product.rb
202
204
  - lib/charger/product_family.rb
205
+ - lib/charger/request.rb
206
+ - lib/charger/resource.rb
207
+ - lib/charger/statement.rb
203
208
  - lib/charger/subscription.rb
209
+ - lib/charger/transaction.rb
204
210
  - lib/charger/version.rb
205
211
  homepage: https://github.com/warmwaffles/charger
206
212
  licenses: []
@@ -214,18 +220,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
214
220
  - - ! '>='
215
221
  - !ruby/object:Gem::Version
216
222
  version: '0'
217
- segments:
218
- - 0
219
- hash: 484567601979014021
220
223
  required_rubygems_version: !ruby/object:Gem::Requirement
221
224
  none: false
222
225
  requirements:
223
226
  - - ! '>='
224
227
  - !ruby/object:Gem::Version
225
228
  version: '0'
226
- segments:
227
- - 0
228
- hash: 484567601979014021
229
229
  requirements: []
230
230
  rubyforge_project:
231
231
  rubygems_version: 1.8.24
@@ -233,3 +233,4 @@ signing_key:
233
233
  specification_version: 3
234
234
  summary: Utilizes Chargify's REST API
235
235
  test_files: []
236
+ has_rdoc: