class ChinoRuby::ChinoBaseAPI
Base class of every resource class. It contains the functions for the GET, POST, PUT, PATCH and DELETE requests
Public Class Methods
new(customer_id, customer_key, host_url)
click to toggle source
Used to inizialize a customer or a user. If you want to authenticate a user, simply pass nil as the customer_id
# 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
Public Instance Methods
delete_resource(path, force)
click to toggle source
base function to DELETE a resource
# 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)
click to toggle source
base function to GET a resource with the proper params if specified
# 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)
click to toggle source
base function to parse the response and raise “chino” errors if problems occurred
# 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)
click to toggle source
base function to PATCH a resource
# 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)
click to toggle source
base function to POST a resource with the proper params if specified
# 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)
click to toggle source
base function to POST a resource with string result
# 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)
click to toggle source
base function to PUT a resource
# 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)
click to toggle source
returns the uri with the proper params if specified
# 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