Module: Bigbertha::Action
Instance Method Summary collapse
- #dec(field) ⇒ Object
- #get_priority ⇒ Object
- #get_rules ⇒ Object
- #inc(field) ⇒ Object
- #push(data = nil) ⇒ Object
- #read ⇒ Object (also: #val)
- #remove ⇒ Object
- #set(data) ⇒ Object
- #set_priority(priority) ⇒ Object
- #set_rules(rules) ⇒ Object
- #update(data) ⇒ Object
Instance Method Details
#dec(field) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bigbertha/action.rb', line 39 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_priority ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/bigbertha/action.rb', line 80 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_rules ⇒ Object
89 90 91 92 93 |
# File 'lib/bigbertha/action.rb', line 89 def get_rules location = rules_url resp = Typhoeus.get( location, gen_opts ) handle_response( resp, location ) end |
#inc(field) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/bigbertha/action.rb', line 27 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
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/bigbertha/action.rb', line 59 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 ) Bigbertha::Ref.new( uri.to_s + '/' + res.name ) end |
#read ⇒ Object Also known as: val
7 8 9 10 11 12 |
# File 'lib/bigbertha/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 |
#remove ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/bigbertha/action.rb', line 51 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
15 16 17 18 19 |
# File 'lib/bigbertha/action.rb', line 15 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
72 73 74 75 76 77 78 |
# File 'lib/bigbertha/action.rb', line 72 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
95 96 97 98 99 |
# File 'lib/bigbertha/action.rb', line 95 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
21 22 23 24 25 |
# File 'lib/bigbertha/action.rb', line 21 def update( data ) location = json_url resp = Typhoeus.patch( location, gen_opts( body: data.to_json ) ) handle_response( resp, location ) end |