Class: Weeblycloud::WeeblyCloudResponse
- Inherits:
-
Object
- Object
- Weeblycloud::WeeblyCloudResponse
- Includes:
- Enumerable
- Defined in:
- lib/weeblycloud/cloudclient/weeblycloudresponse.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#max_page ⇒ Object
readonly
Returns the value of attribute max_page.
-
#page_size ⇒ Object
readonly
Returns the value of attribute page_size.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterate over all pages.
-
#initialize(response, endpoint, headers, content, params) ⇒ WeeblyCloudResponse
constructor
A new instance of WeeblyCloudResponse.
-
#next_page ⇒ Object
Get the next page, return True on success, False on failure.
-
#paginated? ⇒ Boolean
Returns whether the results are paginated.
-
#to_hash ⇒ Object
Returns the current page as a hash.
-
#to_s ⇒ Object
Returns the current page as a JSON string.
Constructor Details
#initialize(response, endpoint, headers, content, params) ⇒ WeeblyCloudResponse
Returns a new instance of WeeblyCloudResponse.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 11 def initialize(response, endpoint, headers, content, params) @page_size = nil @max_page = nil @current_page = nil @records = nil @is_paginated = false # private @response = response @endpoint = endpoint @content = content @params = params @headers = headers @first_iter = true refresh() end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
9 10 11 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 9 def current_page @current_page end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
9 10 11 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 9 def json @json end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
9 10 11 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 9 def list @list end |
#max_page ⇒ Object (readonly)
Returns the value of attribute max_page.
9 10 11 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 9 def max_page @max_page end |
#page_size ⇒ Object (readonly)
Returns the value of attribute page_size.
9 10 11 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 9 def page_size @page_size end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
9 10 11 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 9 def records @records end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over all pages. Accepts a block.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 53 def each(&block) if @is_paginated # loop over all pages while @current_page < @max_page @list.each{|item| yield(item) } if @first_iter @first_iter = false else next_page() end end else # If it isn't paginated just do it once @list.each{ |item| yield(item) } end end |
#next_page ⇒ Object
Get the next page, return True on success, False on failure. If the WeeblyCloudResponse is not paginated, raise an exception.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 36 def next_page() if not @is_paginated raise PaginationError, "Not paginated" end if @current_page >= @max_page return False end next_page = @params["page"].nil? ? 2 : @params["page"] + 1 @params.merge!({"page"=>next_page}) @response = HTTP.headers(@headers).get(@endpoint, :body => "{}", :params => @params) refresh() end |
#paginated? ⇒ Boolean
Returns whether the results are paginated
30 31 32 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 30 def paginated? @is_paginated end |
#to_hash ⇒ Object
Returns the current page as a hash
76 77 78 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 76 def to_hash() @json end |
#to_s ⇒ Object
Returns the current page as a JSON string
71 72 73 |
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 71 def to_s() @json.to_json end |