Module: Github::Key::Upload

Defined in:
lib/github-key-upload.rb,
lib/github-key-upload/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.check(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/github-key-upload.rb', line 33

def self.check(options)
  github = create_auth(options)
  matching_key = nil
  search_key = options[:search_key].to_s
  search_proc = options[:search_proc]

  github.users.keys.all.each_page do |page|
    if page.map {|k| k[search_key]}.include?(search_proc.call(options))
      matching_key = true
      break
    end
  end

  matching_key
end

.check_for_both(options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/github-key-upload.rb', line 18

def self.check_for_both(options)
  github = create_auth(options)
  matching_key = nil

  github.users.keys.all.each_page do |page|
    if page.map {|k| [k["title"], k["key"]]}.include?(
        [options[:title], File.read(options[:key]).split(" ")[0,2].join(" ")])
      matching_key = true
      break
    end
  end

  matching_key
end

.check_for_key(options) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/github-key-upload.rb', line 49

def self.check_for_key(options)
  check(options.merge({
                        :search_key => :key,
                        :search_proc => Proc.new {|opts|
                          File.read(opts[:key]).split(" ")[0,2].join(" ")
                        }
                      }))
end

.check_for_title(options) ⇒ Object



58
59
60
61
62
63
# File 'lib/github-key-upload.rb', line 58

def self.check_for_title(options)
  check(options.merge({
                        :search_key => :title,
                        :search_proc => Proc.new {|opts| opts[:title]}
                      }))
end

.create_auth(options) ⇒ Object



7
8
9
10
11
# File 'lib/github-key-upload.rb', line 7

def self.create_auth(options)
  github = Github.new :basic_auth => "#{options[:user]}:#{options[:password]}"
  github.oauth.create 'scopes' => ['user']
  github
end

.upload(options) ⇒ Object



13
14
15
16
# File 'lib/github-key-upload.rb', line 13

def self.upload(options)
  github = create_auth(options)
  github.users.keys.create({:title => options[:title], :key => File.read(options[:key])})
end