Git Product home page Git Product logo

brainstem's People

Contributors

a-bates avatar ahuth avatar andrewlew1s avatar bennapp avatar brandonduff avatar brentsondgeroth avatar cadryden avatar cantino avatar cpalafox avatar dependabot[bot] avatar diclophis avatar doctorpetervannostrand avatar dordan avatar indirect avatar jarednielson avatar juanca avatar ksylvest avatar lexun avatar marcusg avatar minglecm avatar mseverini avatar naganowl avatar naiyt avatar ncperry avatar racerpeter avatar rickdav avatar robyurkowski avatar seanculver avatar shirish-pampoorickal avatar stevepm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

brainstem's Issues

Conditionally preload data for optional fields

At the moment, I cannot figure out if there is a way to conditionally preload an association given a certain request. At the moment, custom_preload is invoked with the models and the requests associations.

I think it would be beneficial to also have access to the optional fields (are there other possibilities of bottlenecks?) in the custom_preload method to allow custom preloading. At the moment, I am preloading optional fields on every request which has an impact on memory used -- even when it is not necessary for a subset of requests.

Or better yet: a field and an association should provide a preload interface that can be automatically pooled to maximize usage of the preloader and minimize unnecessary memory usage.

Setup Airbrake for your Ruby application

Installation

Using bundler

Add the Airbrake Ruby gem to your Gemfile:

gem 'airbrake-ruby', '~> 2.3'

Manual

Invoke the following command from your terminal:

gem install airbrake-ruby

Example

