Class: ROM::HTTP::Gateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/rom/http/gateway.rb

Overview

HTTP gateway

Examples:

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

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Gateway

HTTP gateway interface

Parameters:

  • config (Hash)

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

See Also:


37
38
39
40
# File 'lib/rom/http/gateway.rb', line 37

def initialize(config)
  @datasets = Concurrent::Map.new
  @config = config
end

Instance Method Details

#[](name) ⇒ Dataset

Retrieve dataset with the given name

Parameters:

  • name (String)

    dataaset name

Returns:


49
50
51
# File 'lib/rom/http/gateway.rb', line 49

def [](name)
  datasets.fetch(name)
end

#dataset(name) ⇒ Dataset

Build dataset with the given name

Parameters:

  • name (String)

    dataaset name

Returns:


60
61
62
63
# File 'lib/rom/http/gateway.rb', line 60

def dataset(name)
  dataset_klass = namespace.const_defined?(:Dataset) ? namespace.const_get(:Dataset) : Dataset
  datasets[name] = dataset_klass.new(config.merge(uri: uri, base_path: name))
end

#dataset?(name) ⇒ Boolean

Check if dataset exists

Parameters:

  • name (String)

    dataset name

Returns:

  • (Boolean)

70
71
72
# File 'lib/rom/http/gateway.rb', line 70

def dataset?(name)
  datasets.key?(name)
end