@@ -2,44 +2,58 @@ module Github
22 # Determines the links in the current response link header to be used
33 # to find the links to other pages of request responses. These will
44 # only be present if the result set size exceeds the per page limit.
5+ #
6+ # @api private
57 class PageLinks
68 include Github ::Constants
79
8- DELIM_LINKS = "," . freeze # :nodoc:
10+ DELIM_LINKS = ',' . freeze # :nodoc:
911
1012 # Hold the extracted values for URI from the Link header
1113 # for the first, last, next and previous page.
1214 attr_accessor :first , :last , :next , :prev
1315
16+ LINK_REGEX = /<([^>]+)>; rel=\" ([^\" ]+)\" /
17+
1418 # Parses links from executed request
1519 #
20+ # @param [Hash] response_headers
21+ #
22+ # @api private
1623 def initialize ( response_headers )
1724 link_header = response_headers [ HEADER_LINK ]
18- if link_header
19- return unless link_header =~ /(next|first|last|prev)/
20-
21- link_header . split ( DELIM_LINKS ) . each do |link |
22- if link . strip =~ /<([^>]+)>; rel=\" ([^\" ]+)\" /
23- url_part , meta_part = $1, $2
24- next if !url_part || !meta_part
25- case meta_part
26- when META_FIRST
27- self . first = url_part
28- when META_LAST
29- self . last = url_part
30- when META_NEXT
31- self . next = url_part
32- when META_PREV
33- self . prev = url_part
34- end
35- end
36- end
25+ if link_header && link_header =~ /(next|first|last|prev)/
26+ extract_links ( link_header )
3727 else
3828 # When on the first page
3929 self . next = response_headers [ HEADER_NEXT ]
4030 self . last = response_headers [ HEADER_LAST ]
4131 end
4232 end
4333
34+ private
35+
36+ def extract_links ( link_header )
37+ link_header . split ( DELIM_LINKS ) . each do |link |
38+ LINK_REGEX . match ( link . strip ) do |match |
39+ url_part , meta_part = match [ 1 ] , match [ 2 ]
40+ next if !url_part || !meta_part
41+ assign_url_part ( meta_part , url_part )
42+ end
43+ end
44+ end
45+
46+ def assign_url_part ( meta_part , url_part )
47+ case meta_part
48+ when META_FIRST
49+ self . first = url_part
50+ when META_LAST
51+ self . last = url_part
52+ when META_NEXT
53+ self . next = url_part
54+ when META_PREV
55+ self . prev = url_part
56+ end
57+ end
4458 end # PageLinks
4559end # Github
0 commit comments