Sha256: 98c54280eff70997153b41c2e7397c866fc48d5b6260ab63ed0a423f2428e485

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require 'active_resource'
module MissionHub
  class Base < ActiveResource::Base
    def self.connection(refresh = false)
      if defined?(@connection) || superclass == Object
        @connection = MissionHub::Connection.new(site, format) if refresh || @connection.nil?
        @connection.proxy = proxy if proxy
        @connection.auth_type = auth_type if auth_type
        @connection.timeout = timeout if timeout
        @connection
      else
        superclass.connection
      end
    end
  end

  class Connection < ActiveResource::Connection

    private

    def request(method, path, *arguments)
      result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
        path = add_secret_to_path(path)
        payload[:method]      = method
        payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
        payload[:result]      = http.send(method, path, *arguments)
      end
      handle_response(result)
    rescue Timeout::Error => e
      raise TimeoutError.new(e.message)
    rescue OpenSSL::SSL::SSLError => e
      raise SSLError.new(e.message)
    end

    def add_secret_to_path(path)
      unless path.include?('secret=')
        path += path.include?('?') ? '&' : '?'
        path += "secret=#{Thread.current["mission_hub.client_secret"]}"
      end
      path
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
MissionHub-2.1.1 lib/missionhub/base.rb
MissionHub-2.1.0 lib/missionhub/base.rb