class Github::Auth::KeysClient

Client for fetching public SSH keys using the Github API

Constants

DEFAULT_HOSTNAME
DEFAULT_OPTIONS
GithubUnavailableError
GithubUserDoesNotExistError
USER_AGENT
UsernameRequiredError

Attributes

hostname[R]
username[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/github/auth/keys_client.rb, line 22
def initialize(options = {})
  options = DEFAULT_OPTIONS.merge options
  raise UsernameRequiredError unless options.fetch :username

  @username = CGI.escape(options.fetch :username)
  @hostname = options.fetch :hostname
end

Public Instance Methods

keys() click to toggle source
# File lib/github/auth/keys_client.rb, line 30
def keys
  @keys ||= Array(github_response).map do |entry|
    Github::Auth::Key.new username, entry.fetch('key')
  end
end

Private Instance Methods

github_response() click to toggle source
# File lib/github/auth/keys_client.rb, line 38
def github_response
  response = http_client.get \
    "#{hostname}/users/#{username}/keys", headers: headers

  raise GithubUserDoesNotExistError if response.status == 404

  JSON.parse response.body
rescue Faraday::Error => e
  raise GithubUnavailableError, e
end
headers() click to toggle source
# File lib/github/auth/keys_client.rb, line 53
def headers
  { 'User-Agent' => USER_AGENT }
end
http_client() click to toggle source
# File lib/github/auth/keys_client.rb, line 49
def http_client
  Faraday
end