Class: GH::Remote
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
-
#api_host ⇒ Object
readonly
Returns the value of attribute api_host.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Attributes inherited from Wrapper
Instance Method Summary collapse
-
#delete(key, body = nil) ⇒ Object
Public: …
-
#fetch_resource(key) ⇒ Object
Internal: …
- #full_url(key) ⇒ Object
-
#generate_response(key, response) ⇒ Object
Internal: …
-
#head(key) ⇒ Object
Public: …
-
#http(verb, url, headers = {}, &block) ⇒ Object
Internal: …
-
#in_parallel ⇒ Object
Public: …
-
#inspect ⇒ Object
Public: …
-
#load(data) ⇒ Object
Public: …
-
#patch(key, body) ⇒ Object
Public: …
- #path_for(key) ⇒ Object
-
#post(key, body) ⇒ Object
Public: …
-
#put(key, body) ⇒ Object
Public: …
-
#request(verb, key, body = nil) ⇒ Object
Internal: …
-
#reset ⇒ Object
Public: …
-
#setup(api_host, options) ⇒ Object
Public: Generates a new Remote instance.
Methods inherited from Wrapper
[], #[], #frontend, #frontend=, #initialize, #prefixed, wraps
Constructor Details
This class inherits a constructor from GH::Wrapper
Instance Attribute Details
#api_host ⇒ Object (readonly)
Returns the value of attribute api_host.
9 10 11 |
# File 'lib/gh/remote.rb', line 9 def api_host @api_host end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/gh/remote.rb', line 9 def connection @connection end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
9 10 11 |
# File 'lib/gh/remote.rb', line 9 def headers @headers end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
9 10 11 |
# File 'lib/gh/remote.rb', line 9 def prefix @prefix end |
Instance Method Details
#delete(key, body = nil) ⇒ Object
Public: …
103 104 105 |
# File 'lib/gh/remote.rb', line 103 def delete(key, body = nil) frontend.request(:delete, key, body) end |
#fetch_resource(key) ⇒ Object
Internal: …
65 66 67 |
# File 'lib/gh/remote.rb', line 65 def fetch_resource(key) frontend.http(:get, frontend.path_for(key), headers) end |
#full_url(key) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/gh/remote.rb', line 136 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) ⇒ Object
Internal: …
70 71 72 73 74 75 76 |
# File 'lib/gh/remote.rb', line 70 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) ⇒ Object
Public: …
108 109 110 |
# File 'lib/gh/remote.rb', line 108 def head(key) frontend.request(:head, key) end |
#http(verb, url, headers = {}, &block) ⇒ Object
Internal: …
79 80 81 82 83 84 |
# File 'lib/gh/remote.rb', line 79 def http(verb, url, headers = {}, &block) body = headers.delete :body connection.run_request(verb, url, body, headers, &block) rescue Exception => error raise Error.new(error, nil, :verb => verb, :url => url, :headers => headers) end |
#in_parallel ⇒ Object
Public: …
132 133 134 |
# File 'lib/gh/remote.rb', line 132 def in_parallel raise RuntimeError, "use GH::Parallel middleware for #in_parallel support" end |
#inspect ⇒ Object
Public: …
60 61 62 |
# File 'lib/gh/remote.rb', line 60 def inspect "#<#{self.class}: #{api_host}>" end |
#load(data) ⇒ Object
Public: …
127 128 129 |
# File 'lib/gh/remote.rb', line 127 def load(data) modify(data) end |
#patch(key, body) ⇒ Object
Public: …
113 114 115 |
# File 'lib/gh/remote.rb', line 113 def patch(key, body) frontend.request(:patch, key, body) end |
#path_for(key) ⇒ Object
144 145 146 |
# File 'lib/gh/remote.rb', line 144 def path_for(key) frontend.full_url(key).request_uri end |
#post(key, body) ⇒ Object
Public: …
98 99 100 |
# File 'lib/gh/remote.rb', line 98 def post(key, body) frontend.request(:post, key, body) end |
#put(key, body) ⇒ Object
Public: …
118 119 120 |
# File 'lib/gh/remote.rb', line 118 def put(key, body) frontend.request(:put, key, body) end |
#request(verb, key, body = nil) ⇒ Object
Internal: …
87 88 89 90 91 92 93 94 95 |
# File 'lib/gh/remote.rb', line 87 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 ⇒ Object
Public: …
123 124 |
# File 'lib/gh/remote.rb', line 123 def reset end |
#setup(api_host, options) ⇒ Object
Public: Generates a new Remote 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).
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 56 57 |
# File 'lib/gh/remote.rb', line 23 def setup(api_host, ) token, username, password = .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" => [:user_agent] || "GH/#{GH::VERSION}", "Accept" => [:accept] || "application/vnd.github.v3+json", "Accept-Charset" => "utf-8", } @headers.merge! [:headers] if [:headers] @headers['Origin'] = [:origin] if [:origin] @prefix = "" @prefix << "#{token}@" if token @prefix << "#{username}:#{password}@" if username and password @prefix << @api_host.host = {:url => api_host} [:ssl] = [:ssl] if [:ssl] .merge! [:faraday_options] if [:faraday_options] @connection = Faraday.new() 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) if defined? FaradayMiddleware::Instrumentation builder.use :instrumentation end builder.response(:logger, nil, formatter: GH.const_get([:formatter].camelize)) if [:formatter] builder.adapter(:net_http) end end |