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

Is it possible to access RequestStore from a Celluloid future? #41

Closed
yannski opened this issue Apr 27, 2015 · 3 comments
Closed

Is it possible to access RequestStore from a Celluloid future? #41

yannski opened this issue Apr 27, 2015 · 3 comments

Comments

@yannski
Copy link

yannski commented Apr 27, 2015

Maybe it's a dumb question or not the "correct way" to achieve what I'd like to do, but for the moment, it seems it's not possible to access RequestStore from a Celluloid future :/

@steveklabnik
Copy link
Owner

Can you show me some specific code with what you're trying to do?

@yannski
Copy link
Author

yannski commented Apr 27, 2015

I've doing HTTP requests with Faraday in a Celluloid Future. I have my own faraday middleware that looks like:

class CustomLogsDisplay < Faraday::Middleware
  def initialize(app, options = {})
    super(app)
    @name = options.fetch(:name, 'request.faraday')
  end

  def call(env)
    foreground_color = 33
    client = @name
    url = env[:url]
    http_method = env[:method].to_s.upcase
    line = "\033[1;#{foreground_color};40m[%s]: %s %s\033[0m" % [client, http_method, url.to_s]
    RequestLocals.store[:debug_logs] << line
    @app.call(env)
  end
end

And in a before_filter I just do RequestLocals.store[:debug_logs] = []

For the faraday requests that are done inside a Celluloid Future, RequestLocals.store[:debug_logs] is always nil.

My final goal is to display the content of RequestLocals.store[:debug_logs] on a Rails view.

@yannski
Copy link
Author

yannski commented Apr 27, 2015

Sorry the code above is with the request_store_rails gem, but the result is the same with the request_store gem and this code for the Faraday Middleware:

class CustomLogsDisplay < Faraday::Middleware
  def initialize(app, options = {})
    super(app)
    @name = options.fetch(:name, 'request.faraday')
  end

  def call(env)
    foreground_color = 33
    client = @name
    url = env[:url]
    http_method = env[:method].to_s.upcase
    line = "\033[1;#{foreground_color};40m[%s]: %s %s\033[0m" % [client, http_method, url.to_s]
    RequestStore.store[:debug_logs] << line
    @app.call(env)
  end
end

And with this code in the before_filter: RequestStore.store[:debug_logs] = []

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

No branches or pull requests

2 participants