phoet / asin

:books: :package: Amazon Simple INterface - Support for ItemLookup, SimilarityLookup, Search, BrowseNode and Cart Operations
http://asin.herokuapp.com/
167 stars 59 forks source link

undefined method `options' for nil:NilClass #19

Closed ychaker closed 11 years ago

ychaker commented 11 years ago

I'm having a weird issue.

I have the following file in the lib folder of my Rails App:

require 'asin'

module YoofersAsin
  # set up a client to talk to the AWS REST API
  @client = ASIN::Client.instance

  def self.search keywords, index = :All, response_group = :Large
    begin
      @client.search(:Keywords => keywords, :SearchIndex => index, :ResponseGroup => response_group)
    rescue HTTPClient::ConnectTimeoutError
      []
    end
  end

  def self.format_result_for_dropdown results
    results.map { |item| { title: item.title, price: item.formatted_amount, image_url: item.image_url, url: item.details_url } }
  end

  # as long as the max is less than the min, it can be ignored
  def self.limit_price_range results, min = 100, max = 200
    if max <= min
      results.select { |each| each.amount >= min }
    else
      results.select { |each| each.amount >= min && each.amount <= max }
    end
  end

  def self.autocomplete keywords, index = :All, response_group = :Large
    results = self.search(keywords, index, response_group)
    results = self.limit_price_range results
    self.format_result_for_dropdown results
  end
end

require 'hashie'
module ASIN
  class SimpleItem
    def formatted_amount
      @raw.ItemAttributes!.ListPrice!.FormattedPrice
    end
  end
end

I'm calling it from a controller like this:

class AmazonSearchController < ApplicationController
  respond_to :json

  def index
     @response = YoofersAsin.autocomplete(params[:term])
     respond_with(@response)
  end
end

When I call the index action of the controller I get the following error:

undefined method `options' for nil:NilClass

