class VpsCli::GithubHTTP

An http wrapper for github api request

Attributes

ssh_file[W]
title[RW]
token[W]
uri[RW]

Public Class Methods

new(uri:, token:, ssh_file:, title:) click to toggle source

@param uri [URI] the url to hit @param token [String] Github API token to use @param ssh_key [String] Your ssh file IE: ~/.ssh/id_rsa.pub @param title [String] The title of your ssh key

Ensure it is in the format: "token 14942842859"
# File lib/vps_cli/helpers/github_http.rb, line 17
def initialize(uri:, token:, ssh_file:, title:)
  @uri = uri
  @token = token
  @ssh_file = ssh_file
  @ssh_key = File.read(ssh_file)
  @headers = headers(token: token)
  @title = title
end

Public Instance Methods

get_request() click to toggle source

@param token [String] Your github api token @return [Net::HTTP::Get] Returns a new get request class

# File lib/vps_cli/helpers/github_http.rb, line 80
def get_request
  Net::HTTP::Get.new(@uri, @headers)
end
headers(token:) click to toggle source

The headers need for authorization of a request @param token [String] (nil) Your github API token

@see https://github.com/settings/keys
make sure your token has write:public_key and read:public_key access

@return [Hash] Returns a hash of headers

# File lib/vps_cli/helpers/github_http.rb, line 31
def headers(token:)
  json = 'application/json'

  { 'Content-Type' => json,
    'Accepts' => json,
    'Authorization' => token }
end
post_request(data:) click to toggle source

@param data [String] The data to send in the post request, must be json @return [Net::HTTP::Post] Returns a new post request class

# File lib/vps_cli/helpers/github_http.rb, line 86
def post_request(data:)
  post = Net::HTTP::Post.new(@uri, @headers)
  post.body = data
  post
end
push_ssh_key() click to toggle source

Pushes your public key to github to push your ssh key to the github @return Net::HTTPResponse

# File lib/vps_cli/helpers/github_http.rb, line 58
def push_ssh_key
  get = get_request

  post = post_request(data: ssh_json_string)

  response = start_connection do |http|
    get_response = http.request(get)

    ssh_keys_json = get_response.body
    return ssh_key_found_msg if ssh_key_exist?(json_string: ssh_keys_json)

    http.request(post)
  end

  VpsCli.errors << response if response != Net::HTTPCreated

  puts 'ssh key pushed to github' if response.class == Net::HTTPCreated
  p response
end
ssh_json_string() click to toggle source

Returns the appropriate json string to write an ssh key @return [String] Returns a json formatted string to write an ssh key

# File lib/vps_cli/helpers/github_http.rb, line 41
def ssh_json_string
  { 'title' => @title,
    'key' => @ssh_key }.to_json
end
ssh_key_exist?(json_string:) click to toggle source

Checks if the ssh key is already found

# File lib/vps_cli/helpers/github_http.rb, line 93
def ssh_key_exist?(json_string:)
  # just in case your ssh key has a comment in it
  # keys pulled from github will not have comments
  ssh_key = if @ssh_key.include?('==')
              @ssh_key.split('==')[0].concat('==')
            else
              @ssh_key
            end

  JSON.parse(json_string).any? do |data|
    data['key'] == ssh_key
  end
end
ssh_key_found_msg() click to toggle source
# File lib/vps_cli/helpers/github_http.rb, line 107
def ssh_key_found_msg
  puts 'The ssh key provided is already on github, no post request made.'
end
start_connection() { |http| ... } click to toggle source

base method for an http connection @yieldparam http [Net::HTTP] yields the http class @return Whatever the value of yield is

# File lib/vps_cli/helpers/github_http.rb, line 49
def start_connection
  Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
    yield(http)
  end
end