Class: ChinoRuby::ChinoBaseAPI
- Inherits:
-
CheckValues
- Object
- CheckValues
- ChinoRuby::ChinoBaseAPI
- Defined in:
- lib/chino_ruby/classes.rb
Overview
Base class of every resource class. It contains the functions for the GET, POST, PUT, PATCH and DELETE requests
Direct Known Subclasses
Applications, Auth, Blobs, Collections, Documents, Groups, Permissions, Repositories, Schemas, Search, UserSchemas, Users
Instance Method Summary collapse
-
#delete_resource(path, force) ⇒ Object
base function to DELETE a resource.
-
#get_resource(path, limit = nil, offset = nil, full_document = nil) ⇒ Object
base function to GET a resource with the proper params if specified.
-
#initialize(customer_id, customer_key, host_url) ⇒ ChinoBaseAPI
constructor
Used to inizialize a customer or a user.
-
#parse_response(response, raw = false) ⇒ Object
base function to parse the response and raise “chino” errors if problems occurred.
-
#patch_resource(path, data) ⇒ Object
base function to PATCH a resource.
-
#post_resource(path, data = nil, limit = nil, offset = nil, full_document = nil) ⇒ Object
base function to POST a resource with the proper params if specified.
-
#post_resource_with_string_result(path, data) ⇒ Object
base function to POST a resource with string result.
-
#put_resource(path, data) ⇒ Object
base function to PUT a resource.
-
#return_uri(path, limit = nil, offset = nil, full_document = nil) ⇒ Object
returns the uri with the proper params if specified.
Methods inherited from CheckValues
#check_boolean, #check_int, #check_json, #check_string
Constructor Details
#initialize(customer_id, customer_key, host_url) ⇒ ChinoBaseAPI
Used to inizialize a customer or a user. If you want to authenticate a user, simply pass nil as the customer_id
62 63 64 65 66 |
# File 'lib/chino_ruby/classes.rb', line 62 def initialize(customer_id, customer_key, host_url) @customer_id = customer_id.blank? ? "Bearer " : customer_id @customer_key = customer_key @host_url = host_url end |
Instance Method Details
#delete_resource(path, force) ⇒ Object
base function to DELETE a resource
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/chino_ruby/classes.rb', line 186 def delete_resource(path, force) check_string(path) check_boolean(force) if force uri = return_uri(path+"?force=true") else uri = return_uri(path) end req = Net::HTTP::Delete.new(uri) if @customer_id == "Bearer " req.add_field("Authorization", @customer_id+@customer_key) else req.basic_auth @customer_id, @customer_key end res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http| http.request(req) } JSON.parse(parse_response(res).to_json)['result'] end |
#get_resource(path, limit = nil, offset = nil, full_document = nil) ⇒ Object
base function to GET a resource with the proper params if specified
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/chino_ruby/classes.rb', line 84 def get_resource(path, limit=nil, offset=nil, full_document=nil) check_string(path) if (limit==nil) && (offset==nil) uri = return_uri(path) elsif full_document==nil uri = return_uri(path, limit, offset) else uri = return_uri(path, limit, offset, full_document) end req = Net::HTTP::Get.new(uri) if @customer_id == "Bearer " req.add_field("Authorization", @customer_id+@customer_key) else req.basic_auth @customer_id, @customer_key end res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http| http.request(req) } parse_response(res)['data'] end |
#parse_response(response, raw = false) ⇒ Object
base function to parse the response and raise “chino” errors if problems occurred
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/chino_ruby/classes.rb', line 207 def parse_response(response, raw=false) if response.is_a?(Net::HTTPServerError) raise ChinoError.new("Chino Server Error: #{response} - #{response.body}", response) elsif response.is_a?(Net::HTTPUnauthorized) d = JSON.parse(response.body) raise ChinoAuthError.new("Chino authentication error: #{d['message']}", response) elsif !response.is_a?(Net::HTTPSuccess) begin d = JSON.parse(response.body) rescue raise ChinoError.new("Chino Server Error: body=#{response.body}", response) end if d['user_error'] and d['error'] raise ChinoError.new(d['error'], response, d['user_error']) #user_error is translated elsif d['message'] == "Invalid credentials given." raise ChinoAuthError.new(response.body, response) elsif d['error'] raise ChinoError.new(d['error'], response) else raise ChinoError.new(response.body, response) end end return response.body if raw begin return JSON.parse(response.body) rescue JSON::ParserError raise ChinoError.new("Unable to parse JSON response: #{response.body}", response) end end |
#patch_resource(path, data) ⇒ Object
base function to PATCH a resource
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/chino_ruby/classes.rb', line 169 def patch_resource(path, data) check_string(path) uri = return_uri(path) req = Net::HTTP::Patch.new(uri.path) if @customer_id == "Bearer " req.add_field("Authorization", @customer_id+@customer_key) else req.basic_auth @customer_id, @customer_key end req.body = data res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http| http.request(req) } parse_response(res)['data'] end |
#post_resource(path, data = nil, limit = nil, offset = nil, full_document = nil) ⇒ Object
base function to POST a resource with the proper params if specified
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/chino_ruby/classes.rb', line 106 def post_resource(path, data=nil, limit=nil, offset=nil, full_document=nil) check_string(path) if (limit==nil) && (offset==nil) uri = return_uri(path) elsif full_document==nil uri = return_uri(path, limit, offset) else uri = return_uri(path, limit, offset, full_document) end req = Net::HTTP::Post.new(uri) if @customer_id == "Bearer " req.add_field("Authorization", @customer_id+@customer_key) else req.basic_auth @customer_id, @customer_key end if data!=nil req.body = data end res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http| http.request(req) } if data!=nil parse_response(res)['data'] else JSON.parse(parse_response(res).to_json)['result'] end end |
#post_resource_with_string_result(path, data) ⇒ Object
base function to POST a resource with string result
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/chino_ruby/classes.rb', line 135 def post_resource_with_string_result(path, data) check_string(path) uri = return_uri(path) req = Net::HTTP::Post.new(uri.path) if @customer_id == "Bearer " req.add_field("Authorization", @customer_id+@customer_key) else req.basic_auth @customer_id, @customer_key end req.body = data res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http| http.request(req) } JSON.parse(parse_response(res).to_json)['result'] end |
#put_resource(path, data) ⇒ Object
base function to PUT a resource
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/chino_ruby/classes.rb', line 152 def put_resource(path, data) check_string(path) uri = return_uri(path) req = Net::HTTP::Put.new(uri.path) if @customer_id == "Bearer " req.add_field("Authorization", @customer_id+@customer_key) else req.basic_auth @customer_id, @customer_key end req.body = data res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http| http.request(req) } parse_response(res)['data'] end |
#return_uri(path, limit = nil, offset = nil, full_document = nil) ⇒ Object
returns the uri with the proper params if specified
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/chino_ruby/classes.rb', line 69 def return_uri(path, limit=nil, offset=nil, full_document=nil) uri = URI(@host_url+path) if limit!=nil && offset!=nil if full_document!=nil params = { :"full_document" => true, :"limit" => limit, :"offset" => offset} uri.query = URI.encode_www_form(params) else params = { "limit" => limit, :"offset" => offset} uri.query = URI.encode_www_form(params) end end uri end |