Class: Githu3::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/githu3/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|@conn| ... } ⇒ Connection

Returns a new instance of Connection.

Yields:



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

def initialize opts={}
  @conn = Faraday.new({ :url => Githu3::Client::BaseUrl })
  
  @conn.adapter opts[:adapter] if opts[:adapter]
  @conn.use FaradayMiddleware::ParseJson
  @conn.use Faraday::Response::RaiseGithu3Error
  
  if opts[:cache]
    cache_klass = Githu3::Cache.const_get(opts[:cache].to_s.camelize)
    @cache = cache_klass.new(opts[:cache_config])
  end
  
  yield @conn if block_given?
  
  @rate_limit = {}
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



7
8
9
# File 'lib/githu3/connection.rb', line 7

def cache
  @cache
end

#connObject (readonly)

Returns the value of attribute conn.



7
8
9
# File 'lib/githu3/connection.rb', line 7

def conn
  @conn
end

#rate_limitObject (readonly)

Returns the value of attribute rate_limit.



7
8
9
# File 'lib/githu3/connection.rb', line 7

def rate_limit
  @rate_limit
end

Instance Method Details

#get(url, headers = {}, opts = {}) ⇒ Object



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

def get url, headers={}, opts={}
  use_cache = !@cache.nil? && !opts[:bypass_cache]
  if use_cache
    ref = Digest::SHA1.hexdigest [url, headers.to_s].join("")
    res = @cache.get ref
    return res unless res.nil?
  end
  res = @conn.get(url, headers)
  if use_cache
    @cache.set(ref, res) if res.status < 400
  else
    @rate_limit[:limit] = res.headers["x-ratelimit-limit"]
    @rate_limit[:remaining] = res.headers["x-ratelimit-remaining"]
  end
  res
end