Class: AMEE::Collection
- Inherits:
-
Array
- Object
- Array
- AMEE::Collection
- Includes:
- ParseHelper
- Defined in:
- lib/amee/collection.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#pager ⇒ Object
readonly
Returns the value of attribute pager.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #each_page ⇒ Object
- #fetch ⇒ Object
-
#initialize(connection, options = {}, &block) ⇒ Collection
constructor
A new instance of Collection.
- #parse_page ⇒ Object
Methods included from ParseHelper
Constructor Details
#initialize(connection, options = {}, &block) ⇒ Collection
Returns a new instance of Collection.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/amee/collection.rb', line 5 def initialize(connection, = {}, &block) # Load data from path @options= @max=.delete :resultMax @connection=connection @filter = block # Parse data from response each_page do parse_page end rescue => err #raise AMEE::BadData.new("Couldn't load #{self.class.name}.\n#{response} due to #{err}") raise AMEE::BadData.new("Couldn't load #{self.class.name}.\n#{response}") end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
4 5 6 |
# File 'lib/amee/collection.rb', line 4 def connection @connection end |
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
4 5 6 |
# File 'lib/amee/collection.rb', line 4 def doc @doc end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
4 5 6 |
# File 'lib/amee/collection.rb', line 4 def json @json end |
#pager ⇒ Object (readonly)
Returns the value of attribute pager.
4 5 6 |
# File 'lib/amee/collection.rb', line 4 def pager @pager end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
4 5 6 |
# File 'lib/amee/collection.rb', line 4 def response @response end |
Instance Method Details
#each_page ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/amee/collection.rb', line 51 def each_page begin fetch yield if json @pager = AMEE::Pager.from_json(doc['pager']) else @pager = AMEE::Pager.from_xml(REXML::XPath.first(doc, '/Resources//Pager')) end break if @max && length>=@max end while @pager && @pager.next! #pager is nil if no pager in response, # pager.next! is false if @pager said current=last. end |
#fetch ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/amee/collection.rb', line 40 def fetch @options.merge! @pager. if @pager @response= @connection.get(collectionpath, @options).body if @response.is_json? @json = true @doc = JSON.parse(@response) else @doc = REXML::Document.new(@response) end end |
#parse_page ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/amee/collection.rb', line 19 def parse_page if json jsoncollector.each do |p| obj = klass.new(parse_json(p)) obj.connection = connection self << obj break if @max&&length>=@max end else REXML::XPath.first(doc,xmlcollectorpath.split('/')[1...-1].join('/')) or raise AMEE::BadData.new("Couldn't load #{self.class.name}.\n#{response}") REXML::XPath.each(doc, xmlcollectorpath) do |p| obj=klass.new(parse_xml(p)) obj.connection = connection x= @filter ? @filter.call(obj) : obj self << x if x break if @max&&length>=@max end end end |