Closed ychaker closed 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.
i pushed a new version of confiture that should raise a better error in your corner-case.
@ychaker bye the way, thx for sending me your amazon secret key :sunglasses:
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?
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.
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?
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...
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. :'(
:cry:
I'm having a weird issue.
I have the following file in the lib folder of my Rails App:
I'm calling it from a controller like this:
When I call the index action of the controller I get the following error:
the full trace is:
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:
I'm running ruby '1.9.3' and rails '3.2.8'
So, 'rails c' works, 'rails s' does not! very odd!
Any ideas?