class GH::Remote

Public: This class deals with HTTP requests to Github. It is the base Wrapper you always want to use. Note that it is usually used implicitely by other wrapper classes if not specified.

Attributes

api_host[R]
connection[R]
headers[R]
prefix[R]

Public Instance Methods

delete(key) click to toggle source

Public: …

# File lib/gh/remote.rb, line 101
def delete(key)
  frontend.request(:delete, key)
end
fetch_resource(key) click to toggle source

Internal: …

# File lib/gh/remote.rb, line 64
def fetch_resource(key)
  frontend.http(:get, frontend.path_for(key), headers)
end
full_url(key) click to toggle source
# File lib/gh/remote.rb, line 134
def full_url(key)
  uri      = Addressable::URI.parse(key)
  uri.path = File.join(api_host.path, uri.path) unless uri.absolute? or uri.path.start_with?(api_host.path)
  uri      = api_host + uri
  raise ArgumentError, "URI out of scope: #{key}" if uri.host != api_host.host
  uri
end
generate_response(key, response) click to toggle source

Internal: …

# File lib/gh/remote.rb, line 69
def generate_response(key, response)
  body, headers = response.body, response.headers
  url = response.env[:url]     if response.respond_to? :env and response.env
  url = response.url           if response.respond_to?(:url)
  url = frontend.full_url(key) if url.to_s.empty?
  modify(body, headers, url)
end
head(key) click to toggle source

Public: …

# File lib/gh/remote.rb, line 106
def head(key)
  frontend.request(:head, key)
end
http(verb, url, headers = {}, &block) click to toggle source

Internal: …

# File lib/gh/remote.rb, line 78
def http(verb, url, headers = {}, &block)
  connection.run_request(verb, url, nil, headers, &block)
rescue Exception => error
  raise Error.new(error, nil, :verb => verb, :url => url, :headers => headers)
end
in_parallel() click to toggle source

Public: …

# File lib/gh/remote.rb, line 130
def in_parallel
  raise RuntimeError, "use GH::Parallel middleware for #in_parallel support"
end
inspect() click to toggle source

Public: …

# File lib/gh/remote.rb, line 59
def inspect
  "#<#{self.class}: #{api_host}>"
end
load(data) click to toggle source

Public: …

# File lib/gh/remote.rb, line 125
def load(data)
  modify(data)
end
patch(key, body) click to toggle source

Public: …

# File lib/gh/remote.rb, line 111
def patch(key, body)
  frontend.request(:patch, key, body)
end
path_for(key) click to toggle source
# File lib/gh/remote.rb, line 142
def path_for(key)
  frontend.full_url(key).request_uri
end
post(key, body) click to toggle source

Public: …

# File lib/gh/remote.rb, line 96
def post(key, body)
  frontend.request(:post, key, body)
end
put(key, body) click to toggle source

Public: …

# File lib/gh/remote.rb, line 116
def put(key, body)
  frontend.request(:put, key, body)
end
request(verb, key, body = nil) click to toggle source

Internal: …

# File lib/gh/remote.rb, line 85
def request(verb, key, body = nil)
  response = frontend.http(verb, path_for(key), headers) do |req|
    req.body = Response.new(body).to_s if body
  end
  frontend.generate_response(key, response)
rescue GH::Error => error
  error.info[:payload] = Response.new(body).to_s if body
  raise error
end
reset() click to toggle source

Public: …

# File lib/gh/remote.rb, line 121
def reset
end
setup(api_host, options) click to toggle source

Public: Generates a new Rempte instance.

#api_host - HTTP host to send requests to, has to include schema (https or http) options - Hash with configuration options:

:token    - OAuth token to use (optional).
:username - Github user used for login (optional).
:password - Github password used for login (optional).
:origin   - Value of the origin request header (optional).
:headers  - HTTP headers to be send on every request (optional).
:adapter  - HTTP library to use for making requests (optional, default: :net_http)

It is highly recommended to set origin, but not to set headers. If you set the username, you should also set the password.

# File lib/gh/remote.rb, line 23
def setup(api_host, options)
  token, username, password = options.values_at :token, :username, :password

  api_host  = api_host.api_host if api_host.respond_to? :api_host
  @api_host = Addressable::URI.parse(api_host)
  @headers  = {
    "User-Agent"      => options[:user_agent] || "GH/#{GH::VERSION}",
    "Accept"          => "application/vnd.github.v3+json,"                               "application/vnd.github.beta+json;q=0.5,"                               "application/json;q=0.1",
    "Accept-Charset"  => "utf-8",
  }

  @headers.merge! options[:headers] if options[:headers]
  @headers['Origin'] = options[:origin] if options[:origin]

  @prefix = ""
  @prefix << "#{token}@" if token
  @prefix << "#{username}:#{password}@" if username and password
  @prefix << @api_host.host

  faraday_options = {:url => api_host}
  faraday_options[:ssl] = options[:ssl] if options[:ssl]
  faraday_options.merge! options[:faraday_options] if options[:faraday_options]

  @connection = Faraday.new(faraday_options) do |builder|
    builder.request(:authorization, :token, token) if token
    builder.request(:basic_auth, username, password)  if username and password
    builder.request(:retry)
    builder.response(:raise_error)
    #builder.use(options[:adapter] || GH::FaradayAdapter)
    builder.adapter(:net_http)
  end
end

Private Instance Methods

identifier(key) click to toggle source
# File lib/gh/remote.rb, line 148
def identifier(key)
  path_for(key)
end
modify(body, headers = {}, url = nil) click to toggle source
# File lib/gh/remote.rb, line 152
def modify(body, headers = {}, url = nil)
  return body if body.is_a? Response
  Response.new(body, headers, url)
end