Skip to content
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

Add .configuration to Grape::API (fixes #1906) #1907

Merged
merged 7 commits into from
Oct 2, 2019
Merged

Add .configuration to Grape::API (fixes #1906) #1907

merged 7 commits into from
Oct 2, 2019

Conversation

unleashy
Copy link
Contributor

@unleashy unleashy commented Sep 12, 2019

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 of with(configuration: hash).

Todo list:

  • inheritance of config on mounted endpoints
  • documentation
  • changelog

Thanks!

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)
Copy link
Member

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?

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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

@@ -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')
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@dblock dblock Sep 13, 2019

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.

Copy link
Contributor Author

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.

@dblock
Copy link
Member

dblock commented Sep 16, 2019

I like this, exposes configure with no other api changes. Document it?

@unleashy unleashy changed the title [WIP] Add .with_configuration to Grape::API (fixes #1906) [WIP] Add .configuration to Grape::API (fixes #1906) Sep 16, 2019
@dblock
Copy link
Member

dblock commented Sep 22, 2019

@unleashy Let's finish this? See comments above, bring this to a close?

@unleashy
Copy link
Contributor Author

Sorry for the delay, I have been busy. I'm kinda sad that I couldn't implement config inheritance but it's ok.

@unleashy unleashy marked this pull request as ready for review September 22, 2019 19:00
@unleashy unleashy changed the title [WIP] Add .configuration to Grape::API (fixes #1906) Add .configuration to Grape::API (fixes #1906) Sep 22, 2019
@myxoh
Copy link
Member

myxoh commented Sep 23, 2019

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 configuration was meant to be specific for the API endpoints.
One way I did do inheritance one I thought that was appropiate was

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

@unleashy
Copy link
Contributor Author

@myxoh yeah I realised I could do that so it's not a big deal actually hehe.

@dblock
Copy link
Member

dblock commented Sep 24, 2019

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?

@myxoh
Copy link
Member

myxoh commented Sep 24, 2019

@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

@myxoh
Copy link
Member

myxoh commented Oct 1, 2019

Should we just merge this @dblock?

@dblock dblock merged commit 2b7be0c into ruby-grape:master Oct 2, 2019
@dblock
Copy link
Member

dblock commented Oct 2, 2019

I hit merge.

@dblock
Copy link
Member

dblock commented Oct 2, 2019

Thanks for your solid work @unleashy!

@unleashy
Copy link
Contributor Author

unleashy commented Oct 2, 2019

Thank you all 💟

basjanssen pushed a commit to basjanssen/grape that referenced this pull request Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants