Class: GitHubWatched::API::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/github_watched/api.rb

Direct Known Subclasses

User

Instance Method Summary collapse

Instance Method Details

#connectionObject



5
6
7
8
# File 'lib/github_watched/api.rb', line 5

def connection
  OctocatHerder::Connection.headers['User-Agent'] = "github-watched/#{GitHubWatched::VERSION} (+https://github.com/jasoncodes/github-watched)"
  @connection ||= OctocatHerder::Connection.new
end

#list(end_point, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/github_watched/api.rb', line 10

def list(end_point, options = {})
  paginated = options.delete(:paginated)
  klass = options.delete(:klass)
  options[:params] ||= {}
  options[:params][:per_page] ||= 100

  Enumerator.new do |y|
    begin
      result = connection.raw_get(end_point, options)
      raise "Unable to retrieve #{end_point}" unless result

      result.parsed_response.each do |item|
        item = klass.new(item, connection) if klass
        y << item
      end

      options[:params][:page] = paginated && connection.page_from_headers(result.headers, 'next')
    end while options[:params][:page]
  end
end