Sha256: 26c02d0d2dd4134cb50fcb02732f716f6d4a9921c9f9f8f72411a0cc807b7874

Contents?: true

Size: 1.2 KB

Versions: 14

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'eac_ruby_utils/fs/temp'
require 'eac_ruby_utils/yaml'

class RequestBuilder
  class << self
    def from_file(api, path)
      new(api, ::EacRubyUtils::Yaml.load_file(path))
    end
  end

  FILE_FIELD_PARSER = /\A@(.+)\z/.to_parser { |m| m[1] }

  common_constructor :api, :data do
    self.data = data.with_indifferent_access
  end

  def result
    r = %i[verb headers].inject(selected_api.request(data.fetch(:url_suffix))) do |a, e|
      data[e].if_present(a) { |v| a.send(e, v) }
    end
    data[:body_data].if_present(r) { |v| r.body_data(build_body_data(v)) }
  end

  def build_body_data(source)
    return source unless source.is_a?(::Hash)

    source.map { |k, v| build_body_field(k, v) }
  end

  def build_body_field(name, value)
    FILE_FIELD_PARSER.parse(name).if_present([name, value]) do |v|
      [v, file_with_value(value)]
    end
  end

  def file_with_value(value)
    r = ::EacRubyUtils::Fs::Temp.directory.join('basename')
    r.write(value)
    ::File.new(r.to_path)
  end

  def selected_api
    data[:auth].if_present(api) do |v|
      api.class.new(api.root_url, v.fetch(:username), v.fetch(:password))
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
eac_tools-0.70.1 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.70.0 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.69.1 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.69.0 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.68.0 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.67.1 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.67.0 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.66.0 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.65.1 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.65.0 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.64.0 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.63.0 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.62.1 sub/eac_rest/spec/spec_helper/request_builder.rb
eac_tools-0.62.0 sub/eac_rest/spec/spec_helper/request_builder.rb