class NagaApiClient

Attributes

authentication_token[R]
credentials[R]
email[R]

Public Class Methods

BASE_URL() click to toggle source
# File lib/minitest/naga/naga_api_client.rb, line 21
def self.BASE_URL
  @@BASE_URL
end
FILE_PATH() click to toggle source
# File lib/minitest/naga/naga_api_client.rb, line 25
def self.FILE_PATH
  @@FILE_PATH
end
new() click to toggle source
# File lib/minitest/naga/naga_api_client.rb, line 10
def initialize
  @options = { headers: { "Content-Type" => "application/x-www-form-urlencoded" } }
  @authenticated = false
  if File.file?(@@FILE_PATH)
    @credentials = eval(File.read(@@FILE_PATH))
    validate(@credentials)
  else
    authenticate
  end
end

Public Instance Methods

authenticated?() click to toggle source
# File lib/minitest/naga/naga_api_client.rb, line 33
def authenticated?
  @authenticated
end

Private Instance Methods

authenticate() click to toggle source
# File lib/minitest/naga/naga_api_client.rb, line 39
def authenticate
  cli = HighLine.new
  email = cli.ask "Enter your email: "
  password = cli.ask("Enter your password: ") { |q| q.echo = false }
  url = @@BASE_URL + "sessions"
  options = @options
  options[:body] = "email=" + email + "&password=" + password
  response = HTTParty.post(url, options)
  parsed_response = response.parsed_response
  if parsed_response["error"]
    STDERR.puts(("Error: " + parsed_response["error"]).colorize(:red))
    @authenticated = false
    authenticate
  else
    puts "Authentication successful".colorize(:green)
    f = File.open(@@FILE_PATH, "w")
    f.write(parsed_response)
    f.close
    @email = parsed_response["email"]
    @authentication_token = parsed_response["authentication_token"]
    @authenticated = true
  end
end
validate(credentials) click to toggle source
# File lib/minitest/naga/naga_api_client.rb, line 63
def validate(credentials)
  url = @@BASE_URL + "sessions/validate"
  options = @options
  options[:body] = "email=" + credentials["email"] + "&authentication_token=" + credentials["authentication_token"]
  response = HTTParty.post(url, options)
  parsed_response = response.parsed_response
  if parsed_response["error"]
    STDERR.puts(("Error: " + parsed_response["error"]).colorize(:red))
    @authenticated = false
    authenticate
  else
    @authenticated = true
    @email = parsed_response["email"]
    @authentication_token = parsed_response["authentication_token"]
    puts "Signed in as " + @email
  end
end