Class: Weeblycloud::WeeblyCloudResponse

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/weeblycloud/cloudclient/weeblycloudresponse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#jsonObject (readonly)

Returns the value of attribute json.



9
10
11
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 9

def json
  @json
end

#listObject (readonly)

Returns the value of attribute list.



9
10
11
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 9

def list
  @list
end

#max_pageObject (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_sizeObject (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

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

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

Returns:

  • (Boolean)


30
31
32
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 30

def paginated?
  @is_paginated
end

#to_hashObject

Returns the current page as a hash



76
77
78
# File 'lib/weeblycloud/cloudclient/weeblycloudresponse.rb', line 76

def to_hash()
  @json
end

#to_sObject

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