-
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document creating an adapter more thoroughly #41
Labels
Comments
Sorry, just looked into this and it seems that the class Users < ROM::Relation[:my_adapter]
dataset :users
register_as :users
def by_id(id)
with_path(id.to_s)
end
end Full example: require 'json'
require 'uri'
require 'net/http'
gemfile(true) do
gem 'rom'
gem 'rom-http'
end
module ROM
module MyAdapter
class Dataset < ROM::HTTP::Dataset
default_request_handler ->(dataset) do
uri = URI(dataset.uri)
uri.path = "/#{dataset.name}/#{dataset.path}"
uri.query = URI.encode_www_form(dataset.params)
http = Net::HTTP.new(uri.host, uri.port)
request_klass = Net::HTTP.const_get(ROM::Inflector.classify(dataset.request_method))
request = request_klass.new(uri.request_uri)
dataset.headers.each_with_object(request) do |(header, value), request|
request[header.to_s] = value
end
response = http.request(request)
end
default_response_handler ->(response, dataset) do
Array([JSON.parse(response.body)]).flatten
end
end
class Gateway < ROM::HTTP::Gateway; end
class Relation < ROM::HTTP::Relation
adapter :my_adapter
end
module Commands
class Create < ROM::HTTP::Commands::Create
adapter :my_adapter
end
class Update < ROM::HTTP::Commands::Update
adapter :my_adapter
end
class Delete < ROM::HTTP::Commands::Delete
adapter :my_adapter
end
end
end
end
ROM.register_adapter(:my_adapter, ROM::MyAdapter)
configuration = ROM::Configuration.new(:my_adapter, {
uri: 'http://jsonplaceholder.typicode.com',
headers: {
Accept: 'application/json'
}
})
class Users < ROM::Relation[:my_adapter]
dataset :users
register_as :users
def by_id(id)
with_path(id.to_s)
end
end
configuration.register_relation(Users)
container = ROM.container(configuration)
container.relation(:users).by_id(1).to_a |
I am using rom-repository. |
Here's a working example with rom-repository: require 'json'
require 'uri'
require 'net/http'
gemfile(true) do
gem 'rom'
gem 'rom-http'
gem 'rom-repository'
end
module ROM
module MyAdapter
class Dataset < ROM::HTTP::Dataset
default_request_handler ->(dataset) do
uri = URI(dataset.uri)
uri.path = "/#{dataset.name}/#{dataset.path}"
uri.query = URI.encode_www_form(dataset.params)
http = Net::HTTP.new(uri.host, uri.port)
request_klass = Net::HTTP.const_get(ROM::Inflector.classify(dataset.request_method))
request = request_klass.new(uri.request_uri)
dataset.headers.each_with_object(request) do |(header, value), request|
request[header.to_s] = value
end
response = http.request(request)
end
default_response_handler ->(response, dataset) do
Array([JSON.parse(response.body, symbolize_names: true)]).flatten
end
end
class Gateway < ROM::HTTP::Gateway; end
class Relation < ROM::HTTP::Relation
adapter :my_adapter
end
module Commands
class Create < ROM::HTTP::Commands::Create
adapter :my_adapter
end
class Update < ROM::HTTP::Commands::Update
adapter :my_adapter
end
class Delete < ROM::HTTP::Commands::Delete
adapter :my_adapter
end
end
end
end
ROM.register_adapter(:my_adapter, ROM::MyAdapter)
configuration = ROM::Configuration.new(:my_adapter, {
uri: 'http://jsonplaceholder.typicode.com',
headers: {
Accept: 'application/json'
}
})
class Users < ROM::Relation[:my_adapter]
dataset :users
register_as :users
schema do
attribute :id, ROM::Types::Int
attribute :name, ROM::Types::String
attribute :username, ROM::Types::String
attribute :email, ROM::Types::String
attribute :phone, ROM::Types::String
attribute :website, ROM::Types::String
end
view(:base, %i(id name username email phone website)) do
self
end
def by_id(id)
with_path(id.to_s)
end
end
configuration.register_relation(Users)
container = ROM.container(configuration)
class UserRepository < ROM::Repository[:users]
def find(id)
users.by_id(id).one!
end
end
user_repo = UserRepository.new(container)
user_repo.find(1)
# => #<ROM::Struct[User] id=1 name="Leanne Graham" username="Bret" email="[email protected]" phone="1-770-736-8031 x56442" website="hildegard.org"> |
Closing due to inactivity |
Why are you closing this issue? Has the documentation been updated and extended? |
Fair point |
AMHOL
changed the title
Document creating an adapter more throughoutly
Document creating an adapter more thoroughly
Oct 8, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have the following adapter code:
and I'm getting to following exception:
After digging a bit it seems I need to set the option base to be an instance but I'm not sure of which class.
So I did:
and now I get:
After adding fetch to the Relation class I get:
It's really not clear what I should be doing.
Can you guys please help me and also document the process?
The text was updated successfully, but these errors were encountered: