Sha256: 0b7963e7255a8be51ceab0c26b670dffc4ae4b5629b7b94b501d8a3f64aa51db
Contents?: true
Size: 1.71 KB
Versions: 3
Compression:
Stored size: 1.71 KB
Contents
require 'httparty' module Xhash module JsonApi module ClassMethods def default_headers { 'Content-type' => 'application/json', 'Authorization' => Xhash.api_key } end def api_get(url:, headers: {}) custom_headers = headers.merge(default_headers) response = HTTParty.get(Xhash.api_base + url, headers: custom_headers, timeout: Xhash.timeout) raise Xhash::Error.new(response) unless response_ok?(response) begin JSON.parse(response.body, symbolize_names: true) rescue => exception raise Xhash::MalformedResponse.new end end def api_post(url:, body: {}, headers: {}) custom_headers = headers.merge(default_headers) response = HTTParty.post( Xhash.api_base + url, body: body.to_json, headers: custom_headers, timeout: Xhash.timeout ) raise Xhash::Error.new(response) unless response_ok?(response) begin JSON.parse(response.body, symbolize_names: true) rescue => exception raise Xhash::MalformedResponse.new end end def api_post_multipart(url:, body: {}, headers: {}) custom_headers = headers.merge(default_headers) response = HTTParty.post( Xhash.api_base + url, multipart: true, body: body, headers: custom_headers, timeout: Xhash.timeout ) raise Xhash::Error.new(response) unless response_ok?(response) response.body end def response_ok?(response) !(response.code == 404 || response.code >= 500) end end def self.included(base) base.extend(ClassMethods) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
xhash_client-0.3.8 | lib/xhash/client/json_api.rb |
xhash_client-0.3.7 | lib/xhash/client/json_api.rb |
xhash_client-0.3.6 | lib/xhash/client/json_api.rb |