-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add .configuration to Grape::API (fixes #1906) #1907
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
Conversation
Includes an intentionally failing test for the inheritance of config to mounted APIs, to be implemented
lib/grape/api.rb
Outdated
@@ -42,6 +42,12 @@ def override_all_methods! | |||
end | |||
end | |||
|
|||
def with_configuration(config) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like base_instance.configuration.merge!(config)
should work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this method return a new duplicate of base_instance
? Or copy of something though and be chainable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#merge
won't work because base_instance.configuration
is a Grape::Util::EndpointConfiguration
, which derives from LazyHash
which doesn't seem to implement merge
like a "normal" hash. I tried implementing merge
there but the way LazyHash
works is unclear to me so I wasn't able to do it, sorry.
And you're totally right, it should return self
so that run MyAPI.with_configuration(...)
can work normally, my bad. I don't see why it should return a duplicate though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay here. If you want I can help on implementing merge
and merge!
on LazyHash
spec/grape/api_spec.rb
Outdated
@@ -3754,6 +3754,42 @@ def before | |||
end | |||
end | |||
|
|||
describe '.with_configuration' do | |||
it 'passes configuration from outside' do | |||
subject.with_configuration(hello: 'hello', bread: 'bread') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imaged with_configuation
to be a block that yields a copy of things. The way this is written is configuring the API, for which in Ruby we have a better paradigm.
subject.configure do |config|
config[x] = y
end
Feels also that you shouldn't be able to config anything after mounting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, so you can do that right now? .configure
I mean, in a normal grape app, like this:
MyApi.configure do |config|
config[:bread] = 'bread'
end
# ...
run MyApi
and configure[:bread]
will be available, returning 'bread'
inside the api normally? If so, this issue is solved because that's exactly what I need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you can, but I want to be able to. What I am saying is that is a more standard way of doing configuration in Ruby. Check out https://github.com/slack-ruby/slack-ruby-client/blob/master/lib/slack/config.rb for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that clears it up, thank you! I'll try my hand at it.
has a new design using blocks for configuration
I like this, exposes |
@unleashy Let's finish this? See comments above, bring this to a close? |
Sorry for the delay, I have been busy. I'm kinda sad that I couldn't implement config inheritance but it's ok. |
Hi, sorry for the delay on commenting on this PR. Config inheritance is an interesting one but it might have unintended behaviour, as for the time being the class Parent < Grape::API
mount Child, with: configuration
end
class Child < Grape::API
get configuration[:key] do
'response'
end
end
class Base <Grape::API
mount Parent, with: {key: 'value'}
end |
@myxoh yeah I realised I could do that so it's not a big deal actually hehe. |
It looks like what we document in README is misleading then because it seems to imply that you are configuring a specific API? Let's explain how it inherits? @myxoh Anything else you think this PR needs to merge? |
@dblock you are configuring a specific API, what I was mentioning above is simply that you can use the same strategy to waterfall the dependency (i.e. you configure the top API, and then you code the middle top API to send through the configuration) this doesn't happen automatically, unless you are specifically sending the documentation downstream |
Should we just merge this @dblock? |
I hit merge. |
Thanks for your solid work @unleashy! |
Thank you all 💟 |
Still work in progress... includes an intentionally failing test for the inheritance of config to mounted APIs, which I am totally unsure of how to implement and would really like some help with :)
Also if the method name is appropriate. I prefer
with_configuration(hash)
but @myxoh thought ofwith(configuration: hash)
.Todo list:
inheritance of config on mounted endpointsThanks!