lib/yoofers_asin.rb:9:in `search'
lib/yoofers_asin.rb:29:in `autocomplete'
app/controllers/amazon_search_controller.rb:5:in `index'

the full trace is:

confiture (0.1.2) lib/confiture/configuration.rb:152:in `method_missing'
confiture (0.1.2) lib/confiture/configuration.rb:123:in `blank?'
confiture (0.1.2) lib/confiture/configuration.rb:103:in `block in valid?'
confiture (0.1.2) lib/confiture/configuration.rb:103:in `each'
confiture (0.1.2) lib/confiture/configuration.rb:103:in `none?'
confiture (0.1.2) lib/confiture/configuration.rb:103:in `valid?'
confiture (0.1.2) lib/confiture/configuration.rb:95:in `validate!'
asin (1.1.0) lib/asin/client.rb:360:in `call'
asin (1.1.0) lib/asin/client.rb:196:in `search'
lib/yoofers_asin.rb:9:in `search'
lib/yoofers_asin.rb:29:in `autocomplete'
app/controllers/amazon_search_controller.rb:5:in `index'
actionpack (3.2.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.8) lib/active_support/callbacks.rb:447:in `_run__2410238790327027604__process_action__3423452464221231008__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
newrelic_rpm (3.5.0.1) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:34:in `block in process_action'
newrelic_rpm (3.5.0.1) lib/new_relic/agent/instrumentation/controller_instrumentation.rb:268:in `block in perform_action_with_newrelic_trace'
newrelic_rpm (3.5.0.1) lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
newrelic_rpm (3.5.0.1) lib/new_relic/agent/instrumentation/controller_instrumentation.rb:263:in `perform_action_with_newrelic_trace'
newrelic_rpm (3.5.0.1) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:33:in `process_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.8) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:600:in `call'
omnicontacts (0.2.2) lib/omnicontacts/middleware/base_oauth.rb:41:in `call'
omnicontacts (0.2.2) lib/omnicontacts/middleware/base_oauth.rb:41:in `call'
omnicontacts (0.2.2) lib/omnicontacts/middleware/base_oauth.rb:41:in `call'
omnicontacts (0.2.2) lib/omnicontacts/builder.rb:27:in `call'
rack-timeout (0.0.3) lib/rack/timeout.rb:16:in `block in call'
/Users/ychaker/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
rack-timeout (0.0.3) lib/rack/timeout.rb:16:in `call'
newrelic_rpm (3.5.0.1) lib/new_relic/rack/error_collector.rb:8:in `call'
newrelic_rpm (3.5.0.1) lib/new_relic/rack/browser_monitoring.rb:12:in `call'
newrelic_rpm (3.5.0.1) lib/new_relic/rack/developer_mode.rb:24:in `call'
mongoid (3.0.10) lib/rack/mongoid/middleware/identity_map.rb:34:in `call'
client_side_validations (3.2.1) lib/client_side_validations/middleware.rb:21:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/cookies.rb:339:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__705490732763847319__call__2964165817004619242__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Users/ychaker/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/ychaker/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/ychaker/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

The weird part is that if I go to the rails console and call the same method as I do in the index action, I get the results just fine.

My initializer looks like this:

ASIN::Configuration.configure do |config|
  config.secret         = "***********"
  config.key            = "************"
  config.associate_tag  = "*********"
end

require 'httpi'
HTTPI.adapter = :httpclient
HTTPI.logger  = Rails.logger

I'm running ruby '1.9.3' and rails '3.2.8'

So, 'rails c' works, 'rails s' does not! very odd!

Any ideas?

phoet commented 11 years ago

@ychaker i think that your module get's loaded before the initializer is run. do not use an i-var in the module. use a method or something else to access the instance, so that it won't be created unless you access your code.

i will patch confiture to show a better error-message when the config is not initialized.

phoet commented 11 years ago

i pushed a new version of confiture that should raise a better error in your corner-case.

phoet commented 11 years ago

@ychaker bye the way, thx for sending me your amazon secret key :sunglasses:

ychaker commented 11 years ago

Thanks for the quick response. And yeah, i notice too late the fact that i didn't remove the access key/secret. I did edit the response and I trust you'll do the responsible thing with it :)

I changed my lib file to match more the example you have on https://github.com/phoet/asin_web:

require 'asin'

class YoofersAsin

  def initialize
    # set up a client to talk to the AWS REST API
    @client = ASIN::Client.instance
  end

  def search keywords, index = :All, response_group = :Large
    begin
      @client.search(:Keywords => keywords, :SearchIndex => index, :ResponseGroup => response_group)
    rescue HTTPClient::ConnectTimeoutError
      []
    end
  end

  def format_result_for_dropdown results
    results.map { |item| { title: item.title, price: item.formatted_amount, image_url: item.image_url, url: item.details_url } }
  end

  # as long as the max is less than the min, it can be ignored
  def limit_price_range results, min = 100, max = 200
    if max <= min
      results.select { |each| each.amount >= min }
    else
      results.select { |each| each.amount >= min && each.amount <= max }
    end
  end

  def autocomplete keywords, index = :All, response_group = :Large
    results = search(keywords, index, response_group)
    results = limit_price_range results
    format_result_for_dropdown results
  end
end

require 'hashie'
module ASIN
  class SimpleItem
    def formatted_amount
      @raw.ItemAttributes!.ListPrice!.FormattedPrice
    end
  end
end
class AmazonSearchController < ApplicationController
  respond_to :json

  def index
     @response = YoofersAsin.new.autocomplete(params[:term])
     respond_with(@response)
  end
end

and i'm still getting the same error...

what am I missing?

phoet commented 11 years ago

i still think that it's a load order issue.

try putting some puts statements into the files to see which one get's loaded first.

ychaker commented 11 years ago

i put some puts statements and here's the output:

bash-3.2$ rails s
Loaded
=> Booting WEBrick
=> Rails 3.2.8 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
HELLOOO!!! IM THE INITIALIZER
[2012-11-01 19:39:36] INFO  WEBrick 1.3.1
[2012-11-01 19:39:36] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin11.3.0]
[2012-11-01 19:39:36] INFO  WEBrick::HTTPServer#start: pid=7984 port=3000
HELLO! IM THE LIB FILE

Started GET "/amazon_search" for 127.0.0.1 at 2012-11-01 19:39:46 +0200
Processing by AmazonSearchController#index as HTML

.....

so you can see that the initializer is loading first and i get the following error:

you are working on an empty configuration. run configuration code first!

i added this too to try:

def initialize
  # set up a client to talk to the AWS REST API
  @client = ASIN::Client.instance
  @client.configure({
    secret:         "redacted",
    key:            "redacted",
    associate_tag:  "redacted"
  })
end

and now i get this error back:

undefined method `options' for nil:NilClass

What gives?

phoet commented 11 years ago

this should work:

def initialize
  ASIN::Configuration.configure do |config|
    config.secret         = "***********"
    config.key            = "************"
    config.associate_tag  = "*********"
  end
  @client = ASIN::Client.instance
end

i still don't have a clue why the configuration is not set properly...

ychaker commented 11 years ago

so you wouldn't believe what got it to work!

changing this:

group :production do
  gem 'thin'
end

to this:

gem 'thin'

i was only using thin in the production group. i moved it out and made it available in development as well and it started working.

I don't know if it's a webrick problem, some silly clash somewhere, or just the world hating me. :'(

phoet commented 11 years ago

:cry: