Module: Basilik::Action

Includes:
Faults
Included in:
Load
Defined in:
lib/basilik/action.rb

Instance Method Summary collapse

Instance Method Details

#dec(field) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/basilik/action.rb', line 38

def dec( field )
  map = read
  current_val = map[field]
  raise NonNumericFieldError, "The field #{field} does not have a numeric value" if current_val and !current_val.is_a? Numeric
  if current_val
    new_val = map[field] - 1        
  else
    new_val = 0
  end
  update( field => new_val )
end

#get_priorityObject



79
80
81
82
83
84
85
86
# File 'lib/basilik/action.rb', line 79

def get_priority
  location = json_url( true )      
  resp = Typhoeus.get( location, gen_opts( params: { format: :export} ) )
  res = handle_response( resp, location )
  res.to_i
rescue NoDataError
  nil
end

#get_rulesObject



88
89
90
91
92
# File 'lib/basilik/action.rb', line 88

def get_rules
  location = rules_url
  resp     = Typhoeus.get( location, gen_opts )
  handle_response( resp, location )       
end

#inc(field) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/basilik/action.rb', line 26

def inc( field )
  map = read
  current_val = map[field]
  raise NonNumericFieldError, "The field #{field} does not have a numeric value" if current_val and !current_val.is_a? Numeric
  if current_val
    new_val = map[field] + 1        
  else 
    new_val = 0
  end
  update( field => new_val )
end

#push(data = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/basilik/action.rb', line 58

def push( data=nil )
  location = json_url
  opts     = {}      
  if data
    opts[:body] = data.to_json
  else
    opts[:body] = ''.to_json
  end
  resp = Typhoeus.post( location, gen_opts( opts ) )
  res = handle_response( resp, location )
  Basilik::Load.new( uri.to_s + '/' + res.name )
end

#readObject



7
8
9
10
11
12
# File 'lib/basilik/action.rb', line 7

def read
  location = json_url
  resp = Typhoeus.get( location, gen_opts( params: {format: :export} ) )
  res  = handle_response( resp, location )
  res.is_a?(Map) ? Snapshot.new( res ).to_map : res.is_a?(String) ? res.to_val : res
end

#removeObject



50
51
52
53
54
55
56
# File 'lib/basilik/action.rb', line 50

def remove
  location = json_url
  resp = Typhoeus.delete( location, gen_opts )
  unless resp.success?
    raise InvalidRequestError, "<#{resp.return_code}> Unable to perform request #{location}"
  end
end

#set(data) ⇒ Object



14
15
16
17
18
# File 'lib/basilik/action.rb', line 14

def set( data )
  location = json_url
  resp = Typhoeus.put( location, gen_opts( body: data.to_json ) )
  handle_response( resp, location )
end

#set_priority(priority) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/basilik/action.rb', line 71

def set_priority( priority )
  location = json_url( true )
  resp = Typhoeus.put( location, gen_opts( body: priority.to_json ) )
  unless resp.success? or resp.body
    raise InvalidRequestError, "<#{resp.return_code}> Unable to perform request #{location}"
  end
end

#set_rules(rules) ⇒ Object



94
95
96
97
98
# File 'lib/basilik/action.rb', line 94

def set_rules( rules )
  location = rules_url
  resp     = Typhoeus.put( location, gen_opts( body: {:rules => rules}.to_json ) )
  handle_response( resp, location )
end

#update(data) ⇒ Object



20
21
22
23
24
# File 'lib/basilik/action.rb', line 20

def update( data )
  location = json_url
  resp = Typhoeus.patch( location, gen_opts( body: data.to_json ) )
  handle_response( resp, location )
end