Class: AMEE::Collection
- Inherits:
-
Array
- Object
- Array
- AMEE::Collection
- Includes:
- ParseHelper
- Defined in:
- lib/amee/collection.rb,
lib/amee/v3/collection.rb
Direct Known Subclasses
Admin::ItemDefinitionList, Admin::ItemValueDefinitionList, Admin::ReturnValueDefinitionList
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
- #each_page_without_v3 ⇒ Object
- #fetch ⇒ Object
- #fetch_without_v3 ⇒ Object
-
#initialize(connection, options = {}, &block) ⇒ Collection
constructor
A new instance of Collection.
- #parse_page ⇒ Object
- #v3 ⇒ Object
Methods included from ParseHelper
#load_xml_doc, #node_value, #xmlpathpreamble
Constructor Details
#initialize(connection, options = {}, &block) ⇒ Collection
Returns a new instance of Collection.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/amee/collection.rb', line 8 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 JSONParseError, XMLParseError @connection.expire(collectionpath) raise AMEE::BadData.new("Couldn't load #{self.class.name}.\n#{@response}") rescue AMEE::BadData @connection.expire(collectionpath) raise end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
7 8 9 |
# File 'lib/amee/collection.rb', line 7 def connection @connection end |
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
7 8 9 |
# File 'lib/amee/collection.rb', line 7 def doc @doc end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
7 8 9 |
# File 'lib/amee/collection.rb', line 7 def json @json end |
#pager ⇒ Object (readonly)
Returns the value of attribute pager.
7 8 9 |
# File 'lib/amee/collection.rb', line 7 def pager @pager end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/amee/collection.rb', line 7 def response @response end |
Instance Method Details
#each_page ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/amee/collection.rb', line 69 def each_page begin fetch yield if json @pager = AMEE::Pager.from_json(doc['pager']) else @pager = AMEE::Pager.from_xml(doc.xpath('/Resources//Pager').first) 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 |
#each_page_without_v3 ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/amee/v3/collection.rb', line 38 def each_page begin fetch yield if json @pager = AMEE::Pager.from_json(doc['pager']) else @pager = AMEE::Pager.from_xml(doc.xpath('/Resources//Pager').first) 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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/amee/collection.rb', line 47 def fetch @options.merge! @pager. if @pager retries = [1] * connection.retries begin @response= @connection.get(collectionpath, @options).body if @response.is_json? @json = true @doc = JSON.parse(@response) else @doc = load_xml_doc(@response) end rescue JSON::ParserError, Nokogiri::XML::SyntaxError @connection.expire(collectionpath) if delay = retries.shift sleep delay retry else raise end end end |
#fetch_without_v3 ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/amee/v3/collection.rb', line 11 def fetch @options.merge! @pager. if @pager retries = [1] * connection.retries begin @response= @connection.get(collectionpath, @options).body if @response.is_json? @json = true @doc = JSON.parse(@response) else @doc = load_xml_doc(@response) end rescue JSON::ParserError, Nokogiri::XML::SyntaxError @connection.expire(collectionpath) if delay = retries.shift sleep delay retry else raise end end end |
#parse_page ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/amee/collection.rb', line 26 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 doc.xpath(xmlcollectorpath.split('/')[1...-1].join('/')).first or raise AMEE::BadData.new("Couldn't load #{self.class.name}. \n#{@response}") doc.xpath(xmlcollectorpath).each 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 |
#v3 ⇒ Object
7 8 9 |
# File 'lib/amee/v3/collection.rb', line 7 def v3 collectionpath=~/^\/3/ end |