views:

1915

answers:

1

I'd like to store and update blogger labels to datastore in GAE. When I run that code, I get this error:

javax.servlet.ServletContext log: Application Error /base/data/home/apps/yet-another-problem/1.334886515480009498/WEB-INF/gems/gems/sinatra-0.9.2/lib/sinatra/base.rb:45:in each': undefined method bytesize' for #<Hash:0x86684c> (NoMethodError)

The Code

class Labels
   class LabelData
    include Bumble
    ds :blog_element_labels
   end

  def update
    response = URLFetch.get($label_url)
    result = response.to_s
    result_headless = result.gsub("listLabels(",'')
    pure_result = result_headless.gsub(");",'')
    json_to_yaml = YAML::load(pure_result)['entry']['category']

    json_to_yaml.each do |label|
    @label = LabelData.find(:blog_element_labels => label['term'])
    @label = LabelData.create(:blog_element_labels => label['term']) if @label.nil?
    end
  end
end

and run by cron job does '/job'

get '/job' do
  @labels = Labels.new
  @labels.update
end

Where is the problem? Please teach me.

But when run cron job first time,label data was stored,even occur that error. Could not update data.

+5  A: 

I think your having the same problem that's been discussed here: http://stackoverflow.com/questions/1117272/error-happens-when-i-try-all-method-in-datamapper

In your case, Sinatra is trying to take the return value of @lavels.update and turn that into a string to display to the user.

Try this to see if it fixes the problem:

get '/job' do
  @labels = Labels.new
  @labels.update
  "Labels Updated"
end

Your return value is now a string, so you shouldn't get the error.

Luke Antins
Thank you vey much !Fix and works !Appreciate your light speed answer.
tknv
@tknv, You should mark this answer as Accepted if it fixed your problem, so Luke gets reputation for it.
Peter Recore
@Peter,Sorry delay,Really hard to find where is and how to accept.I tried to find some button for accept,but not.
tknv
Thanks! You fixed my problem with this too! :)
Justin Bozonier