Module: ChartMogul::ImportApi
Instance Method Summary collapse
-
#create_data_source(args) ⇒ Object
Public - create a DataSource.
-
#delete_data_source!(data_source_uuid) ⇒ Object
(also: #purge_data_source!)
Public - delete DataSource and purge all related data super dangerous.
-
#import_customer(args) ⇒ Object
Public - import a Customer.
-
#import_invoice(customer_id, invoice) ⇒ Object
Public - import a single Invoice.
-
#import_invoices(customer_id, invoices) ⇒ Object
Public - import Invoices.
-
#import_plan(args) ⇒ Object
Public - import a Plan.
-
#list_customers(options = {}) ⇒ Object
Public - list all Customers.
-
#list_customers_each(options = {}, &block) ⇒ Object
Public - iterate through all customers.
-
#list_data_sources ⇒ Object
Public - list DataSources.
-
#list_invoices(customer_id) ⇒ Object
Public - list of Customer invoices customer_id - ChartMogul id for the customer.
-
#list_invoices_each(customer_id, &block) ⇒ Object
Public - iterate through all invoices.
-
#list_plans(options = {}) ⇒ Object
Public - list all Plans.
-
#list_plans_each(options = {}, &block) ⇒ Object
Public - iterate through all plans.
Methods included from Assertive
Instance Method Details
#create_data_source(args) ⇒ Object
Public - create a DataSource
args - Hash of params only :name is supported
{
name: "Name of data source"
}
Returns a ChartMogul::Import::DataSource
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/chart_mogul/import_api.rb', line 27 def create_data_source(args) refute_blank! args[:name], :name response = connection.post do |request| request.url "/v1/import/data_sources" request.headers['Content-Type'] = "application/json" request.body = args.to_json end Import::DataSource.new(preprocess_response(response)) end |
#delete_data_source!(data_source_uuid) ⇒ Object Also known as: purge_data_source!
Public - delete DataSource and purge all related data
super dangerous
Returns nothing but an empty hole where your data was!
43 44 45 46 47 48 |
# File 'lib/chart_mogul/import_api.rb', line 43 def delete_data_source!(data_source_uuid) response = connection.delete do |request| request.url "/v1/import/data_sources/#{data_source_uuid}" end response.status == 202 end |
#import_customer(args) ⇒ Object
Public - import a Customer
args - Hash of params see dev.chartmogul.com/docs/customers
Mandatory: :data_source_uuid, :external_id, :name
Returns a ChartMogul::Import::Customer
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/chart_mogul/import_api.rb', line 93 def import_customer(args) [:data_source_uuid, :external_id, :name].each do |attribute| refute_blank! args[attribute], attribute end # ChartMogul API will 500 if nill keys are sent args.keys.each do |key| refute! args[key].nil?, "nil keys not supported [#{key}]" end response = connection.post do |request| request.url "/v1/import/customers" request.headers['Content-Type'] = "application/json" request.body = args.to_json end Import::Customer.new(preprocess_response(response)) end |
#import_invoice(customer_id, invoice) ⇒ Object
Public - import a single Invoice. Convenience method that
maps the output (and validation errors) to a single operation
customer_id - ChartMogul id for the customer invoice - Hash of params see dev.chartmogul.com/docs/invoices
See invoice_invoices for mandatory attributes
Returns an Array of ChartMogul::Import::Invoice
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/chart_mogul/import_api.rb', line 174 def import_invoice(customer_id, invoice) invoices = import_invoices(customer_id, [ invoice ]) invoices.first rescue ChartMogul::Client::ValidationError => e # restructure ValidationError to map the single invoice error that was returned if e.body[:invoices] raise ChartMogul::Client::ValidationError.new(e.body[:invoices].first) else raise end end |
#import_invoices(customer_id, invoices) ⇒ Object
Public - import Invoices
customer_id - ChartMogul id for the customer invoices - Array of Hash of params see dev.chartmogul.com/docs/invoices
Mandatory: :external_id, :date, :currency, :line_items
Returns an Array of ChartMogul::Import::Invoice
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/chart_mogul/import_api.rb', line 194 def import_invoices(customer_id, invoices) refute_blank! customer_id, "customer_id" refute! invoices.nil? && invoices.empty?, "invoices required" args = { invoices: invoices } args[:invoices].each do |invoice| [:external_id, :date, :currency].each do |attribute| refute_blank! invoice[attribute], attribute end invoice[:date] = format_datetime(invoice[:date]) assert! invoice[:line_items] && invoice[:line_items].any?, "line_items is required" invoice[:line_items].each do |line_item| line_item[:service_period_start] = format_datetime(line_item[:service_period_start]) if line_item[:service_period_start] line_item[:service_period_end] = format_datetime(line_item[:service_period_end]) if line_item[:service_period_end] assert! line_item[:quantity].nil? || line_item[:quantity] > 0, "line_item quantity must be greater than zero if specified" end end response = connection.post do |request| request.url "/v1/import/customers/#{customer_id}/invoices" request.headers['Content-Type'] = "application/json" request.body = args.to_json end preprocess_response(response)[:invoices].map { |i| Import::Invoice.new(i) } end |
#import_plan(args) ⇒ Object
Public - import a Plan
args - Hash of params see dev.chartmogul.com/docs/plans
Mandatory: :data_source_uuid, :name, :interval_count, :interval_unit
Returns a ChartMogul::Import::Plan
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/chart_mogul/import_api.rb', line 150 def import_plan(args) [:data_source_uuid, :name, :interval_unit, :interval_count].each do |attribute| refute_blank! args[attribute], attribute end assert! (args[:interval_count].is_a?(Integer) && args[:interval_count] > 0), "interval_count must be an integer greater than zero" assert! [:day, :month, :year].include?(args[:interval_unit].to_sym), "interval_unit must be one of :day, :month, :year" response = connection.post do |request| request.url "/v1/import/plans" request.headers['Content-Type'] = "application/json" request.body = args.to_json end Import::Plan.new(preprocess_response(response)) end |
#list_customers(options = {}) ⇒ Object
Public - list all Customers.
this will page through all customers see #list_customers_each
for iterator method to prevent loading the whole array in
memory
options - see #list_customers_each
Returns an Array of ChartMogul::Import::Customer
63 64 65 66 67 |
# File 'lib/chart_mogul/import_api.rb', line 63 def list_customers(={}) customers = [] list_customers_each() { |c| customers << c } customers end |
#list_customers_each(options = {}, &block) ⇒ Object
Public - iterate through all customers
options - Hash of filter options
:data_source_uuid
Returns an Enumerable that will yield a ChartMogul::Import::Customer for each record
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/chart_mogul/import_api.rb', line 76 def list_customers_each(={}, &block) params = {} params[:data_source_uuid] = [:data_source_uuid] if [:data_source_uuid] paged_get("/v1/import/customers", params, :customers) do |customers| customers.each do |customer| yield Import::Customer.new(customer) end end end |
#list_data_sources ⇒ Object
Public - list DataSources
Returns an Array of ChartMogul::Import::DataSource
13 14 15 16 17 |
# File 'lib/chart_mogul/import_api.rb', line 13 def list_data_sources response = connection.get("/v1/import/data_sources") preprocess_response(response)[:data_sources] .map { |ds| Import::DataSource.new(ds) } end |
#list_invoices(customer_id) ⇒ Object
Public - list of Customer invoices customer_id - ChartMogul id for the customer
Returns an Array of ChartMogul::Import::Invoice
230 231 232 233 234 |
# File 'lib/chart_mogul/import_api.rb', line 230 def list_invoices(customer_id) invoices = [] list_invoices_each(customer_id) { |i| invoices << i } invoices end |
#list_invoices_each(customer_id, &block) ⇒ Object
Public - iterate through all invoices
customer_id - ChartMogul id for the customer
Returns an Enumerable that will yield a ChartMogul::Import::Invoice for each record
242 243 244 245 246 247 248 249 250 |
# File 'lib/chart_mogul/import_api.rb', line 242 def list_invoices_each(customer_id, &block) refute_blank! customer_id, "customer_id" paged_get("/v1/import/customers/#{customer_id}/invoices", {}, :invoices) do |invoices| invoices.each do |invoice| yield Import::Invoice.new(invoice) end end end |
#list_plans(options = {}) ⇒ Object
Public - list all Plans.
this will page through all plans see #list_plans_each
for iterator method to prevent loading the whole array in
memory
options - see #list_plans_each
Returns an Array of ChartMogul::Import::Plan
120 121 122 123 124 |
# File 'lib/chart_mogul/import_api.rb', line 120 def list_plans(={}) plans = [] list_plans_each() { |p| plans << p } plans end |
#list_plans_each(options = {}, &block) ⇒ Object
Public - iterate through all plans
options - Hash of filter options
:data_source_uuid
Returns an Enumerable that will yield a ChartMogul::Import::Plan for each record
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/chart_mogul/import_api.rb', line 133 def list_plans_each(={}, &block) params = {} params[:data_source_uuid] = [:data_source_uuid] if [:data_source_uuid] paged_get("/v1/import/plans", params, :plans) do |plans| plans.each do |plan| yield Import::Plan.new(plan) end end end |