Class: Gigawatt::Commands::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/gigawatt/commands/setup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, options) ⇒ Setup

Returns a new instance of Setup.



43
44
45
46
# File 'lib/gigawatt/commands/setup.rb', line 43

def initialize(settings, options)
  @options = options
  @settings = settings
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/gigawatt/commands/setup.rb', line 4

def options
  @options
end

Class Method Details

.run!(settings) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gigawatt/commands/setup.rb', line 6

def self.run!(settings)
  options = Trollop::options do
    banner <<-EOS
88 Miles Command line application - http://88miles.net

This command will request an access token, giving the command line utility access to your 88 Miles account.

To do this, you will be asked for your 88 Miles login and password. Please note that the login and password will not be saved.

Usage
  88miles setup [options]

options:
    EOS
    opt :force, "Override existing settings", :default => false, :type => :boolean
  end

  instance = self.new(settings, options)

  if instance.settings_exists?
    puts "The settings file #{instance.settings.path} already exists. Use --force to overwrite"
    return SETTINGS_FILE_EXISTS
  end

  instance.preamble

  begin
    instance.authenticate
  rescue OAuth2::Error => e
    puts "There was an issue authenticating your account. Please try again."
    return INVALID_OAUTH_TOKEN_EXIT_CODE
  end
  instance.postamble

  return OK_EXIT_CODE
end

Instance Method Details

#authenticateObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gigawatt/commands/setup.rb', line 75

def authenticate
  client = Gigawatt::OAuth.client

  redirect_uri = Gigawatt::OAuth.redirect_uri
  url = client.auth_code.authorize_url(:response_type => 'token', :redirect_uri => redirect_uri)

  Launchy.open(url) do |exception|
    say "Couldn't open a browser. Please paste the following URL into a browser"
    say url
  end

  say("After you have completed the approval process, cut and paste the URL you are redirected to.")
  access_key = get_access_key(ask("URL: ") do |url|
    url.validate = /\A#{redirect_uri}#access_token=.+&state=\Z/
    url.responses[:not_valid] = "That URL doesn't look right. It should look like: #{redirect_uri}#access_token=[some characters]&state="
  end)

  @settings.access_key = access_key
  @access_key = OAuth.token(access_key)

  cache = Gigawatt::Cache.new(@settings, @access_key)

  cache.refresh!

  @settings.companies = cache.companies
  @settings.projects = cache.projects

  @settings.write(:accesskey)
  @access_key.token
end

#get_access_key(url) ⇒ Object



68
69
70
71
72
73
# File 'lib/gigawatt/commands/setup.rb', line 68

def get_access_key(url)
  uri = URI(url)
  token = uri.fragment.split('&').map{ |kv| kv.split('=') }.delete_if{ |kv| kv[0] != 'access_token' }.first
  return token[1] if token
  return nil
end

#postambleObject



64
65
66
# File 'lib/gigawatt/commands/setup.rb', line 64

def postamble
  say("Thank you. We can now access your account. You can now initialize a directory by running <%= color('88miles init [directory]', BOLD) %>")
end

#preambleObject



57
58
59
60
61
62
# File 'lib/gigawatt/commands/setup.rb', line 57

def preamble
  say("88 Miles command line utility setup")
  say("-----------------------------------")
  say("To setup the 88 Miles command line utility, we need to authenticate you and request an access token.")
  say("We will open a browser, where you will be asked to login and approve access to this app.")
end

#settingsObject



48
49
50
# File 'lib/gigawatt/commands/setup.rb', line 48

def settings
  @settings
end

#settings_exists?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/gigawatt/commands/setup.rb', line 52

def settings_exists?
  return false if options[:force]
  @settings.setup?
end