Class: VpsCli::GithubHTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/vps_cli/helpers/github_http.rb

Overview

An http wrapper for github api request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, token:, ssh_file:, title:) ⇒ GithubHTTP

Returns a new instance of GithubHTTP.

Parameters:

  • uri (URI)

    the url to hit

  • token (String)

    Github API token to use

  • ssh_key (String)

    Your ssh file IE: ~/.ssh/id_rsa.pub

  • title (String)

    The title of your ssh key Ensure it is in the format: “token 14942842859”



17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#ssh_file=(value) ⇒ Object (writeonly)

Sets the attribute ssh_file

Parameters:

  • value

    the value to set the attribute ssh_file to.



10
11
12
# File 'lib/vps_cli/helpers/github_http.rb', line 10

def ssh_file=(value)
  @ssh_file = value
end

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/vps_cli/helpers/github_http.rb', line 9

def title
  @title
end

#token=(value) ⇒ Object (writeonly)

Sets the attribute token

Parameters:

  • value

    the value to set the attribute token to.



10
11
12
# File 'lib/vps_cli/helpers/github_http.rb', line 10

def token=(value)
  @token = value
end

#uriObject

Returns the value of attribute uri.



9
10
11
# File 'lib/vps_cli/helpers/github_http.rb', line 9

def uri
  @uri
end

Instance Method Details

#get_requestNet::HTTP::Get

Returns a new get request class

Parameters:

  • token (String)

    Your github api token

Returns:

  • (Net::HTTP::Get)

    Returns a new get request class



80
81
82
# File 'lib/vps_cli/helpers/github_http.rb', line 80

def get_request
  Net::HTTP::Get.new(@uri, @headers)
end

#headers(token:) ⇒ Hash

The headers need for authorization of a request

Parameters:

  • token (String)

    (nil) Your github API token @see github.com/settings/keys make sure your token has write:public_key and read:public_key access

Returns:

  • (Hash)

    Returns a hash of headers



31
32
33
34
35
36
37
# 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:) ⇒ Net::HTTP::Post

Returns a new post request class

Parameters:

  • data (String)

    The data to send in the post request, must be json

Returns:

  • (Net::HTTP::Post)

    Returns a new post request class



86
87
88
89
90
# 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_keyObject

Pushes your public key to github to push your ssh key to the github

Returns:

  • Net::HTTPResponse



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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_stringString

Returns the appropriate json string to write an ssh key

Returns:

  • (String)

    Returns a json formatted string to write an ssh key



41
42
43
44
# 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:) ⇒ Boolean

Checks if the ssh key is already found

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
105
# 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_msgObject



107
108
109
# 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| ... } ⇒ Object

base method for an http connection

Yield Parameters:

  • http (Net::HTTP)

    yields the http class

Returns:

  • Whatever the value of yield is



49
50
51
52
53
# 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