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, #options

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

#delete(key) ⇒ Object

Public: …



96
97
98
# File 'lib/gh/remote.rb', line 96

def delete(key)
  frontend.request(:delete, key)
end

#fetch_resource(key) ⇒ Object

Internal: …



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

def fetch_resource(key)
  frontend.http(:get, path_for(key), headers)
end

#generate_response(key, response) ⇒ Object

Internal: …



68
69
70
71
72
73
# File 'lib/gh/remote.rb', line 68

def generate_response(key, response)
  body, headers = response.body, response.headers
  url = response.respond_to?(:url) ? response.url : response.env.try(:[], :url)
  url = full_url(key) if url.to_s.empty?
  modify(body, headers, url)
end

#head(key) ⇒ Object

Public: …



101
102
103
# File 'lib/gh/remote.rb', line 101

def head(key)
  frontend.request(:head, key)
end

#http(verb, url, headers = {}, &block) ⇒ Object

Internal: …



76
77
78
79
80
# File 'lib/gh/remote.rb', line 76

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_parallelObject

Public: …

Raises:

  • (RuntimeError)


125
126
127
# File 'lib/gh/remote.rb', line 125

def in_parallel
  raise RuntimeError, "use GH::Parallel middleware for #in_parallel support"
end

#inspectObject

Public: …



58
59
60
# File 'lib/gh/remote.rb', line 58

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

#load(data) ⇒ Object

Public: …



120
121
122
# File 'lib/gh/remote.rb', line 120

def load(data)
  modify(data)
end

#patch(key, body) ⇒ Object

Public: …



106
107
108
# File 'lib/gh/remote.rb', line 106

def patch(key, body)
  frontend.request(:patch, key, body)
end

#post(key, body) ⇒ Object

Public: …



91
92
93
# File 'lib/gh/remote.rb', line 91

def post(key, body)
  frontend.request(:post, key, body)
end

#put(key, body) ⇒ Object

Public: …



111
112
113
# File 'lib/gh/remote.rb', line 111

def put(key, body)
  frontend.request(:put, key, body)
end

#request(verb, key, body = nil) ⇒ Object

Internal: …



83
84
85
86
87
88
# File 'lib/gh/remote.rb', line 83

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)
end

#resetObject

Public: …



116
117
# File 'lib/gh/remote.rb', line 116

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
49
50
51
52
53
54
55
# 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)  || {
    "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['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