This is the minimal example that you can use to test Airbrake Ruby with your project (You can find your project ID and API KEY with your project's settings):

require 'airbrake-ruby'

Airbrake.configure do |c|
  c.project_id = <Your project ID>
  c.project_key = '<Your project API KEY>'
end

# Synchronous error delivery.
begin
  1/0
rescue ZeroDivisionError => ex
  # Return value is a Hash.
  response = Airbrake.notify_sync(ex)
end

puts 'A ZeroDivisionError was sent to Airbrake synchronously!',
     "See it at #{response['url']}\n"

# Asynchronous error delivery.
begin
  1/0
rescue ZeroDivisionError => ex
  Airbrake.notify(ex).then do |resp|
    puts 'A ZeroDivisionError was sent to Airbrake asynchronously!',
         "See it at #{resp['url']}"
  end
end

Airbrake.close

Configuration

For advanced configuration options like error filtering or custom parameters, please visit our official GitHub repo.

Ability to preload on an association by association basis

The ability to specify preloads on an association would be a good addition to Brainstem.

These preloads would only be added when a specific association is requested (via includes) instead of all the time in the case of custom_preload.

Behavior around preloading multiple times (IE globally preloading [:workspace] and then preloading workspace in an association) would need to be examined.

class StoryPresenter < Brainstem::Presenter
  presents "Story"

  # This will still be used to globally load associations on all models.
  def custom_preload(models, associations = [])
    ActiveRecord::Associations::Preloader.new(models, [:workspace]).run
  end

  def present(story)
    {
      :implicit_preload   => association(:story) # preloads story implicitly based on method name
      :explicit_preload   => association(:blah, :preloads => [:story]) # will use :story instead of :blah
      :explicit_preload2  => association(:json_name => "time", :preloads => [:line_items]) { |model| model }
      :empty_preload      => association(:json_name => "test") { |model| model } # doesn't preload anything
    }
  end
end

Nest meta response key-values under `meta`

Kinda weird that we have to whitelist in our JS libs. Would be nice to be able to do this programmatically. Nesting count, page info, and results under meta can facilitate this.

Thoughts? We will need to be able to version our API or figure out how many other people use this.

API v2+

Just a general issue about potential useful

  • features
  • cleanup
  • optimizations
  • ...
  • PROFIT

I think it will make sense to use a Github Project to manage prioritization. This issue should be used to propose and discuss changes.

Support for Rails 4 style scopes

If you're using Rails 4 style scopes which require a callable block (e.g., lambda) that doesn't take any args you'll run into some trouble with Brainstem. Example:

Say I have a Rails 3 style scope that looks like this:

scope :current, where(:current => true)

This works fine if I want to send a request for entities in that scope:

GET /coolsite/api/v1?current=true

But say I convert it to a Rails 4 style scope with a lambda:

scope :current, -> { where(:current => true) }

This will cause things to explode with Exception: wrong number of arguments (1 for 0) because of how PresenterCollection.run_filters works. It will extract "true" as an argument that you want to pass to the scope, and call it with scope = scope.send(filter_name, arg) (passing in true as the arg). And since the scope doesn't take an argument, we get screamed at.

I've been thinking about this, and I cannot come up with a good solution to this. The problem is that there's no good way for Brainstem to actually know (that I'm aware of) that I didn't want to pass an argument to that lambda.

The other option is to refactor any lambda's that are interacting w/Brainstem to include an optional arg. e.g., rewrite that scope as scope :current, ->(current=true) { where(:current => current) }. I don't like this because it requires somebody interacting with Brainstem to refactor their code because of its internal behavior (leaky abstraction).

If there's some Ruby magic that would allow Brainstem to introspect the scope and see if it's a lambda that accepts 0 arguments (and then just call scope = scope.send(filter_name) in PresenterCollection.run_filters) that would be best, I think. I haven't figured out if that's possible yet, but I'll look into it.

I've been messing around with scope.method(filter_name) but with no luck. .arity always returns -1 and .parameters is also not helpful. Hoping there's some other type of introspection that could help here.

Ints cast as strings

In your README example:
widgets: {
"10": {
id: "10",
name: "disco ball",
feature_ids: ["5"],
popularity: 85,
location_id: "2"
},

"2": {
  id: "2",
  name: "flubber",
  feature_ids: ["6", "12"],
  popularity: 100,
  location_id: "2"
}

}

Is it possible to have *_id's as ints so that this would render as below? This would keep it consistent data types.

widgets: {
"10": {
id: 10,
name: "disco ball",
feature_ids: [5],
popularity: 85,
location_id: 2
},

"2": {
  id: 2,
  name: "flubber",
  feature_ids: [6, 12],
  popularity: 100,
  location_id: 2
}

}

Brainstem and rails 4.1.7 / 4.2.0beta

Hi,

I am trying to use brainstem for an api in a rails 4.2.0beta project but when I load the page I get a no method error - "undefined method 'reflections' for #". The same happens when I downgrade to 4.1.7. Downgrading to 4.0.11 works as expected with no error. What version of rails is supported in current version of brainstem gem?

example: https://github.com/eoinodrisc/brain

Thanks

Release Version 0.2.7

Current version of the gem is 0.2.6.1. What do you think, @shirish-pampoorickal, of releasing a 0.2.7 now because we dropped support for Ruby versions less than 2.1.10?

Then, when the API doc generation goes in, we can release 0.3, or maybe even 1.0.

Specs fail on Rails 4.2

One of the issues is that ActiveRecord::Reflection::ClassMethods::reflections has been changed to use string keys rather than symbols, which breaks https://github.com/mavenlink/brainstem/blob/master/lib/brainstem/presenter_collection.rb#L185) A solution would be to use reflect_on_association instead which works with both symbols and strings (and prefers symbols, according to the docs).

 association = model.class.reflect_on_association(v.method_name)

After the above change is made there are 3 additional spec failures. Further investigation is needed.

Conditionally Disable Count from Being Returned

For large tables including the count can have very large performance implications:

SELECT COUNT(id) FROM something; -- Runtime: 5507.556ms
SELECT * FROM something LIMIT 50 OFFSET 0; -- Runtime: 0.193 ms

It would be great to be able to send in an option to disable the count entirely.

Restrict includes to specific filters?

I got a user with posts. Some posts are not published.

If I do user.json?include=posts this will give me all posts for that user. Is it possible to restrict it to just posts that are published?

Best,
Vinny

Trying to upgrade gem to work with Rails 4

I've been trying to upgrade the gem to work with Rails 4, and after bumping the Active Record version in the gemspec, I've run into a few issues.

Firstly, there is a failing spec:

Failures:

  1) Brainstem::PresenterCollection#presenting filters without blocks can use filters without lambdas in the presenter or model, but behaves strangely when false is given
     Failure/Error: result[:workspaces].keys.should eq(%w[2 4])

       expected: ["2", "4"]
            got: ["2", "4", "6"]

       (compared using ==)
     # ./spec/brainstem/presenter_collection_spec.rb:422:in `block (5 levels) in <top (required)>'

I'm not entirely sure what this spec is doing, I don't know if it's necessary either. It seems to be testing that the user can call a scope without passing a lambda, but I believe lambdas are required by law now.

I assume the failure occurs because the :numeric_filter defined in spec_helpers/db.rb uses invalid syntax:

scope :numeric_description, where(:description => ["1", "2", "3"])

I can make the test work by changing the scope to:

scope :numeric_description, -> description { where(:description => ["1", "2", "3"]) }

Secondly there are several deprecation warnings regarding the use of Model.scoped. I can make them go away by replacing them all with Model.where(nil). I dunno if that's the right way to go about it though.

Finally Rspec throws the following warning:

DEPRECATION: RSpec::Core::Configuration#backtrace_clean_patterns is deprecated. Use RSpec::Core::Configuration#backtrace_exclusion_patterns instead. Called from /home/stephen/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'.

For the life of me I can't figure out where this is coming from, perhaps it's not relevant?

Pass in variable into presenter

Url: /api/v1/videos?auth_token=11112323432432

In controllors/api/v1/videos
I can easily get the user from the auth_token. I want to then make it available in the presenter, ideally via render :json => present("videos") { videos, :extra => user }

Is it possible to pass variables to presenter?
render :json => present("videos") { Widget.visible_to(current_user) } does not work in this case, as in the presenter, I want to show different information, depending on the user

I am currently storing the user_id in a thread in a controller:
Thread.current[:user_id] = 1

and accessing that in the presenter, but this is far less than ideal.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.