Class: NirvanaHQ

Inherits:
Object
  • Object
show all
Defined in:
lib/nirvanahq.rb,
lib/nirvanahq/auth.rb,
lib/nirvanahq/task.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ NirvanaHQ

Returns a new instance of NirvanaHQ.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nirvanahq/auth.rb', line 8

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

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/nirvanahq/auth.rb', line 4

def password
  @password
end

#usernameObject

Returns the value of attribute username.



3
4
5
# File 'lib/nirvanahq/auth.rb', line 3

def username
  @username
end

Instance Method Details

#add(task_details) ⇒ Object

task_details a hash with the task info



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nirvanahq/task.rb', line 16

def add task_details

  #prepare payload
  now = Time.now.to_i    
  defaults = {
    "method" => "task.save",
    "id" => UUID.generate,
    "type" => 0,
    "_type" => now,
    "state" => 0,
    "_state" => now,
  }

  task = defaults.merge(task_details)
  self.post [task].to_json
end

#delete(task_id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nirvanahq/task.rb', line 45

def delete task_id

  task = {
    "method" => "task.save",
    "id" => task_id,
    "deleted" => Time.now.to_i,
    "_deleted" => Time.now.to_i
  }

  self.post [task].to_json
end

#everythingObject

final form tbd. For now, have it parse out the results of the request, and send that along as a hash obj



10
11
12
13
# File 'lib/nirvanahq/task.rb', line 10

def everything
  raw = JSON.parse(`curl -sX GET 'https://api.nirvanahq.com/?api=rest&appid=gem&authtoken=#{@token}&method=everything&since=0'`)
  raw['results']
end

#fetch_tokenObject



32
33
34
35
36
37
# File 'lib/nirvanahq/auth.rb', line 32

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

#post(payload) ⇒ Object



5
6
7
# File 'lib/nirvanahq/task.rb', line 5

def post payload
  result = `curl -sX POST 'https://api.nirvanahq.com/?api=json&appid=gem&authtoken=#{@token}' -d '#{payload}'`  
end

#save_token!(token) ⇒ Object



39
40
41
42
43
# File 'lib/nirvanahq/auth.rb', line 39

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

#trash(task_id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nirvanahq/task.rb', line 33

def trash task_id

  task = {
    "method" => "task.save",
    "id" => task_id,
    "state" => 6,
    "_state" => Time.now.to_i
  }

  self.post [task].to_json
end

#undelete(task_id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nirvanahq/task.rb', line 57

def undelete task_id

  task = {
    "method" => "task.save",
    "id" => task_id,
    "deleted" => 0,
    "_deleted" => Time.now.to_i
  }

  self.post [task].to_json
end