Sha256: ffe813e738afe27972d774a8b94322baac733241a0dbebc0a71f48f9ba01cbb6

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

class RemoteInteractorService
  def initialize(source)
    @source = source
  end

  def send_request(path, query_values = {})
    uri = initialize_uri(path, query_values)
    req = initialize_request(uri)
    connection = initialize_connection(uri)
    perform_request(connection, req)
  rescue => e
    return unless defined?(Rails)
    ::Rails.logger.error { "Lit remote error: #{e}" }
  end

  private

  def initialize_uri(path, query_values)
    uri = URI(@source.url + path)
    query_values.each do |k, v|
      params = URI.decode_www_form(uri.query || '') << [k, v]
      uri.query = URI.encode_www_form(params)
    end
    uri
  end

  def initialize_request(uri)
    req = Net::HTTP::Get.new(uri.request_uri)
    req.add_field('Authorization', %(Token token="#{@source.api_key}"))
    req
  end

  def initialize_connection(uri)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (uri.port == 443)
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http
  end

  def perform_request(connection, request)
    res = connection.start { |http| http.request(request) }
    return res unless res.is_a?(Net::HTTPSuccess)
    JSON.parse(res.body)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lit-1.0.2 app/services/remote_interactor_service.rb
lit-1.0.1 app/services/remote_interactor_service.rb
lit-1.0 app/services/remote_interactor_service.rb
lit-0.4.0.pre.alpha.5 app/services/remote_interactor_service.rb
lit-0.4.0.pre.alpha.4 app/services/remote_interactor_service.rb
lit-0.4.0.pre.alpha.3 app/services/remote_interactor_service.rb
lit-0.4.0.pre.alpha.2 app/services/remote_interactor_service.rb
lit-0.4.0.pre.alpha.1 app/services/remote_interactor_service.rb
lit-0.4.0.pre.alpha app/services/remote_interactor_service.rb