Class: Gas::GithubSpeaker

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, =nil, password=nil, server = 'api.github.com')
  @user = user
  @server = server
  @keys = nil
  
  # sort out username and password...  Make it's own function?
  if .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 = 
    @password = password
    authenticate
  end
  
end

Instance Attribute Details

#account_nameObject (readonly)

Returns the value of attribute account_name.



4
5
6
# File 'lib/gas/github_speaker.rb', line 4

def 
  @account_name
end

#keysObject (readonly)

Returns the value of attribute keys.



4
5
6
# File 'lib/gas/github_speaker.rb', line 4

def keys
  @keys
end

#passwordObject (readonly)

Returns the value of attribute password.



4
5
6
# File 'lib/gas/github_speaker.rb', line 4

def password
  @password
end

#serverObject

Returns the value of attribute server.



5
6
7
# File 'lib/gas/github_speaker.rb', line 5

def server
  @server
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/gas/github_speaker.rb', line 4

def status
  @status
end

#userObject (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_keysObject



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