Module: ClioClient::Http
- Included in:
- Session
- Defined in:
- lib/clio_client/http.rb
Instance Method Summary collapse
- #base_uri(path, params = {}) ⇒ Object
- #delete(path, params = {}, parse = true) ⇒ Object
- #get(path, params = {}, parse = true) ⇒ Object
- #make_api_request(req, uri, parse = true) ⇒ Object
- #make_request(req, uri, parse = true) ⇒ Object
- #multipart_post(path, params, parse = true) ⇒ Object
- #parse_response(res, parse) ⇒ Object
- #post(path, body = "", parse = true) ⇒ Object
- #put(path, body = "", parse = true) ⇒ Object
Instance Method Details
#base_uri(path, params = {}) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/clio_client/http.rb', line 11 def base_uri(path, params = {}) uri = URI.parse(self.base_scope_url) uri.path = path uri.query = URI.encode_www_form(params) if params.any? uri end |
#delete(path, params = {}, parse = true) ⇒ Object
46 47 48 49 50 |
# File 'lib/clio_client/http.rb', line 46 def delete(path, params = {}, parse=true) uri = base_uri("#{api_prefix}/#{path}", params) req = Net::HTTP::Delete.new(uri.request_uri) make_api_request(req, uri, parse) end |
#get(path, params = {}, parse = true) ⇒ Object
18 19 20 21 22 |
# File 'lib/clio_client/http.rb', line 18 def get(path, params ={}, parse=true) uri = base_uri("#{api_prefix}/#{path}", params) req = Net::HTTP::Get.new(uri.request_uri) make_api_request(req, uri, parse) end |
#make_api_request(req, uri, parse = true) ⇒ Object
52 53 54 55 56 |
# File 'lib/clio_client/http.rb', line 52 def make_api_request(req, uri, parse = true) raise ClioClient::Unauthorized if self.access_token.nil? || self.access_token.empty? req.add_field("Authorization", "Bearer #{self.access_token}") make_request(req, uri, parse) end |
#make_request(req, uri, parse = true) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/clio_client/http.rb', line 58 def make_request(req, uri, parse = true) req.add_field("Accept", "text/json") n = Net::HTTP.new(uri.host, uri.port) n.use_ssl = uri.scheme == 'https' res = n.start do |http| http.request(req) end parse_response(res, parse) end |
#multipart_post(path, params, parse = true) ⇒ Object
32 33 34 35 36 |
# File 'lib/clio_client/http.rb', line 32 def multipart_post(path, params, parse = true) uri = base_uri("#{api_prefix}/#{path}") req = Net::HTTP::Post::Multipart.new(uri.request_uri, params) make_api_request(req, uri, parse) end |
#parse_response(res, parse) ⇒ 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/clio_client/http.rb', line 68 def parse_response(res, parse) case res when Net::HTTPNotFound raise ClioClient::ResourceNotFound.new(parse ? parse_body(res.body)["message"] : "") when Net::HTTPSuccess parse ? parse_body(res.body) : res.body when Net::HTTPUnauthorized begin = parse_body(res.body)["message"] rescue JSON::ParserError = res.body end raise ClioClient::Unauthorized.new() when Net::HTTPBadRequest raise ClioClient::BadRequest.new(parse_body(res.body)["message"]) when Net::HTTPSeeOther res["Location"] when Net::HTTPForbidden raise ClioClient::Forbidden.new(res.body) else raise UnknownResponse.new(res.body) end end |
#post(path, body = "", parse = true) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/clio_client/http.rb', line 38 def post(path, body ="", parse = true) uri = base_uri("#{api_prefix}/#{path}") req = Net::HTTP::Post.new(uri.request_uri) req.body = body req.add_field("Content-Type", "application/json") make_api_request(req, uri, parse) end |
#put(path, body = "", parse = true) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/clio_client/http.rb', line 24 def put(path, body = "", parse=true) uri = base_uri("#{api_prefix}/#{path}") req = Net::HTTP::Put.new(uri.request_uri) req.body = body req.add_field("Content-Type", "application/json") make_api_request(req, uri, parse) end |