Class: Githu3::ResourceCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/githu3/resource_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, resource_klass, url, params = {}, opts = {}) ⇒ ResourceCollection

Returns a new instance of ResourceCollection.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/githu3/resource_collection.rb', line 14

def initialize client, resource_klass, url, params={}, opts={}
  @resource_klass = resource_klass
  @url = url
  @params = params
  @client = client
  @params = params.stringify_keys
  @pagination = {}
  @current_page = (@params["page"] || 1).to_i
  @resources = []
  @fetch_error = false
  fetch
  self
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



12
13
14
# File 'lib/githu3/resource_collection.rb', line 12

def current_page
  @current_page
end

#fetch_errorObject (readonly)

Returns the value of attribute fetch_error.



12
13
14
# File 'lib/githu3/resource_collection.rb', line 12

def fetch_error
  @fetch_error
end

#paginationObject (readonly)

Returns the value of attribute pagination.



12
13
14
# File 'lib/githu3/resource_collection.rb', line 12

def pagination
  @pagination
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/githu3/resource_collection.rb', line 12

def url
  @url
end

Instance Method Details

#fetch(page = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/githu3/resource_collection.rb', line 28

def fetch(page=nil)
  page = page || @current_page unless @current_page == 1
  begin
    params = @params.dup.stringify_keys
    params["page"] = page unless page.nil?
    res = @client.get(@url, :params => params)
    update_pagination(res, params)
    fetched_resources = []
    res.body.map do |r| 
      resource = @resource_klass.new(r, @client)
      fetched_resources << resource
      @resources << resource unless @resources.include?(resource)
    end if res.body.is_a?(Array)
    fetched_resources
  rescue => err
    @fetch_error = err
    raise err
  end
end

#fetch_firstObject



69
70
71
# File 'lib/githu3/resource_collection.rb', line 69

def fetch_first
  fetch(1)
end

#fetch_lastObject



64
65
66
67
# File 'lib/githu3/resource_collection.rb', line 64

def fetch_last
  return [] unless @pagination["last"]
  fetch(@last_page)
end

#fetch_nextObject



54
55
56
57
# File 'lib/githu3/resource_collection.rb', line 54

def fetch_next
  return [] if (@current_page + 1) > @last_page
  fetch(@current_page + 1)
end

#fetch_prevObject



59
60
61
62
# File 'lib/githu3/resource_collection.rb', line 59

def fetch_prev
  return [] if @current_page == 1
  fetch(@current_page - 1)
end

#next_pageObject



73
74
75
76
# File 'lib/githu3/resource_collection.rb', line 73

def next_page
  np = @current_page + 1
  np < @last_page ? np : nil
end


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/githu3/resource_collection.rb', line 84

def parse_link_header src
  return {} if src.nil?
  links = src.scan(/<http[^<]+/i)
  return [] if links.nil?      
  links.inject({}) do |ll,l|
    m = /<(.*)>;(.*)rel=["|']([^"']*)["|'](.*)/i.match(l)
    uri = URI.parse(m[1])
    query = uri.query.split("&").inject({}) { |q,a| k,v=a.split("="); q.merge(k => v.to_i) }
    ll.merge(m[3] => query["page"])
  end
end

#prev_pageObject



78
79
80
81
# File 'lib/githu3/resource_collection.rb', line 78

def prev_page
  pp = @current_page - 1
  pp > 1 ? pp : nil
end

#update_pagination(res, params) ⇒ Object



48
49
50
51
52
# File 'lib/githu3/resource_collection.rb', line 48

def update_pagination(res, params)
  @pagination = parse_link_header(res.headers['link'])
  @current_page = [1, params["page"].to_i].max
  @last_page = @pagination['last'] unless @pagination['last'].nil?
end