Class: GithubHelper::FirstTime

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/github_helper/first_time.rb

Overview

First time run ? Let’s make the config file !

Instance Method Summary collapse

Constructor Details

#initializeFirstTime

Returns a new instance of FirstTime.



20
21
22
23
24
25
# File 'lib/github_helper/first_time.rb', line 20

def initialize()
  @config = Hash.new()
  @config = {'credentials' => {'user' => '', 'token' => ''}, 'remote' => {'user' => '', 'repo' => ''}}

  fillArray!
end

Instance Method Details

#fillArray!Object

Interactive building of the YAML file.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/github_helper/first_time.rb', line 28

def fillArray!
  puts "Setting your credentials"
  user = ask("What's your github user name (Won't work with your email) ?")
  password = ask("What's your git password ?\n(This won't be logged, used to get an OAuth token)") { |q| q.echo = false }
  self.class.basic_auth user, password

  @config['credentials']['user'] = user
  @config['credentials']['token'] = getToken(user,password)

  puts "Now let's set up your repo you want to send your pull requests to"
  @config['remote']['user'] = ask("What's the user you want to send your pull requests to ? ")
  @config['remote']['repo'] = ask("What's the user's repo ?")

  puts "Writing to config file : .git/githelper.config.yml"
  header = "# GithubHelper config file\n# You can edit your configuration here\n"
  yaml = @config.to_yaml

  output = header + yaml
  begin
      File.open('.git/githelper.config.yml', 'w') {|f| f.write(output) }
  rescue Exception
      puts "Can't write to .git/githelper.config.yml, are you sure this a git directory?"
      raise
  end

end

#getToken(user, password) ⇒ Object

This let us get the OAuth token



9
10
11
12
13
14
15
16
17
18
# File 'lib/github_helper/first_time.rb', line 9

def getToken(user,password)
    data = {:scopes => ["repo"], :note => "GithubHelper Gem"}
    resp = self.class.post("/authorizations",:body => data.to_json)

    if resp.headers['status'][0..2].to_i != 201
      raise "Can't get the OAuth token : Wrong credentials, please try again"
    end

    resp.parsed_response["token"]
end