class Octokit::Client
Client
for the GitHub API
@see developer.github.com
Constants
- CONVENIENCE_HEADERS
Header keys that can be passed in options hash to {#get},{#head}
Public Class Methods
# File lib/octokit/client.rb, line 127 def initialize(options = {}) # Use options passed in, but fall back to module defaults Octokit::Configurable.keys.each do |key| value = options.key?(key) ? options[key] : Octokit.instance_variable_get(:"@#{key}") instance_variable_set(:"@#{key}", value) end login_from_netrc unless user_authenticated? || application_authenticated? end
Public Instance Methods
Set OAuth access token for authentication
@param value [String] 40 character GitHub OAuth access token
# File lib/octokit/client.rb, line 198 def access_token=(value) reset_agent @access_token = value end
Duplicate client using client_id and client_secret as Basic Authentication
credentials. @example
Octokit.client_id = "foo" Octokit.client_secret = "bar" # GET https://api.github.com/?client_id=foo&client_secret=bar Octokit.get "/" Octokit.client.as_app do |client| # GET https://foo:bar@api.github.com/ client.get "/" end
# File lib/octokit/client.rb, line 167 def as_app(key = client_id, secret = client_secret, &block) if key.to_s.empty? || secret.to_s.empty? raise ApplicationCredentialsRequired, "client_id and client_secret required" end app_client = self.dup app_client.client_id = app_client.client_secret = nil app_client.login = key app_client.password = secret yield app_client if block_given? end
Set Bearer Token for authentication
@param value [String] JWT
# File lib/octokit/client.rb, line 206 def bearer_token=(value) reset_agent @bearer_token = value end
Set OAuth app client_id
@param value [String] 20 character GitHub OAuth app client_id
# File lib/octokit/client.rb, line 214 def client_id=(value) reset_agent @client_id = value end
Set OAuth app client_secret
@param value [String] 40 character GitHub OAuth app client_secret
# File lib/octokit/client.rb, line 222 def client_secret=(value) reset_agent @client_secret = value end
# File lib/octokit/client.rb, line 227 def client_without_redirects(options = {}) conn_opts = @connection_options conn_opts[:url] = @api_endpoint conn_opts[:builder] = @middleware.dup if @middleware conn_opts[:proxy] = @proxy if @proxy conn_opts[:ssl] = { :verify_mode => @ssl_verify_mode } if @ssl_verify_mode conn = Faraday.new(conn_opts) do |http| if basic_authenticated? http.basic_auth(@login, @password) elsif token_authenticated? http.authorization 'token', @access_token elsif bearer_authenticated? http.authorization 'Bearer', @bearer_token end http.headers['accept'] = options[:accept] if options.key?(:accept) end conn.builder.delete(Octokit::Middleware::FollowRedirects) conn end
Text representation of the client, masking tokens and passwords
@return [String]
# File lib/octokit/client.rb, line 140 def inspect inspected = super # mask password inspected.gsub! @password, '*******' if @password inspected.gsub! @management_console_password, '*******' if @management_console_password inspected.gsub! @bearer_token, '********' if @bearer_token # Only show last 4 of token, secret inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}" if @access_token inspected.gsub! @client_secret, "#{'*'*36}#{@client_secret[36..-1]}" if @client_secret inspected end
Set username for authentication
@param value [String] GitHub username
# File lib/octokit/client.rb, line 182 def login=(value) reset_agent @login = value end
Set password for authentication
@param value [String] GitHub password
# File lib/octokit/client.rb, line 190 def password=(value) reset_agent @password = value end
Private Instance Methods
convenience method for constructing a user specific path, if the user is logged in
# File lib/octokit/client/users.rb, line 347 def user_path(user, path) if user == login && user_authenticated? "user/#{path}" else "#{User.path user}/#{path}" end end