lib/eac_rest/response.rb in eac_rest-0.6.1 vs lib/eac_rest/response.rb in eac_rest-0.7.0

- old
+ new

@@ -1,8 +1,9 @@ # frozen_string_literal: true require 'active_support/core_ext/hash/conversions' +require 'eac_rest' require 'eac_ruby_utils/core_ext' require 'json' module EacRest class Response < ::StandardError @@ -16,14 +17,15 @@ # https://www.w3.org/wiki/LinkHeader LINK_PARSER = /\A\<(.+)\>\s*;\s*rel\s*=\s*\"(.*)\"\z/.to_parser do |m| [m[2], m[1]] end - common_constructor :curl, :body_data_proc + common_constructor :request + delegate :body_data_proc, to: :request def body_data - r = performed_curl.headers['Accept'].if_present(body_str) do |v| + r = performed.headers['Accept'].if_present(body_str) do |v| method_name = "body_data_from_#{v.parameterize.underscore}" respond_to?(method_name) ? send(method_name) : body_str end r = body_data_proc.call(r) if body_data_proc.present? r @@ -33,11 +35,14 @@ raise_unless_200 body_data end - delegate :body_str, to: :performed_curl + # @return [String] + def body_str + performed.body + end def body_str_or_raise raise_unless_200 body_str @@ -46,11 +51,11 @@ def header(name) hash_search(headers, name) end def headers - performed_curl.header_str.each_line.map(&:strip)[1..-1].reject(&:blank?) + performed.header_str.each_line.map(&:strip)[1..-1].reject(&:blank?) .map { |header_line| HEADER_LINE_PARSER.parse!(header_line) } .to_h end def link(rel) @@ -68,11 +73,11 @@ raise self end def status - performed_curl.status.to_i + performed.status.to_i end delegate :url, to: :curl def to_s @@ -95,18 +100,12 @@ return v if k.to_s.downcase == key end nil end - def perform - @perform ||= begin - curl.perform || raise("CURL perform failed for #{url}") - true - end - end - - def performed_curl - perform - curl + def performed + @performed_curl ||= request.faraday_response + rescue ::Faraday::Error + raise ::EacRest::Error end end end