Sha256: 8a64347a4149f0517c20fedce1230d0064daebaa9bf136880b7acb152cbfde08

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

require 'digest/sha1'

module Githu3
  
  class Connection
    
    attr_reader :rate_limit, :cache, :conn
    
    
    
    def initialize opts={}
      @conn = Faraday.new({ :url => Githu3::Client::BaseUrl })
      
      @conn.adapter opts[:adapter] if opts[:adapter]
      @conn.use Faraday::Response::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
    
    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
    
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
githu3-0.0.7 lib/githu3/connection.rb
githu3-0.0.6 lib/githu3/connection.rb
githu3-0.0.5 lib/githu3/connection.rb
githu3-0.0.4 lib/githu3/connection.rb