Skip to content

Commit

Permalink
Make collections enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
jtnegrotto committed Nov 6, 2017
1 parent 5a646f6 commit 095a011
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ To send a fax:

## TODO

1. Flesh out Collections.
2. Add documentation for existing code.
3. Rewrite README for v2 updates.
4. Refactor.
1. Add documentation for existing code.
2. Rewrite README for v2 updates.
3. Refactor.
2 changes: 1 addition & 1 deletion lib/phaxio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
require file
end

%w[fax_recipient fax account area_code callback phax_code phone_number].each do |filename|
%w[fax_recipient country fax account area_code callback phax_code phone_number].each do |filename|
require File.expand_path(File.join('..', 'phaxio', 'resources', filename), __FILE__)
end

Expand Down
17 changes: 14 additions & 3 deletions lib/phaxio/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def response_record raw_data
# @param raw_data [Array] The raw response data from Phaxio.
# @return [Phaxio::Resource::Collection] A collection of Phaxio::Resource instances.
def response_collection raw_data
Collection.new raw_data
Collection.new raw_data, self
end

# @api private
Expand Down Expand Up @@ -125,15 +125,26 @@ def inherited subclass
end

class Collection
include Enumerable

# The raw response data
attr_accessor :raw_data
attr_accessor :raw_data, :collection

# Returns a new collection of resource instances for this data. Generally this is not called
# directly.
#
# @see Phaxio::Resource.response_collection
def initialize raw_data
def initialize raw_data, resource
self.raw_data = raw_data
self.collection = raw_data.map { |record_data| resource.response_record record_data }
end

def [] idx
collection[idx]
end

def each(&block)
collection.each(&block)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/phaxio/resources/country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Phaxio
module Resources
class Country < Resource
has_normal_attributes %w[
name alpha2 country_code price_per_page send_support receive_support
]
end
end
end
2 changes: 1 addition & 1 deletion lib/phaxio/resources/fax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_receive params = {}, options = {}

def supported_countries params = {}, options = {}
response = Client.request :get, supported_countries_endpoint, params, options
response_collection response
Country.response_collection response
end

private
Expand Down

0 comments on commit 095a011

Please sign in to comment.