Class: Rescuegroups::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
# File 'lib/rescuegroups/client.rb', line 18

def initialize(options={})
  self.api_key = options[:api_key] || Rescuegroups.api_key
  self.object_type = options[:object_type]
  self.object_action = options[: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_keyObject

Returns the value of attribute api_key.



16
17
18
# File 'lib/rescuegroups/client.rb', line 16

def api_key
  @api_key
end

#fieldsObject

Returns the value of attribute fields.



16
17
18
# File 'lib/rescuegroups/client.rb', line 16

def fields
  @fields
end

#object_actionObject

Returns the value of attribute object_action.



16
17
18
# File 'lib/rescuegroups/client.rb', line 16

def object_action
  @object_action
end

#object_typeObject

Returns the value of attribute object_type.



16
17
18
# File 'lib/rescuegroups/client.rb', line 16

def object_type
  @object_type
end

#searchObject

Returns the value of attribute search.



16
17
18
# File 'lib/rescuegroups/client.rb', line 16

def search
  @search
end

#tokenObject

Returns the value of attribute token.



16
17
18
# File 'lib/rescuegroups/client.rb', line 16

def token
  @token
end

#token_hashObject

Returns the value of attribute token_hash.



16
17
18
# File 'lib/rescuegroups/client.rb', line 16

def token_hash
  @token_hash
end

#valuesObject

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_fieldsObject



67
68
69
# File 'lib/rescuegroups/client.rb', line 67

def clear_search_fields
  self.search["search"]["fields"] = []
end

#clear_search_filtersObject



59
60
61
# File 'lib/rescuegroups/client.rb', line 59

def clear_search_filters
  self.search["search"]["filters"] = []
end

#clear_value_fieldsObject



75
76
77
# File 'lib/rescuegroups/client.rb', line 75

def clear_value_fields
  self.values["values"] = []
end

#logged_in?Boolean

Returns:

  • (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 (options={})
  main_options = { :username => options[:username],
                   :password => options[:password],
                   :account_number => options[:account_number],
                   :action => "login"}
  main_options = camelize(main_options)
  response_object = Curl.post(Rescuegroups::Configuration::ENDPOINT, main_options.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

#queryObject



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_paramsObject



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?
    main_options = {:token => self.token,
                    :token_hash => self.token_hash, 
                    :object_type => self.object_type,
                    :object_action => self.object_action}
  else
    main_options = {:apikey => self.api_key, 
                    :object_type => self.object_type,
                    :object_action => self.object_action}
  end

  if self.search["search"]["filters"].present?
    main_options.merge!(self.search)
  elsif self.values["values"].present?
    main_options.merge!(self.values)
  end
  main_options = camelize(main_options)
  return main_options
end

#search_paramsObject



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_paramsObject



118
119
120
# File 'lib/rescuegroups/client.rb', line 118

def value_params
  camelize({:values =>[] })
end