Class: Gupshup::REST::Page
- Inherits:
-
Object
- Object
- Gupshup::REST::Page
- Includes:
- Enumerable
- Defined in:
- lib/gupshup-rb/framework/rest/page.rb
Overview
Page Base Class
Constant Summary collapse
- META_KEYS =
[ 'end', 'first_page_uri', 'next_page_uri', 'last_page_uri', 'page', 'page_size', 'previous_page_uri', 'total', 'num_pages', 'start', 'uri' ].freeze
Instance Method Summary collapse
- #each ⇒ Object
- #get_instance(payload) ⇒ Object
-
#initialize(version, response) ⇒ Page
constructor
A new instance of Page.
- #load_page(payload) ⇒ Object
- #next_page ⇒ Object
- #next_page_url ⇒ Object
- #previous_page ⇒ Object
- #previous_page_url ⇒ Object
- #process_response(response) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(version, response) ⇒ Page
Returns a new instance of Page.
23 24 25 26 27 28 29 30 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 23 def initialize(version, response) payload = process_response(response) @version = version @payload = payload @solution = {} @records = load_page(payload) end |
Instance Method Details
#each ⇒ Object
92 93 94 95 96 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 92 def each @records.each do |record| yield get_instance(record) end end |
#get_instance(payload) ⇒ Object
72 73 74 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 72 def get_instance(payload) raise Gupshup::REST::GupshupError, 'Page.get_instance() must be implemented in the derived class' end |
#load_page(payload) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 40 def load_page(payload) if payload['meta'] && payload['meta']['key'] return payload[payload['meta']['key']] else keys = payload.keys key = keys - META_KEYS return payload[key.first] if key.size == 1 end raise Gupshup::REST::GupshupError, 'Page Records can not be deserialized' end |
#next_page ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 84 def next_page return nil unless next_page_url response = @version.domain.request('GET', next_page_url) self.class.new(@version, response, @solution) end |
#next_page_url ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 62 def next_page_url if @payload['meta'] && @payload['meta']['next_page_url'] return @version.domain.absolute_url(URI.parse(@payload['meta']['next_page_url']).request_uri) elsif @payload['next_page_uri'] return @version.domain.absolute_url(@payload['next_page_uri']) end nil end |
#previous_page ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 76 def previous_page return nil unless previous_page_url response = @version.domain.request('GET', previous_page_url) self.class.new(@version, response, @solution) end |
#previous_page_url ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 52 def previous_page_url if @payload['meta'] && @payload['meta']['previous_page_url'] return @version.domain.absolute_url(URI.parse(@payload['meta']['previous_page_url']).request_uri) elsif @payload['previous_page_uri'] return @version.domain.absolute_url(@payload['previous_page_uri']) end nil end |
#process_response(response) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 32 def process_response(response) if response.status_code != 200 raise Gupshup::REST::RestError.new('Unable to fetch page', response) end response.body end |
#to_s ⇒ Object
98 99 100 |
# File 'lib/gupshup-rb/framework/rest/page.rb', line 98 def to_s '#<Page>' end |