require "faraday" require "json" class Requester attr_reader :host, :conn def initialize(host, options={}) @host = host @conn = init_conn end def json_send(http_method, path, params={}, headers={}) res = @conn.send(http_method) do |req| req.url path req.headers['Content-Type'] = 'application/json' req.body = params.to_json end process_response(res) rescue => e error_response(e) end private def init_conn() Faraday.new(url: host, ssl: nil) do |faraday| faraday.request :url_encoded # form-encode POST params faraday.response :logger # log requests to $stdout faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end def process_response(response) result = {'status' => response.status} begin body = Oj.load(response.body) body = body.is_a?(Hash) ? body : {'body' => body} body['body_status'] = body.delete('status') unless body['status'].is_a?(Integer) rescue body = {'body' => response.body} end result.merge(body) end def error_response(err) {'status' => 500, 'message' => "#{err.class.name}: #{err.message}"} end end