Skip to content

Commit

Permalink
Pageable (#38)
Browse files Browse the repository at this point in the history
* Page all the things!

* Fix Customers.search test

* all alias in Subscriptions, tests, params in search, fixes
  • Loading branch information
pkopac authored Mar 21, 2017
1 parent ca4cfa6 commit 8f391ed
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 36 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions lib/chartmogul/api/actions/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ def all(options = {})
end
end
json = ChartMogul::Utils::JSONParser.parse(resp.body)

if resource_root_key && json.key?(resource_root_key)
json[resource_root_key].map { |attributes| new_from_json(attributes) }
else
new_from_json(json)
end
new_from_json(json)
end
end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/chartmogul/api/actions/custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ def custom_without_assign!(http_method, http_path, body_data = {})

def custom!(http_method, http_path, body_data = {})
json = custom_without_assign!(http_method, http_path, body_data)

if resource_root_key && json.key?(resource_root_key)
json[resource_root_key].map { |attributes| new_from_json(attributes) }
else
new_from_json(json)
end
new_from_json(json)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chartmogul/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.set_resource_name(name)
def self.set_resource_root_key(root_key)
@resource_root_key = root_key
end

def self.connection
@connection ||= Faraday.new(url: ChartMogul::API_BASE) do |faraday|
faraday.use Faraday::Request::BasicAuthentication, ChartMogul.account_token, ChartMogul.secret_key
Expand Down
24 changes: 13 additions & 11 deletions lib/chartmogul/concerns/entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ module Concerns
module Entries
def self.included(base)
base.extend ClassMethods
base.send :prepend, InstanceMethods

base.instance_eval do
readonly_attr :entries, default: []
if @resource_root_key.nil?
@resource_root_key = :entries
end
readonly_attr @resource_root_key, default: []

include API::Actions::All

include Enumerable
def_delegators :entries, :each, :[], :<<, :size, :length
def_delegators @resource_root_key, :each, :[], :<<, :size, :length, :empty?, :first

resource_root_key = @resource_root_key.to_s
base.send :define_method, "set_" + resource_root_key do |entries|
objects = entries.map do |entity|
self.class.get_entry_class.new_from_json(entity)
end
self.instance_variable_set "@#{resource_root_key}", objects
end
end
end

Expand All @@ -24,14 +34,6 @@ def get_entry_class
instance_variable_get("@entry_class")
end
end

module InstanceMethods
def set_entries(entries_attributes)
@entries = entries_attributes.map do |entity|
self.class.get_entry_class.new_from_json(entity)
end
end
end
end
end
end
8 changes: 4 additions & 4 deletions lib/chartmogul/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def self.retrieve(uuid)
custom!(:get, "/v1/customers/#{uuid}")
end

def self.search(email)
Customers.search(email)
def self.search(email, options = {})
Customers.search(email, options)
end

def self.find_by_external_id(external_id)
Expand Down Expand Up @@ -147,9 +147,9 @@ class Customers < APIResource

set_entry_class Customer

def self.search(email)
def self.search(email, options = {})
path = ChartMogul::ResourcePath.new('/v1/customers/search')
custom!(:get, path.apply_with_get_params(email: email))
custom!(:get, path.apply_with_get_params(options.merge(email: email)))
end
end
end
4 changes: 3 additions & 1 deletion lib/chartmogul/customer_invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CustomerInvoices < APIResource

include API::Actions::All
include API::Actions::Create
include Concerns::Pageable2

def serialize_invoices
map(&:serialize_for_write)
Expand All @@ -23,10 +24,11 @@ def self.all(customer_uuid, options = {})
super(options.merge(customer_uuid: customer_uuid))
end

def_delegators :invoices, :each, :[], :<<, :size, :length
def_delegators :invoices, :each, :[], :<<, :size, :length, :empty?, :first

private

# TODO: replace with Entries concern?
def set_invoices(invoices_attributes)
@invoices = invoices_attributes.map.with_index do |invoice_attributes, index|
existing_invoice = invoices[index]
Expand Down
17 changes: 16 additions & 1 deletion lib/chartmogul/data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module ChartMogul
class DataSource < APIResource
set_resource_name 'Data Source'
set_resource_path '/v1/data_sources'
set_resource_root_key :data_sources

readonly_attr :uuid
readonly_attr :status
Expand All @@ -19,5 +18,21 @@ class DataSource < APIResource
def self.retrieve(uuid)
custom!(:get, "/v1/data_sources/#{uuid}")
end

def self.all(options = {})
DataSources.all(options)
end
end


class DataSources < APIResource
set_resource_name 'Data Sources'
set_resource_path '/v1/data_sources'

set_resource_root_key :data_sources

include Concerns::Entries

set_entry_class DataSource
end
end
19 changes: 17 additions & 2 deletions lib/chartmogul/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module ChartMogul
class Plan < APIResource
set_resource_name 'Plan'
set_resource_path '/v1/plans'
set_resource_root_key :plans

readonly_attr :uuid

Expand All @@ -13,7 +12,6 @@ class Plan < APIResource

writeable_attr :data_source_uuid

include API::Actions::All
include API::Actions::Create
include API::Actions::Update
include API::Actions::Destroy
Expand All @@ -22,5 +20,22 @@ class Plan < APIResource
def self.retrieve(uuid)
custom!(:get, "/v1/plans/#{uuid}")
end

def self.all(options = {})
Plans.all(options)
end
end


class Plans < APIResource
set_resource_name 'Plans'
set_resource_path '/v1/plans'

set_resource_root_key :plans

include Concerns::Entries
include Concerns::Pageable2

set_entry_class Plan
end
end
21 changes: 19 additions & 2 deletions lib/chartmogul/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module ChartMogul
class Subscription < APIResource
set_resource_name 'Subscription'
set_resource_path '/v1/import/customers/:customer_uuid/subscriptions'
set_resource_root_key :subscriptions

readonly_attr :uuid
readonly_attr :external_id
Expand All @@ -11,7 +10,6 @@ class Subscription < APIResource
readonly_attr :plan_uuid
readonly_attr :data_source_uuid

include API::Actions::All
include API::Actions::Custom

def set_cancellation_dates(cancellation_dates_array)
Expand All @@ -24,8 +22,27 @@ def cancel(cancelled_at)
custom!(:patch, "/v1/import/subscriptions/#{uuid}", cancelled_at: cancelled_at)
end

def self.all(customer_uuid, options = {})
Subscriptions.all(customer_uuid, options)
end
end

class Subscriptions < APIResource
readonly_attr :customer_uuid

set_resource_name 'Subscriptions'
set_resource_path '/v1/import/customers/:customer_uuid/subscriptions'

set_resource_root_key :subscriptions

include Concerns::Entries
include Concerns::Pageable2

set_entry_class Subscription

def self.all(customer_uuid, options = {})
super(options.merge(customer_uuid: customer_uuid))
end

end
end
2 changes: 1 addition & 1 deletion lib/chartmogul/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ChartMogul
VERSION = "1.0.2"
VERSION = "1.1.0"
end
Loading

0 comments on commit 8f391ed

Please sign in to comment.