Last active
March 17, 2018 20:38
-
-
Save cflipse/1c4f40120143a26403f6a55fb44cd4ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'bundler/inline' | |
require 'json' | |
gemfile(:install) do | |
gem 'rom', '>= 2.0', github: 'rom-rb/rom', branch: 'master' | |
gem 'rom-http', github: 'rom-http-rb/rom-http', branch: 'master' | |
gem 'faraday' | |
end | |
class RequestHandler | |
def call(dataset) | |
uri = dataset.uri | |
path = "/#{dataset.name}/#{dataset.path}" | |
client = Faraday.new(uri, headers: dataset.headers) | |
client.send(dataset.request_method, path, dataset.params) | |
end | |
end | |
class ResponseHandler | |
def call(response, dataset) | |
Array([JSON.parse(response.body)]).flatten | |
end | |
end | |
conf = ROM::Configuration.new( | |
placeholder: [:http, { uri: "https://jsonplaceholder.typicode.com", | |
headers: { Accept: "application/json" }, | |
request_handler: RequestHandler.new, | |
response_handler: ResponseHandler.new } ] | |
) | |
class Users < ROM::Relation[:http] | |
gateway :placeholder | |
register_as :users | |
dataset "users" | |
def by_id(id) | |
append_path(id.to_s) | |
end | |
end | |
conf.register_relation(Users) | |
rom = ROM.container(conf) | |
puts rom.relation(:users).to_a | |
puts rom.relation(:users).by_id(1).to_a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment