Class: GH::Remote

Inherits:
Wrapper show all
Defined in:
lib/gh/remote.rb

Overview

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.

Instance Attribute Summary collapse

Attributes inherited from Wrapper

#backend

Instance Method Summary collapse

Methods inherited from Wrapper

[], #frontend, #frontend=, #initialize, #prefixed, wraps

Constructor Details

This class inherits a constructor from GH::Wrapper

Instance Attribute Details

#api_hostObject (readonly)

Returns the value of attribute api_host.



8
9
10
# File 'lib/gh/remote.rb', line 8

def api_host
  @api_host
end

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/gh/remote.rb', line 8

def connection
  @connection
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/gh/remote.rb', line 8

def headers
  @headers
end

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/gh/remote.rb', line 8

def prefix
  @prefix
end

Instance Method Details

#[](key) ⇒ Object

Public: Retrieves resources from Github.

Examples

Github::Remote.new['users/rkh'] # => { ... }

Raises Faraday::Error::ResourceNotFound if the resource returns status 404. Raises Faraday::Error::ClientError if the resource returns a status between 400 and 599. Returns the Response.



64
65
66
67
# File 'lib/gh/remote.rb', line 64

def [](key)
  response = connection.get(path_for(key), headers)
  modify(response.body, response.headers)
end

#inspectObject

Public: …



51
52
53
# File 'lib/gh/remote.rb', line 51

def inspect
  "#<#{self.class}: #{api_host}>"
end

#load(data) ⇒ Object

Public: …



74
75
76
# File 'lib/gh/remote.rb', line 74

def load(data)
  modify(data)
end

#resetObject

Public: …



70
71
# File 'lib/gh/remote.rb', line 70

def reset
end

#setup(api_host, options) ⇒ Object

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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  = options[:headers].try(:dup)  || {
    "Origin"          => options[:origin] || "http://example.org",
    "Accept"          => "application/vnd.github.v3.raw+json," \
                         "application/vnd.github.beta.raw+json;q=0.5," \
                         "application/json;q=0.1",
    "Accept-Charset"  => "utf-8"
  }

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

  @connection = Faraday.new(:url => api_host) do |builder|
    builder.request(:token_auth, token)               if token
    builder.request(:basic_auth, username, password)  if username and password
    builder.request(:retry)
    builder.response(:raise_error)
    builder.adapter(options[:adapter] || :net_http)
  end
end