This library officially supports the following Ruby versions:
- MRI >=
2.7
jruby >=(awaits Ruby 2.7 compatibility)9.3
See LICENSE
file.
Abstract HTTP adapter for ROM
Home Page: https://rom-rb.org
License: MIT License
This library officially supports the following Ruby versions:
2.7
9.3
See LICENSE
file.
Sometimes, APIs like to key their response JSON in a weird way. For example, one API we use returns results in two ways:
{
"results": []
}
{
"customers": []
}
In order to account for this, it would be nice to have a way to attach meta on the dataset, as you can with the relation. That way, we can know the correct key to unwrap the response in the response handler.
Right now, we are hacking around this by storing the key in the params
, which is not ideal.
dataset do
with_meta(result_key: :customers)
end
Rather than relying on users correctly assembling the parts of a request URI in the request_handler, dataset should be able to build it's own URI.
In the example in the README right now, calling container.relation(:users).by_id(1)
returns a hash. It would be nice to see how to get some sort of domain object back.
I'm talking about http://www.rubydoc.info/gems/rom-http#Extending
Also, the require statements for rom and rom-http are missing.
romhttp.rb:19: syntax error, unexpected keyword_do_block, expecting keyword_end
...rs.each_with_object(request) do |(header, value), request|
... ^
romhttp.rb:19: syntax error, unexpected '|', expecting '='
romhttp.rb:51: syntax error, unexpected keyword_end, expecting end-of-input
It's not clear to me how they should be used. Right now rom-http's input schemas is used for projecting data, in other words for processing an HTTP response. And it looks strange provided that in rom-sql it is used for writing data via commands, and for projecting we're using so called output
schemas there. Also it's common for HTTP APIs that POST/PUT requests return an ID or some other sort of a result which differs from ordinary GETs. Any thoughts on this?
/cc @solnic @cflipse @AMHOL
I have the following adapter code:
require 'rom'
require 'rom-http'
module ROM
module RestfullClientAdapter
class Dataset < ROM::HTTP::Dataset
default_request_handler -> (dataset) do
puts "begin request"
end
default_response_handler -> (response, dataset) do
puts "end request"
end
def response_transformer(response)
end
end
class Gateway < ROM::HTTP::Gateway; end
class Relation < ROM::HTTP::Relation
adapter :restfull_client
end
module Commands
class Create < ROM::HTTP::Commands::Create
adapter :restfull_client
end
class Update < ROM::HTTP::Commands::Update
adapter :restfull_client
end
class Delete < ROM::HTTP::Commands::Delete
adapter :restfull_client
end
end
end
end
ROM.register_adapter(:restfull_client, ROM::RestfullClientAdapter)
and I'm getting to following exception:
KeyError: key not found: :base
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-2.0.0/lib/rom/plugins/relation/view.rb:35:in `fetch'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-2.0.0/lib/rom/plugins/relation/view.rb:35:in `attributes'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-repository-0.3.1/lib/rom/repository/relation_proxy.rb:212:in `method_missing'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-repository-0.3.1/lib/rom/repository/relation_proxy.rb:132:in `to_ast'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-repository-0.3.1/lib/rom/repository.rb:203:in `compile_command'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-repository-0.3.1/lib/rom/repository.rb:138:in `block in command'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/concurrent-ruby-1.0.2/lib/concurrent/map.rb:133:in `block in fetch_or_store'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/concurrent-ruby-1.0.2/lib/concurrent/map.rb:122:in `fetch'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/concurrent-ruby-1.0.2/lib/concurrent/map.rb:132:in `fetch_or_store'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-repository-0.3.1/lib/rom/repository.rb:137:in `command'
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-repository-0.3.1/lib/rom/repository/class_interface.rb:116:in `block in define_command_method'
./spec/rom_restful_client_adapter_spec.rb:15:in `block (2 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
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:
class Relation < ROM::HTTP::Relation
adapter :restfull_client
option :base, self
end
and now I get:
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-support-2.0.0/lib/rom/support/options.rb:49:in `initialize': undefined method `fetch' for ROM::RestfullClientAdapter::Relation:Class (NoMethodError)
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-support-2.0.0/lib/rom/support/options.rb:164:in `new'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-support-2.0.0/lib/rom/support/options.rb:164:in `option'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:25:in `<class:Relation>'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:22:in `<module:RestfullClientAdapter>'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:5:in `<module:ROM>'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:4:in `<top (required)>'
from /Volumes/fiverr_dev/restfull-client/spec/support/models.rb:2:in `require'
from /Volumes/fiverr_dev/restfull-client/spec/support/models.rb:2:in `<top (required)>'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `load'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `block in <top (required)>'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `each'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `<top (required)>'
from /Users/omer.katz/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/omer.katz/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1394:in `block in requires='
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1394:in `each'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1394:in `requires='
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration_options.rb:112:in `block in process_options_into'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration_options.rb:111:in `each'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration_options.rb:111:in `process_options_into'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration_options.rb:21:in `configure'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:99:in `setup'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:86:in `run'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/invocations.rb:23:in `call'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:69:in `run'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/exe/rspec:4:in `<top (required)>'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/bin/rspec:23:in `load'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/bin/rspec:23:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
After adding fetch to the Relation class I get:
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-2.0.0/lib/rom/relation/class_interface.rb:129:in `rescue in []': Failed to find relation class for coercer adapter. Make sure ROM setup was started and the adapter identifier is correct. (ROM::AdapterNotPresentError)
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-2.0.0/lib/rom/relation/class_interface.rb:127:in `[]'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-support-2.0.0/lib/rom/support/options.rb:51:in `initialize'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-support-2.0.0/lib/rom/support/options.rb:164:in `new'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-support-2.0.0/lib/rom/support/options.rb:164:in `option'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:29:in `<class:Relation>'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:22:in `<module:RestfullClientAdapter>'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:5:in `<module:ROM>'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:4:in `<top (required)>'
from /Volumes/fiverr_dev/restfull-client/spec/support/models.rb:2:in `require'
from /Volumes/fiverr_dev/restfull-client/spec/support/models.rb:2:in `<top (required)>'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `load'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `block in <top (required)>'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `each'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `<top (required)>'
from /Users/omer.katz/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/omer.katz/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1394:in `block in requires='
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1394:in `each'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1394:in `requires='
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration_options.rb:112:in `block in process_options_into'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration_options.rb:111:in `each'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration_options.rb:111:in `process_options_into'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/configuration_options.rb:21:in `configure'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:99:in `setup'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:86:in `run'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/invocations.rb:23:in `call'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:69:in `run'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.2/exe/rspec:4:in `<top (required)>'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/bin/rspec:23:in `load'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/bin/rspec:23:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
It's really not clear what I should be doing.
Can you guys please help me and also document the process?
The schema included here was a first run through the concept; I think we should be able to use the schema used in ROM core now.
I'm talking about http://www.rubydoc.info/gems/rom-http#Extending
rom-rb/rom-support#9 should fix this but there might be something wrong with the example itself.
/Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rom-2.0.0/lib/rom/plugins/relation/view.rb:76:in `view': undefined method `arity' for nil:NilClass (NoMethodError)
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:30:in `<class:Relation>'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:22:in `<module:RestfullClientAdapter>'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:5:in `<module:ROM>'
from /Volumes/fiverr_dev/restfull-client/lib/rom_adapter.rb:4:in `<top (required)>'
from /Volumes/fiverr_dev/restfull-client/spec/support/models.rb:2:in `require'
from /Volumes/fiverr_dev/restfull-client/spec/support/models.rb:2:in `<top (required)>'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `load'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `block in <top (required)>'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `each'
from /Volumes/fiverr_dev/restfull-client/spec/spec_helper.rb:6:in `<top (required)>'
from /Users/omer.katz/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/omer.katz/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/configuration.rb:1394:in `block in requires='
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/configuration.rb:1394:in `each'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/configuration.rb:1394:in `requires='
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/configuration_options.rb:112:in `block in process_options_into'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/configuration_options.rb:111:in `each'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/configuration_options.rb:111:in `process_options_into'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/configuration_options.rb:21:in `configure'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/runner.rb:99:in `setup'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/runner.rb:86:in `run'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/invocations.rb:23:in `call'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/runner.rb:69:in `run'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/gems/rspec-core-3.5.3/exe/rspec:4:in `<top (required)>'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/bin/rspec:23:in `load'
from /Users/omer.katz/.rvm/gems/ruby-2.2.2@restfull_client/bin/rspec:23:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
It may be helpful to provide a pair of built-in response handlers:
Just to clarify....
It doesn't support inferring schemas, does it?
Right now, the replaces the params hash each time with_params
is called. Add a new method that wraps the process of merging new params, so it's easier to work with for chained interfaces.
REST APIs come in many flavours, some not always purely RESTful. It would be helpful if schemas for rom-http relations could support optional attributes. This is for a scenario where an API provides a limited set of attributes from a search or collection API endpoint. Then the full details of the resource must be retrieved from a REST endpoint that represents that resource.
The specific example that triggered this is using rom-http with the Spoonacular API. This has a "Search Recipes" endpoint that returns recipes with about 8 attributes, then the "Get Recipe Information" endpoint returns a full recipe object with much more data.
This might look like:
module Spoonacular
class Recipes < ROM::Relation[:http]
schema(:recipes) do
attribute :id, Types::Integer.meta(primary_key: true)
attribute :title, Types::String
attribute :image, Types::String
attribute :readyInMinutes, Types::Integer.optional
attribute :servings, Types::Integer.optional
attribute :sourceUrl, Types::String.optional
attribute :extendedIngredients, Types::Array.optional
end
struct_namespace Spoonacular
auto_struct true
....
end
end
NOTE: Some attributes are camelCase because currently I'm not sure of the best approach to map them to ruby snake case. If there is a known pattern for this, tips welcome :)
First queried here.
different HTTP methods are going to have different needs from their response handlers; it should be easy to select a separate response handler to use, so that the same call
method doesn't have to handle both DELETE
and GET
responses.
path-based requests are not particularly capable of handling requests for multiple ID values,
and when an HTTP relation is provided as a part of a pipeline, it may be necessary to provide the illusion of a single interaction.
In such cases, it's almost certainly preferable if the remote service provides some sort of multiple-id query interface, for performance reasons, but this is not always possible. This is a knife with a sharpened handle, but in some cases may be necessary...
Defining an associaton gives me a finalize_associations!'
exception, so I guess it's not implemented. I need to combine a http relation with a sql relation. Am I not mistaking that the only option I have is to use combine_with
?
Is there a way to set default params for current relation? I want to be able not to include these kind of lines
.with_base_path('users')
.add_params(expand: 'account,account_details')
everywhere in current relation...
Looks like this has already been fixed in the repo. It just needs to get tagged and pushed to the gem hosts.
https://rubygems.org/gems/rom-http/
VERSIONS:
0.6.0 - February 07, 2017 (18.5 KB)
0.6.0.rc1 - January 28, 2017 (18.5 KB)
0.5.0 - August 08, 2016 (17 KB)
0.4.0 - April 30, 2016 (15.5 KB)
0.3.0 - March 17, 2016 (15 KB)
Show all versions (11 total)
RUNTIME DEPENDENCIES (4):
concurrent-ruby >= 0
dry-core >= 0.2.3, ~> 0.2
dry-equalizer ~> 0.2
rom ~> 3.0.0
DEVELOPMENT DEPENDENCIES (4):
bundler >= 0
rake ~> 10.0
rspec ~> 3.1
rspec-its >= 0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.