Sha256: 014a6d126eac4ecad4cfaa9e13b9228ada212add72901dadee1afe16aa05018d

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

require "faraday"
require "uri"
require "active_support/core_ext/object"
require "octogate/transfer_request"

class Octogate::Client
  attr_reader :event

  def initialize(event)
    @event = event
  end

  def request_to_targets
    Octogate.config.targets.each do |target_name, t|
      if match_target?(t)
        request(t)
      end
    end
  end

  def request(target)
    uri = URI(target.url)

    options = {url: target.url}
    options.merge!(ssl_options) if uri.scheme == "https"

    conn = build_connection(options, target.username, target.password)

    params = target.params.is_a?(Proc) ?
      target.instance_exec(event, &target.params) : target.params

    case target.http_method
    when :get
      Octogate::TransferRequest::GET.new(conn)
        .do_request(url: uri.path, params: params)
    when :post
      Octogate::TransferRequest::POST.new(conn)
        .do_request(url: uri.path, params: params, parameter_type: target.parameter_type)
    end

    sent_event = Octogate::SentEvent.build(event, target, params)
    Octogate.add_sent(sent_event)
  end

  private

  def match_target?(target)
    condition = event.default_condition && target.include_event?(event)
    case target.match
    when Proc
      condition && target.instance_exec(event, &target.match)
    when nil
      condition
    else
      condition && !!target.match
    end
  end

  def build_connection(options, username = nil, password = nil)
    conn = Faraday.new(options) do |faraday|
      faraday.request  :url_encoded
      faraday.response :logger if ENV["RACK_ENV"] == "development" || ENV["RACK_ENV"] == "production"
      faraday.adapter  Faraday.default_adapter
    end
    conn.basic_auth(username, password) if username && password
    conn
  end

  def ssl_options
    if Octogate.config.ssl_verify
      Octogate.config.ca_file ? {ssl: {ca_file: Octogate.config.ca_file}} : {}
    else
      {ssl: {verify: false}}
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
octogate-0.4.0 lib/octogate/client.rb
octogate-0.3.0 lib/octogate/client.rb