class ROM::HTTP::Gateway

HTTP gateway

@example

config = {
  uri: 'http://jsonplaceholder.typicode.com',
  headers: { Accept: 'applicaiton/json' }
}

gateway = ROM::HTTP::Gateway.new(config)
users   = gateway.dataset(:users)

@api public

Attributes

config[R]
datasets[R]

Public Class Methods

new(config) click to toggle source

HTTP gateway interface

@param [Hash] config configuration options

@option config [String] :uri The base API for the HTTP service
@option config [Hash] :headers Default request headers

@see Dataset

@api public

# File lib/rom/http/gateway.rb, line 37
def initialize(config)
  @datasets = Concurrent::Map.new
  @config = config
end

Public Instance Methods

[](name) click to toggle source

Retrieve dataset with the given name

@param [String] name dataaset name

@return [Dataset]

@api public

# File lib/rom/http/gateway.rb, line 49
def [](name)
  datasets.fetch(name)
end
dataset(name) click to toggle source

Build dataset with the given name

@param [String] name dataaset name

@return [Dataset]

@api public

# File lib/rom/http/gateway.rb, line 60
def dataset(name)
  datasets[name] = dataset_class.new(**dataset_options(name))
end
dataset?(name) click to toggle source

Check if dataset exists

@param [String] name dataset name

@api public

# File lib/rom/http/gateway.rb, line 69
def dataset?(name)
  datasets.key?(name)
end

Private Instance Methods

dataset_class() click to toggle source

Return Dataset class

@return [Class]

@api private

# File lib/rom/http/gateway.rb, line 80
def dataset_class
  namespace.const_defined?(:Dataset) ? namespace.const_get(:Dataset) : Dataset
end
dataset_options(name) click to toggle source

Return Dataset options

@return [Class]

@api private

# File lib/rom/http/gateway.rb, line 89
def dataset_options(name)
  config.merge(uri: uri, base_path: name, **default_handlers)
end
default_handlers() click to toggle source

Return default handlers registered in Handlers registry

@return [Hash]

@api private

# File lib/rom/http/gateway.rb, line 98
def default_handlers
  if (handlers_key = config[:handlers])
    Handlers[handlers_key]
      .map { |key, value| [:"#{key}_handler", value] }.to_h
  else
    EMPTY_HASH
  end
end
namespace() click to toggle source

@api private

# File lib/rom/http/gateway.rb, line 113
def namespace
  self.class.to_s[/(.*)(?=::)/].split("::").inject(::Object) do |constant, const_name|
    constant.const_get(const_name)
  end
end
uri() click to toggle source

@api private

# File lib/rom/http/gateway.rb, line 108
def uri
  config.fetch(:uri) { raise Error, "+uri+ configuration missing" }
end