Module: GHTorrent::APIClient
- Included in:
- GHTMirrorEvents, Retriever
- Defined in:
- lib/ghtorrent/api_client.rb
Constant Summary
Constants included from Settings
Settings::CONFIGKEYS, Settings::DEFAULTS
Instance Method Summary collapse
-
#api_request(url) ⇒ Object
A normal request.
-
#num_pages(url) ⇒ Object
Determine the number of pages contained in a multi-page API response.
-
#paged_api_request(url, pages = config(:mirror_history_pages_back), last = nil) ⇒ Object
A paged request.
Methods included from Settings
#config, #merge, #merge_config_values, #override_config, #settings
Methods included from Utils
included, #read_value, #user_type, #write_value
Methods included from Logging
#debug, #error, #info, #loggerr, #warn
Instance Method Details
#api_request(url) ⇒ Object
A normal request. Returns a hash or an array of hashes representing the parsed JSON result.
58 59 60 |
# File 'lib/ghtorrent/api_client.rb', line 58 def api_request(url) parse_request_result api_request_raw(ensure_max_per_page(url)) end |
#num_pages(url) ⇒ Object
Determine the number of pages contained in a multi-page API response
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ghtorrent/api_client.rb', line 63 def num_pages(url) url = ensure_max_per_page(url) data = api_request_raw(url) if data.nil? or data..nil? or data.['link'].nil? return 1 end links = parse_links(data.['link']) if links.nil? or links['last'].nil? return 1 end params = CGI::parse(URI::parse(links['last']).query) params['page'][0].to_i end |
#paged_api_request(url, pages = config(:mirror_history_pages_back), last = nil) ⇒ Object
A paged request. Used when the result can expand to more than one result pages.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ghtorrent/api_client.rb', line 26 def paged_api_request(url, pages = config(:mirror_history_pages_back), last = nil) url = ensure_max_per_page(url) data = api_request_raw(url) return [] if data.nil? unless data.['link'].nil? links = parse_links(data.['link']) last = links['last'] if last.nil? if pages > 0 pages = pages - 1 if pages == 0 return parse_request_result(data) end end if links['next'].nil? parse_request_result(data) else parse_request_result(data) | paged_api_request(links['next'], pages, last) end else parse_request_result(data) end end |