Module: FridaybuildsCli

Defined in:
lib/fridaybuilds_cli.rb,
lib/fridaybuilds_cli/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

ENDPOINT =
ENV["GIT_ENDPOINT"] || 'https://git.fridaybuilds.com'
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.loginObject



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

def self.
  response = send_request('/login')
  if response['status'] == 'success'
    puts 'You are logged in!'
  else
    token = response['token']
    # credentials are not valid, prompt user to authenticate via browser
    puts "Please visit #{response['url']} to authenticate."
    Launchy.open response['url']
    puts "Waiting for authentication..."
    # check for new credentials
    loop do
      response = send_request('/check-authentication', { token: token })
      if response['status'] == 'success'
        # new credentials received, save to .netrc file
        netrc = Netrc.read
        host = ENDPOINT.gsub('https://', '').gsub('http://', '')
        netrc[host] = response['credentials']
        netrc.save
        puts "Successfully logged in as #{response['credentials'][0]}!"
        break
      end
      sleep(5) # wait 5 seconds before checking again
    end
  end
end

.send_request(path, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fridaybuilds_cli.rb', line 16

def self.send_request(path, params = {})
  uri = URI.join(ENDPOINT, path)
  use_ssl = uri.scheme == 'https'
  uri.query = URI.encode_www_form(params)
  request = Net::HTTP::Get.new(uri.request_uri)
  netrc = Netrc.read
  user, password = netrc[uri.host]
  request.basic_auth(user, password)
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
    http.request(request)
  end
  JSON.parse(response.body)
end

.start(args) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/fridaybuilds_cli.rb', line 57

def self.start(args)
  case args[0]
  when "login"
    
  else
    puts "Invalid command. Please use `friday login`."
  end
end