summaryrefslogtreecommitdiff
path: root/lib/github/client.rb
blob: e65d908d232543e3ed277a6b92c5d97dd6c4e06b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module Github
  class Client
    attr_reader :connection, :rate_limit

    def initialize(options)
      @connection = Faraday.new(url: options.fetch(:url)) do |faraday|
        faraday.options.open_timeout = options.fetch(:timeout, 60)
        faraday.options.timeout = options.fetch(:timeout, 60)
        faraday.authorization 'token', options.fetch(:token)
        faraday.adapter :net_http
      end

      @rate_limit = RateLimit.new(connection)
    end

    def get(url, query = {})
      exceed, reset_in = rate_limit.get
      sleep reset_in if exceed

      Github::Response.new(connection.get(url, query))
    end
  end
end