class RemoteResource::AttributeHttpClient

Public Class Methods

new(attribute, client = nil) click to toggle source
# File lib/remote_resource/attribute_http_client.rb, line 7
def initialize(attribute, client = nil)
  @attribute = attribute
  @client = client || @attribute.client
  @resource_name = @attribute.resource_name
end

Public Instance Methods

get(headers = {}) click to toggle source
# File lib/remote_resource/attribute_http_client.rb, line 13
def get(headers = {})
  instrument_attribute('http_get', @attribute) do
    if headers && headers.size > 0
      with_headers_for_method(:get, headers) do |client|
        @attribute.resource(client)
      end
    else
      @attribute.resource(@client)
    end
  end
  @client.last_response
end

Private Instance Methods

with_headers_for_method(method, headers) { |client| ... } click to toggle source

Internal: yield a client with headers bound on the supplied method.

# File lib/remote_resource/attribute_http_client.rb, line 29
def with_headers_for_method(method, headers)
  old_method = "orig_#{method}".to_sym
  client_class = @client.singleton_class
  client_class.send(:alias_method, old_method, method)
  client_class.send(:define_method, method) do |url, _|
    send(old_method, url, headers: headers)
  end
  yield @client
  client_class.send(:alias_method, method, old_method)
  client_class.send(:remove_method, old_method)
end