rescuegroups 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rescuegroups.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 bornfree
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ This is Ruby wrapper for Rescuegroups API
2
+ http://support.rescuegroups.org:8091/display/userguide/HTTP+API
3
+
4
+ For now, as gem is not yet complete, please clone/fork the repo
5
+ and bundle using the :path param in your Gemfile.
6
+
7
+ ## Usage
8
+
9
+ First setup your api key
10
+ ```ruby
11
+ Rescuegroups.configure do |config|
12
+ config.api_key = 'ABCD'
13
+ end
14
+ ```
15
+ Then create a client anywhere with object_type and object_action attributes.
16
+ Your snake_case symbols are automatically camelcased within.
17
+ ```ruby
18
+ client = Rescuegroups::Client.new :object_type => "animals", :object_action => "publicSearch"
19
+ ```
20
+
21
+ If you wish to perform elevated actions like add or edit animal you will have to login.
22
+ ```ruby
23
+ client.login(:username => "john", :password => "doe", :user_account => "0")
24
+ ```
25
+ This will attach token and tokenHash to further requests
26
+
27
+ ### Search procedure
28
+
29
+ Add filters. For example to search for an animal with animalID as 1234
30
+ ```ruby
31
+ client.set_search_filter 'animalID', :eq, "1234"
32
+ client.set_search_filter 'animalStatus', :eq, "Available" # Not required but adviced
33
+ ```
34
+
35
+ To clear all filters do
36
+ ```ruby
37
+ client.clear_search_filters
38
+ ```
39
+
40
+ Specify the fields you need as an array
41
+ ```ruby
42
+ client.set_search_fields ['animalName', 'animalID', 'animalBreed']
43
+ ```
44
+
45
+ To clear all fields do
46
+ ```ruby
47
+ client.clear_search_fields
48
+ ```
49
+
50
+ Then, run the query and get the json
51
+ ```ruby
52
+ response = client.query
53
+ ```
54
+
55
+ ### Add/Edit animal procedure
56
+ To add animal species(dog/cat/bird) and primary breed are compulsory
57
+ First create a client and login
58
+ ```ruby
59
+ client = Rescuegroups::Client.new :object_type => "animals", :object_action => "add" # or "edit"
60
+ client.login(:username => "john", :password => "doe", :user_account => "0")
61
+ ```
62
+
63
+ Then set the values of fields(snake_cased)
64
+ ```ruby
65
+ client.set_value_fields(:animal_name => "Pepsi", :animal_species_id => "Dog", :animal_primary_breed_id => "123")
66
+ ```
67
+
68
+ Send a query as usual and watch for response
69
+ ```ruby
70
+ response = client.query
71
+ ```
72
+
73
+
74
+ For available object type and object actions refer to
75
+ http://support.rescuegroups.org:8091/display/userguide/HTTP+API+object+definitions
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.ruby_opts << "-rubygems"
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in build.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 bornfree
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Build
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'build'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install build
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'build/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "build"
8
+ spec.version = Build::VERSION
9
+ spec.authors = ["bornfree"]
10
+ spec.email = ["harsha.xg@gmail.com"]
11
+ spec.description = %q{TODO: Write a gem description}
12
+ spec.summary = %q{TODO: Write a gem summary}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,5 @@
1
+ require "build/version"
2
+
3
+ module Build
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module Build
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "json"
2
+ require "curb"
3
+ require "rescuegroups/version"
4
+ require "rescuegroups/configuration"
5
+ require "rescuegroups/client"
6
+
7
+ module Rescuegroups
8
+ extend Configuration
9
+ end
10
+
@@ -0,0 +1,139 @@
1
+ module Rescuegroups
2
+ class Client
3
+
4
+ OPERATIONS = { :eq => "equal",
5
+ :ne => "notequal",
6
+ :lt => "lessthan",
7
+ :lte => "lessthanorequal",
8
+ :gt => "greaterthan",
9
+ :gte => "greaterthanorequal",
10
+ :contains => "contains",
11
+ :notcontains => "notcontains",
12
+ :blank => "blank",
13
+ :notblank => "notblank",
14
+ :radius => "radius"}
15
+
16
+ attr_accessor :api_key, :object_type, :object_action, :search, :fields, :token, :token_hash, :values
17
+
18
+ def initialize(options={})
19
+ self.api_key = options[:api_key] || Rescuegroups.api_key
20
+ self.object_type = options[:object_type]
21
+ self.object_action = options[:object_action]
22
+ self.search = search_params
23
+ self.values = value_params
24
+
25
+ raise ArgumentError, "Please provide object_type and object_action" if (self.object_type == nil or self.object_action == nil)
26
+ end
27
+
28
+ def login(options={})
29
+ main_options = { :username => options[:username],
30
+ :password => options[:password],
31
+ :account_number => options[:account_number],
32
+ :action => "login"}
33
+ main_options = camelize(main_options)
34
+ response_object = Curl.post(Rescuegroups::Configuration::ENDPOINT, main_options.to_json)
35
+ response = JSON.parse(response_object.body_str)
36
+ if response["status"] == "ok"
37
+ self.token = response["data"]["token"]
38
+ self.token_hash = response["data"]["tokenHash"]
39
+ puts "Login successful. Token set as #{self.token} and token hash as #{self.token_hash}"
40
+ return true
41
+ else
42
+ puts "Login unsuccessful." + response["messages"]["generalMessages"]["messageText"]
43
+ return false
44
+ end
45
+ end
46
+
47
+ def logged_in?
48
+ self.token && self.token_hash
49
+ end
50
+
51
+ def set_search_filter(field_name, operation, criteria)
52
+
53
+ filter ={ :field_name => field_name.to_s.split("_").each_with_index {|part,i| part.capitalize! unless i == 0}.join,
54
+ :operation => OPERATIONS[operation],
55
+ :criteria => criteria.to_s}
56
+ self.search["search"]["filters"] << camelize(filter)
57
+ end
58
+
59
+ def clear_search_filters
60
+ self.search["search"]["filters"] = []
61
+ end
62
+
63
+ def set_search_fields(fields)
64
+ self.search["search"]["fields"] = fields
65
+ end
66
+
67
+ def clear_search_fields
68
+ self.search["search"]["fields"] = []
69
+ end
70
+
71
+ def set_value_fields(fields)
72
+ self.values["values"] << camelize(fields)
73
+ end
74
+
75
+ def clear_value_fields
76
+ self.values["values"] = []
77
+ end
78
+
79
+ def query
80
+ response_object = Curl.post(Rescuegroups::Configuration::ENDPOINT, query_params.to_json)
81
+ @response = JSON.parse(response_object.body_str)
82
+ end
83
+
84
+ def query_params
85
+ if logged_in?
86
+ main_options = {:token => self.token,
87
+ :token_hash => self.token_hash,
88
+ :object_type => self.object_type,
89
+ :object_action => self.object_action}
90
+ else
91
+ main_options = {:apikey => self.api_key,
92
+ :object_type => self.object_type,
93
+ :object_action => self.object_action}
94
+ end
95
+
96
+ if self.search["search"]["filters"].present?
97
+ main_options.merge!(self.search)
98
+ elsif self.values["values"].present?
99
+ main_options.merge!(self.values)
100
+ end
101
+ main_options = camelize(main_options)
102
+ return main_options
103
+ end
104
+
105
+ def search_params
106
+ camelize({:search =>
107
+ {
108
+ :result_start => 0,
109
+ :result_limit => 10,
110
+ :result_sort => "animalID",
111
+ :result_order => "asc",
112
+ :fields => [],
113
+ :filters => []
114
+ }
115
+ })
116
+ end
117
+
118
+ def value_params
119
+ camelize({:values =>[] })
120
+ end
121
+
122
+ def camelize(snake_hash)
123
+ camel_hash = {}
124
+ snake_hash.each do |k,v|
125
+ camelized_key = k.to_s.split("_").each_with_index do |part,i|
126
+ next if i == 0
127
+ (part == "id")? part.upcase! : part.capitalize!
128
+ end.join
129
+ if v.class == Hash
130
+ camel_hash[camelized_key] = camelize(v)
131
+ else
132
+ camel_hash[camelized_key] = v
133
+ end
134
+ end
135
+ return camel_hash
136
+ end
137
+
138
+ end
139
+ end
@@ -0,0 +1,23 @@
1
+ module Rescuegroups
2
+ module Configuration
3
+
4
+ ENDPOINT = 'https://api.rescuegroups.org/http/json'
5
+ API_KEY = nil
6
+ CONTENT_TYPE = "application/json"
7
+
8
+ attr_accessor :api_key
9
+
10
+ def self.extended(base)
11
+ base.reset
12
+ end
13
+
14
+ def reset
15
+ self.api_key = API_KEY
16
+ end
17
+
18
+ def configure
19
+ yield self
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Rescuegroups
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rescuegroups/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rescuegroups"
8
+ spec.version = Rescuegroups::VERSION
9
+ spec.authors = ["bornfree"]
10
+ spec.email = ["harsha.xg@gmail.com"]
11
+ spec.description = "Ruby wrapper for Rescuegroups API"
12
+ spec.summary = "Ruby wrapper for talking to the Rescuegroups API"
13
+ spec.homepage = "https://github.com/bornfree/rescuegroups"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "curb"
24
+ spec.add_development_dependency "json"
25
+ spec.add_development_dependency "pry-rails"
26
+ end
@@ -0,0 +1,3 @@
1
+ require 'rescuegroups'
2
+ require 'minitest/spec'
3
+ require 'minitest/autorun'
@@ -0,0 +1,28 @@
1
+ require 'helper'
2
+
3
+ describe 'client' do
4
+
5
+ describe '.api_key' do
6
+ it 'should be able to set api key when provided' do
7
+ client = Rescuegroups::Client.new :api_key => "123", :object_type => "animals", :object_action => "define"
8
+ client.api_key.must_equal "123"
9
+ end
10
+
11
+ it 'should be able to revert to default when api key when created' do
12
+ client = Rescuegroups::Client.new :object_type => "animals", :object_action => "define"
13
+ client.api_key.must_equal Rescuegroups::Configuration::API_KEY
14
+ end
15
+
16
+ it 'should have all three values of apikey, objecttype and objectaction' do
17
+ client = Rescuegroups::Client.new :object_type => "animals", :object_action => "define"
18
+ end
19
+
20
+ it 'should run a query and return something' do
21
+ client = Rescuegroups::Client.new :object_type => "animals", :object_action => "define"
22
+ client.filter :age, :lt, 5
23
+ client.query != nil
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'helper'
2
+
3
+ describe 'configuration' do
4
+
5
+ describe '.api_key' do
6
+ it 'should have an api key' do
7
+ Rescuegroups.api_key.must_equal Rescuegroups::Configuration::API_KEY
8
+ end
9
+ end
10
+
11
+ after do
12
+ Rescuegroups.reset
13
+ end
14
+
15
+ describe '.configure' do
16
+ it 'should configure api_key' do
17
+ Rescuegroups.configure do |config|
18
+ config.send("api_key=", "123")
19
+ Rescuegroups.send("api_key").must_equal "123"
20
+ end
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ describe Rescuegroups do
4
+ it 'should have a version' do
5
+ Rescuegroups::VERSION.wont_be_nil
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rescuegroups
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - bornfree
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ prerelease: false
16
+ type: :development
17
+ name: bundler
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: '1.3'
24
+ requirement: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ prerelease: false
32
+ type: :development
33
+ name: rake
34
+ version_requirements: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirement: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ prerelease: false
48
+ type: :development
49
+ name: curb
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirement: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ prerelease: false
64
+ type: :development
65
+ name: json
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirement: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ prerelease: false
80
+ type: :development
81
+ name: pry-rails
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirement: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Ruby wrapper for Rescuegroups API
95
+ email:
96
+ - harsha.xg@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - build/.gitignore
107
+ - build/Gemfile
108
+ - build/LICENSE.txt
109
+ - build/README.md
110
+ - build/Rakefile
111
+ - build/build.gemspec
112
+ - build/lib/build.rb
113
+ - build/lib/build/version.rb
114
+ - lib/rescuegroups.rb
115
+ - lib/rescuegroups/client.rb
116
+ - lib/rescuegroups/configuration.rb
117
+ - lib/rescuegroups/version.rb
118
+ - rescuegroups.gemspec
119
+ - test/helper.rb
120
+ - test/rescuegroups/client_test.rb
121
+ - test/rescuegroups/configuration_test.rb
122
+ - test/rescuegroups/rescuegroups_test.rb
123
+ homepage: https://github.com/bornfree/rescuegroups
124
+ licenses:
125
+ - MIT
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 1.8.25
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Ruby wrapper for talking to the Rescuegroups API
148
+ test_files:
149
+ - test/helper.rb
150
+ - test/rescuegroups/client_test.rb
151
+ - test/rescuegroups/configuration_test.rb
152
+ - test/rescuegroups/rescuegroups_test.rb