class Weeblycloud::WeeblyCloudResponse
Attributes
current_page[R]
json[R]
list[R]
max_page[R]
page_size[R]
records[R]
Public Class Methods
new(response, endpoint, headers, content, params)
click to toggle source
# 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
Public Instance Methods
each() { |item| ... }
click to toggle source
Iterate over all pages. Accepts a block.
# 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()
click to toggle source
Get the next page, return True on success, False on failure. If the WeeblyCloudResponse
is not paginated, raise an exception.
# 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?()
click to toggle source
Returns whether the results are paginated
# File lib/weeblycloud/cloudclient/weeblycloudresponse.rb, line 30 def paginated? @is_paginated end
to_hash()
click to toggle source
Returns the current page as a hash
# File lib/weeblycloud/cloudclient/weeblycloudresponse.rb, line 76 def to_hash() @json end
to_s()
click to toggle source
Returns the current page as a JSON string
# File lib/weeblycloud/cloudclient/weeblycloudresponse.rb, line 71 def to_s() @json.to_json end
Private Instance Methods
refresh()
click to toggle source
Refreshes the internal parameters based on the current value of @request.
# File lib/weeblycloud/cloudclient/weeblycloudresponse.rb, line 83 def refresh() @status_code = @response.code # Handle errors. Sometimes there may not be a response body (which is why ValueError) # must be caught. begin if !([200,204].include? @status_code) || @response.parse().include?("error") error = @response.parse() raise ResponseError.new(error["error"]["message"], error["error"]["code"]) end rescue NoMethodError, HTTP::Error # Sometimes DELETE returns nothing. When this is the case, it will have a status code 204 raise ResponseError.new(code=@status_code) unless @status_code == 204 end # Get information on paging if response is paginated headers = @response.headers if headers["X-Resultset-Total"] && headers["X-Resultset-Total"].to_i > headers["X-Resultset-Limit"].to_i @is_paginated = true @records = headers["X-Resultset-Total"].to_i @page_size = headers["X-Resultset-Limit"].to_i @current_page = headers["X-Resultset-Page"].to_i @max_page = (@records.to_f / @page_size.to_i).ceil end # Save the content of the request begin @json = @response.parse() # Parse the result so it can be paged over. if @is_paginated if @json.is_a?(Hash) == true @list = @json.first[1] else @list = @json end else if @json.kind_of?(Array) @list = @json elsif @json.values[0].kind_of?(Array) @list = @json.values[0] else # I think this accounts for plan @list = @json.values end end rescue NoMethodError, HTTP::Error # Sometimes DELETE returns nothing. When this is the case, it will have a status code 204 if @status_code == 204 @json = {"success" => true} else raise ResponseError.new(code = @status_code) end end end