Sha256: ccac0fd861846c35b4c34791db6fc1af347d03680d0df3cafe815aea22ba51d7

Contents?: true

Size: 982 Bytes

Versions: 1

Compression:

Stored size: 982 Bytes

Contents

require 'net/https'
require 'singleton'

module GitHub
  class Api
    include Singleton

    attr_writer :auth

    def auth
      @auth || {}
    end

    def authenticated?
      auth != {}
    end

    #TODO: need to fix it in terms of options and add tests
    def ensure_auth opts ={}
      return if authenticated?
      login = opts[:login]
      token = opts[:token]
      raise("Authentication failed") unless login && token
      @auth = {'login'=>login, 'token'=>token}
    end

    def request verb, url, data = {}
      method = ('Net::HTTP::' + verb.to_s.capitalize).to_class
      uri = URI.parse url
      server = Net::HTTP.new(uri.host, uri.port)
      server.use_ssl = (uri.scheme == 'https')
      server.verify_mode = OpenSSL::SSL::VERIFY_NONE if server.use_ssl?
      server.start do |http|
        req = method.new(uri.path)
        req.form_data = data.merge(auth)
        http.request(req)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git_hub-0.3.0 lib/git_hub/api.rb