Class: Infobase::Base
- Inherits:
-
Object
- Object
- Infobase::Base
- Defined in:
- lib/infobase/base.rb
Direct Known Subclasses
Activity, Ministry, Person, Region, Statistic, Subministry, TargetArea, Team, User
Class Method Summary collapse
- .default_path ⇒ Object
- .delete(id) ⇒ Object
- .find(id, params = {}) ⇒ Object
- .get(params = {}) ⇒ Object
- .handle_response(response, request, result) ⇒ Object
- .path_with_id(id) ⇒ Object
- .plural_name ⇒ Object
- .post(params = {}) ⇒ Object
- .put(id, params = {}) ⇒ Object
- .request(method, params, path = nil) ⇒ Object
- .search(params = {}) ⇒ Object
- .singular_name ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(hash) ⇒ Base
constructor
A new instance of Base.
- #keys ⇒ Object
- #method_missing(symbol, &block) ⇒ Object
- #respond_to?(symbol, include_private = false) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(hash) ⇒ Base
Returns a new instance of Base.
8 9 10 |
# File 'lib/infobase/base.rb', line 8 def initialize(hash) @item = hash end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, &block) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/infobase/base.rb', line 24 def method_missing(symbol, &block) key = symbol.to_s return @item[key] if @item.keys.include?(key) super end |
Class Method Details
.default_path ⇒ Object
113 114 115 |
# File 'lib/infobase/base.rb', line 113 def self.default_path plural_name end |
.delete(id) ⇒ Object
60 61 62 |
# File 'lib/infobase/base.rb', line 60 def self.delete(id) request(:delete, {}, path_with_id(id)) end |
.find(id, params = {}) ⇒ Object
36 37 38 |
# File 'lib/infobase/base.rb', line 36 def self.find(id, params = {}) request(:get, params, path_with_id(id)) end |
.get(params = {}) ⇒ Object
48 49 50 |
# File 'lib/infobase/base.rb', line 48 def self.get(params = {}) request(:get, params) end |
.handle_response(response, request, result) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/infobase/base.rb', line 92 def self.handle_response(response, request, result) case response.code when 200..299 json = Oj.load(response) if json[singular_name] new(json[singular_name]) else json[plural_name] = json[plural_name].collect { |hash| new(hash) } json end when 400 raise RestClient::BadRequest, response when 500 raise RestClient::InternalServerError, response else puts response.inspect puts request.inspect raise result.inspect end end |
.path_with_id(id) ⇒ Object
117 118 119 |
# File 'lib/infobase/base.rb', line 117 def self.path_with_id(id) "#{default_path}/#{id}" end |
.plural_name ⇒ Object
44 45 46 |
# File 'lib/infobase/base.rb', line 44 def self.plural_name singular_name.pluralize end |
.post(params = {}) ⇒ Object
52 53 54 |
# File 'lib/infobase/base.rb', line 52 def self.post(params = {}) request(:post, params) end |
.put(id, params = {}) ⇒ Object
56 57 58 |
# File 'lib/infobase/base.rb', line 56 def self.put(id, params = {}) request(:put, params, path_with_id(id)) end |
.request(method, params, path = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/infobase/base.rb', line 68 def self.request(method, params, path = nil) raise 'You need to configure Infobase with your access_token.' unless Infobase.access_token path ||= default_path url = Infobase.base_url url += '/' unless url.last == '/' url += path case method when :post RestClient.post(url, params, authorization: "Bearer #{Infobase.access_token}", :timeout => -1) { |response, request, result, &block| handle_response(response, request, result) } when :put RestClient.put(url, params, authorization: "Bearer #{Infobase.access_token}", :timeout => -1) { |response, request, result, &block| handle_response(response, request, result) } else RestClient::Request.execute(:method => method, :url => url, :headers => {params: params, authorization: "Bearer #{Infobase.access_token}"}, :timeout => -1) { |response, request, result, &block| handle_response(response, request, result) } end end |
.search(params = {}) ⇒ Object
64 65 66 |
# File 'lib/infobase/base.rb', line 64 def self.search(params = {}) request(:post, params, "#{default_path}/search") end |
.singular_name ⇒ Object
40 41 42 |
# File 'lib/infobase/base.rb', line 40 def self.singular_name to_s.split('::').last.underscore end |
Instance Method Details
#[](key) ⇒ Object
12 13 14 |
# File 'lib/infobase/base.rb', line 12 def [](key) @item[key.to_s] end |
#keys ⇒ Object
16 17 18 |
# File 'lib/infobase/base.rb', line 16 def keys @item.keys end |
#respond_to?(symbol, include_private = false) ⇒ Boolean
32 33 34 |
# File 'lib/infobase/base.rb', line 32 def respond_to?(symbol, include_private = false) @item.keys.include?(symbol.to_s) || super end |
#to_s ⇒ Object
20 21 22 |
# File 'lib/infobase/base.rb', line 20 def to_s @item['to_s'] || super end |