Class: Gas::GithubSpeaker
- Inherits:
-
Object
- Object
- Gas::GithubSpeaker
- Defined in:
- lib/gas/github_speaker.rb
Overview
A beautiful class that makes working with the github API an enjoyable experience
Instance Attribute Summary collapse
-
#account_name ⇒ Object
readonly
Returns the value of attribute account_name.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#server ⇒ Object
Returns the value of attribute server.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(user, account_name = nil, password = nil, server = 'api.github.com') ⇒ GithubSpeaker
constructor
A new instance of GithubSpeaker.
- #post_key!(rsa) ⇒ Object
- #refresh_keys ⇒ Object
-
#remove_key!(rsa) ⇒ Object
Cycles through github, looking to see if rsa exists as a public key, then deletes it if it does.
Constructor Details
#initialize(user, account_name = nil, password = nil, server = 'api.github.com') ⇒ GithubSpeaker
Returns a new instance of GithubSpeaker.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/gas/github_speaker.rb', line 12 def initialize(user, account_name=nil, password=nil, server = 'api.github.com') @user = user @server = server @keys = nil # sort out username and password... Make it's own function? if account_name.nil? and password.nil? # Prompt for username and password credentials = get_username_and_password_diligently @account_name = credentials[:account_name] # this overwrite happens twice to make testing easier... Stub on get_username, kekeke @password = credentials[:password] else @account_name = account_name @password = password authenticate end end |
Instance Attribute Details
#account_name ⇒ Object (readonly)
Returns the value of attribute account_name.
4 5 6 |
# File 'lib/gas/github_speaker.rb', line 4 def account_name @account_name end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
4 5 6 |
# File 'lib/gas/github_speaker.rb', line 4 def keys @keys end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
4 5 6 |
# File 'lib/gas/github_speaker.rb', line 4 def password @password end |
#server ⇒ Object
Returns the value of attribute server.
5 6 7 |
# File 'lib/gas/github_speaker.rb', line 5 def server @server end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/gas/github_speaker.rb', line 4 def status @status end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
4 5 6 |
# File 'lib/gas/github_speaker.rb', line 4 def user @user end |
Instance Method Details
#post_key!(rsa) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gas/github_speaker.rb', line 32 def post_key!(rsa) refresh_keys if @keys.nil? puts "Posting key to GitHub.com..." # find key... if has_key(rsa) puts "Key already installed." return false end title = "GAS: #{@user.nickname}" result = install_key(rsa) if result != false @keys << result return true end end |
#refresh_keys ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gas/github_speaker.rb', line 52 def refresh_keys raise "Attempted to update keys when unable to authenticate credentials with github" if @status != :authenticated path = '/user/keys' http = Net::HTTP.new(@server,443) req = Net::HTTP::Get.new(path) http.use_ssl = true req.basic_auth @account_name, @password response = http.request(req) @keys = JSON.parse(response.body) end |
#remove_key!(rsa) ⇒ Object
Cycles through github, looking to see if rsa exists as a public key, then deletes it if it does
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gas/github_speaker.rb', line 68 def remove_key!(rsa) refresh_keys # loop through arrays checking against 'key' @keys.each do |key| if key["key"] == rsa return remove_key_by_id!(key["id"]) end end return false # key not found end |