Class: AMEE::Collection

Inherits:
Array
  • Object
show all
Includes:
ParseHelper
Defined in:
lib/amee/collection.rb,
lib/amee/v3/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {}, &block)
  # Load data from path
  @options=options
  @max=options.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

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/amee/collection.rb', line 7

def connection
  @connection
end

#docObject (readonly)

Returns the value of attribute doc.



7
8
9
# File 'lib/amee/collection.rb', line 7

def doc
  @doc
end

#jsonObject (readonly)

Returns the value of attribute json.



7
8
9
# File 'lib/amee/collection.rb', line 7

def json
  @json
end

#pagerObject (readonly)

Returns the value of attribute pager.



7
8
9
# File 'lib/amee/collection.rb', line 7

def pager
  @pager
end

#responseObject (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_pageObject



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_v3Object



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

#fetchObject



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.options 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_v3Object



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.options 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_pageObject



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

#v3Object



7
8
9
# File 'lib/amee/v3/collection.rb', line 7

def v3
  collectionpath=~/^\/3/
end