Class: Rescuegroups::Client
- Inherits:
-
Object
- Object
- Rescuegroups::Client
- Defined in:
- lib/rescuegroups/client.rb
Constant Summary collapse
- OPERATIONS =
{ :eq => "equal", :ne => "notequal", :lt => "lessthan", :lte => "lessthanorequal", :gt => "greaterthan", :gte => "greaterthanorequal", :contains => "contains", :notcontains => "notcontains", :blank => "blank", :notblank => "notblank", :radius => "radius"}
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#object_action ⇒ Object
Returns the value of attribute object_action.
-
#object_type ⇒ Object
Returns the value of attribute object_type.
-
#search ⇒ Object
Returns the value of attribute search.
-
#token ⇒ Object
Returns the value of attribute token.
-
#token_hash ⇒ Object
Returns the value of attribute token_hash.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #camelize(snake_hash) ⇒ Object
- #clear_search_fields ⇒ Object
- #clear_search_filters ⇒ Object
- #clear_value_fields ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #logged_in? ⇒ Boolean
- #login(options = {}) ⇒ Object
- #query ⇒ Object
- #query_params ⇒ Object
- #search_params ⇒ Object
- #set_search_fields(fields) ⇒ Object
- #set_search_filter(field_name, operation, criteria) ⇒ Object
- #set_value_fields(fields) ⇒ Object
- #value_params ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 |
# File 'lib/rescuegroups/client.rb', line 18 def initialize(={}) self.api_key = [:api_key] || Rescuegroups.api_key self.object_type = [:object_type] self.object_action = [:object_action] self.search = search_params self.values = value_params raise ArgumentError, "Please provide object_type and object_action" if (self.object_type == nil or self.object_action == nil) end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
16 17 18 |
# File 'lib/rescuegroups/client.rb', line 16 def api_key @api_key end |
#fields ⇒ Object
Returns the value of attribute fields.
16 17 18 |
# File 'lib/rescuegroups/client.rb', line 16 def fields @fields end |
#object_action ⇒ Object
Returns the value of attribute object_action.
16 17 18 |
# File 'lib/rescuegroups/client.rb', line 16 def object_action @object_action end |
#object_type ⇒ Object
Returns the value of attribute object_type.
16 17 18 |
# File 'lib/rescuegroups/client.rb', line 16 def object_type @object_type end |
#search ⇒ Object
Returns the value of attribute search.
16 17 18 |
# File 'lib/rescuegroups/client.rb', line 16 def search @search end |
#token ⇒ Object
Returns the value of attribute token.
16 17 18 |
# File 'lib/rescuegroups/client.rb', line 16 def token @token end |
#token_hash ⇒ Object
Returns the value of attribute token_hash.
16 17 18 |
# File 'lib/rescuegroups/client.rb', line 16 def token_hash @token_hash end |
#values ⇒ Object
Returns the value of attribute values.
16 17 18 |
# File 'lib/rescuegroups/client.rb', line 16 def values @values end |
Instance Method Details
#camelize(snake_hash) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rescuegroups/client.rb', line 122 def camelize(snake_hash) camel_hash = {} snake_hash.each do |k,v| camelized_key = k.to_s.split("_").each_with_index do |part,i| next if i == 0 (part == "id")? part.upcase! : part.capitalize! end.join if v.class == Hash camel_hash[camelized_key] = camelize(v) else camel_hash[camelized_key] = v end end return camel_hash end |
#clear_search_fields ⇒ Object
67 68 69 |
# File 'lib/rescuegroups/client.rb', line 67 def clear_search_fields self.search["search"]["fields"] = [] end |
#clear_search_filters ⇒ Object
59 60 61 |
# File 'lib/rescuegroups/client.rb', line 59 def clear_search_filters self.search["search"]["filters"] = [] end |
#clear_value_fields ⇒ Object
75 76 77 |
# File 'lib/rescuegroups/client.rb', line 75 def clear_value_fields self.values["values"] = [] end |
#logged_in? ⇒ Boolean
47 48 49 |
# File 'lib/rescuegroups/client.rb', line 47 def logged_in? self.token && self.token_hash end |
#login(options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rescuegroups/client.rb', line 28 def login(={}) = { :username => [:username], :password => [:password], :account_number => [:account_number], :action => "login"} = camelize() response_object = Curl.post(Rescuegroups::Configuration::ENDPOINT, .to_json) response = JSON.parse(response_object.body_str) if response["status"] == "ok" self.token = response["data"]["token"] self.token_hash = response["data"]["tokenHash"] puts "Login successful. Token set as #{self.token} and token hash as #{self.token_hash}" return true else puts "Login unsuccessful." + response["messages"]["generalMessages"]["messageText"] return false end end |
#query ⇒ Object
79 80 81 82 |
# File 'lib/rescuegroups/client.rb', line 79 def query response_object = Curl.post(Rescuegroups::Configuration::ENDPOINT, query_params.to_json) @response = JSON.parse(response_object.body_str) end |
#query_params ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rescuegroups/client.rb', line 84 def query_params if logged_in? = {:token => self.token, :token_hash => self.token_hash, :object_type => self.object_type, :object_action => self.object_action} else = {:apikey => self.api_key, :object_type => self.object_type, :object_action => self.object_action} end if self.search["search"]["filters"].present? .merge!(self.search) elsif self.values["values"].present? .merge!(self.values) end = camelize() return end |
#search_params ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rescuegroups/client.rb', line 105 def search_params camelize({:search => { :result_start => 0, :result_limit => 10, :result_sort => "animalID", :result_order => "asc", :fields => [], :filters => [] } }) end |
#set_search_fields(fields) ⇒ Object
63 64 65 |
# File 'lib/rescuegroups/client.rb', line 63 def set_search_fields(fields) self.search["search"]["fields"] = fields end |
#set_search_filter(field_name, operation, criteria) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/rescuegroups/client.rb', line 51 def set_search_filter(field_name, operation, criteria) filter ={ :field_name => field_name.to_s.split("_").each_with_index {|part,i| part.capitalize! unless i == 0}.join, :operation => OPERATIONS[operation], :criteria => criteria.to_s} self.search["search"]["filters"] << camelize(filter) end |
#set_value_fields(fields) ⇒ Object
71 72 73 |
# File 'lib/rescuegroups/client.rb', line 71 def set_value_fields(fields) self.values["values"] << camelize(fields) end |
#value_params ⇒ Object
118 119 120 |
# File 'lib/rescuegroups/client.rb', line 118 def value_params camelize({:values =>[] }) end |