Class: AMEE::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParseHelper

#xmlpathpreamble

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, 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 => 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

#connectionObject (readonly)

Returns the value of attribute connection.



4
5
6
# File 'lib/amee/collection.rb', line 4

def connection
  @connection
end

#docObject (readonly)

Returns the value of attribute doc.



4
5
6
# File 'lib/amee/collection.rb', line 4

def doc
  @doc
end

#jsonObject (readonly)

Returns the value of attribute json.



4
5
6
# File 'lib/amee/collection.rb', line 4

def json
  @json
end

#pagerObject (readonly)

Returns the value of attribute pager.



4
5
6
# File 'lib/amee/collection.rb', line 4

def pager
  @pager
end

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



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

#fetchObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/amee/collection.rb', line 40

def fetch
  @options.merge! @pager.options 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_pageObject



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