Class: Zipmark::Iterator
- Inherits:
-
Object
- Object
- Zipmark::Iterator
- Defined in:
- lib/zipmark/iterator.rb
Instance Attribute Summary collapse
-
#current_item ⇒ Object
Returns the value of attribute current_item.
-
#json ⇒ Object
Returns the value of attribute json.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #client ⇒ Object
- #collection ⇒ Object
- #fetch_item_from_next_page ⇒ Object
-
#initialize(json, options = {}) ⇒ Iterator
constructor
A new instance of Iterator.
- #items ⇒ Object
- #links ⇒ Object
- #metadata ⇒ Object
- #next_item ⇒ Object
- #next_page ⇒ Object
- #pages ⇒ Object
- #pagination ⇒ Object
- #resource_singular ⇒ Object
- #total_items ⇒ Object
Constructor Details
#initialize(json, options = {}) ⇒ Iterator
Returns a new instance of Iterator.
5 6 7 8 9 |
# File 'lib/zipmark/iterator.rb', line 5 def initialize(json, = {}) @json = JSON.parse(json) = @current_item = items.first end |
Instance Attribute Details
#current_item ⇒ Object
Returns the value of attribute current_item.
3 4 5 |
# File 'lib/zipmark/iterator.rb', line 3 def current_item @current_item end |
#json ⇒ Object
Returns the value of attribute json.
3 4 5 |
# File 'lib/zipmark/iterator.rb', line 3 def json @json end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/zipmark/iterator.rb', line 3 def end |
Instance Method Details
#client ⇒ Object
11 12 13 |
# File 'lib/zipmark/iterator.rb', line 11 def client [:client] end |
#collection ⇒ Object
15 16 17 |
# File 'lib/zipmark/iterator.rb', line 15 def collection json[[:resource_name]] end |
#fetch_item_from_next_page ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/zipmark/iterator.rb', line 48 def fetch_item_from_next_page return if pagination.last_page? self.json = JSON.parse(client.get(next_page["href"]).body) @items = nil @pagination = nil @current_item = items.first return true end |
#items ⇒ Object
27 28 29 |
# File 'lib/zipmark/iterator.rb', line 27 def items @items ||= collection.map {|item| Entity.new(item.merge(:client => [:client], :resource_type => resource_singular)) } end |
#links ⇒ Object
23 24 25 |
# File 'lib/zipmark/iterator.rb', line 23 def links json["links"] end |
#metadata ⇒ Object
19 20 21 |
# File 'lib/zipmark/iterator.rb', line 19 def json["meta"] end |
#next_item ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/zipmark/iterator.rb', line 39 def next_item item = @items.fetch(@items.index(current_item) + 1) rescue nil if item @current_item = item else @current_item = fetch_item_from_next_page ? @items.fetch(0) : nil end end |
#next_page ⇒ Object
57 58 59 |
# File 'lib/zipmark/iterator.rb', line 57 def next_page links.detect {|l| l["rel"] == "next"} end |
#pages ⇒ Object
65 66 67 |
# File 'lib/zipmark/iterator.rb', line 65 def pages pagination.pages if pagination end |
#pagination ⇒ Object
35 36 37 |
# File 'lib/zipmark/iterator.rb', line 35 def pagination @pagination ||= Pagination.new(["pagination"] ) if end |
#resource_singular ⇒ Object
31 32 33 |
# File 'lib/zipmark/iterator.rb', line 31 def resource_singular [:resource_name].sub(/s$/, '') end |
#total_items ⇒ Object
61 62 63 |
# File 'lib/zipmark/iterator.rb', line 61 def total_items pagination.total if pagination end |