Class: VpsCli::GithubHTTP
- Inherits:
-
Object
- Object
- VpsCli::GithubHTTP
- Defined in:
- lib/vps_cli/helpers/github_http.rb
Overview
An http wrapper for github api request
Instance Attribute Summary collapse
-
#ssh_file ⇒ Object
writeonly
Sets the attribute ssh_file.
-
#title ⇒ Object
Returns the value of attribute title.
-
#token ⇒ Object
writeonly
Sets the attribute token.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#get_request ⇒ Net::HTTP::Get
Returns a new get request class.
-
#headers(token:) ⇒ Hash
The headers need for authorization of a request.
-
#initialize(uri:, token:, ssh_file:, title:) ⇒ GithubHTTP
constructor
A new instance of GithubHTTP.
-
#post_request(data:) ⇒ Net::HTTP::Post
Returns a new post request class.
-
#push_ssh_key ⇒ Object
Pushes your public key to github to push your ssh key to the github.
-
#ssh_json_string ⇒ String
Returns the appropriate json string to write an ssh key.
-
#ssh_key_exist?(json_string:) ⇒ Boolean
Checks if the ssh key is already found.
- #ssh_key_found_msg ⇒ Object
-
#start_connection {|http| ... } ⇒ Object
base method for an http connection.
Constructor Details
#initialize(uri:, token:, ssh_file:, title:) ⇒ GithubHTTP
Returns a new instance of GithubHTTP.
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
10 11 12 |
# File 'lib/vps_cli/helpers/github_http.rb', line 10 def ssh_file=(value) @ssh_file = value end |
#title ⇒ Object
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
10 11 12 |
# File 'lib/vps_cli/helpers/github_http.rb', line 10 def token=(value) @token = value end |
#uri ⇒ Object
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_request ⇒ 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
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
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_key ⇒ Object
Pushes your public key to github to push your ssh key to the github
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_string ⇒ String
Returns the appropriate json 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
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_msg ⇒ Object
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
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 |