Sha256: 4a076ef54e9c5c55d72ca57d99f40d34e847fe54e62d579cb79fcfe600059aac

Contents?: true

Size: 987 Bytes

Versions: 5

Compression:

Stored size: 987 Bytes

Contents

class NirvanaHQ

  attr_accessor :username
  attr_accessor :password

  TOKEN_FILE = "#{ENV['HOME']}/.nirvanahq/token"

  def initialize config
    
    if(nil == config)
      return
    end
    
    @username= config[:username]
    @password= config[:password]

    begin
      # is this the spot to check is the dir exists? 
      File.new(TOKEN_FILE, 'w').write('') 
    end unless File.exists?(TOKEN_FILE) 

    @token = File.open(TOKEN_FILE, 'r').read

    if @token.empty? then
      save_token!(fetch_token)
    end

  rescue "EmptyToken"
    puts "Token Issues."
  end

  def fetch_token
    puts "Fetching your token..."
    result = `curl -sX POST 'https://api.nirvanahq.com/?api=rest' -d "method=auth.new&u=#{@username}&p=#{@password}"`
    parser = JSON.parse(result)
    parser['results'][0]['auth']['token'] || ''
  end

  def save_token! token
    raise "EmptyToken" if  token.nil? || token.empty?
    File.open(TOKEN_FILE, 'w').write(token)
    @token = token
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nirvanahq-0.1.4 lib/nirvanahq/auth.rb
nirvanahq-0.1.3 lib/nirvanahq/auth.rb
nirvanahq-0.1.1 lib/nirvanahq/auth.rb
nirvanahq-0.1.0 lib/nirvanahq/auth.rb
nirvanahq-0.0.4 lib/nirvanahq/auth.rb