Topic: Missing methods when running Mechanize inside a rake task

Ok so here's the code to my ruby file which works perfectly when executed from a desktop folder with "ruby script.rb" command:

    require 'rubygems'
    require 'mechanize'
    require 'hpricot'
    require 'open-uri'
    require 'json'
    
    agent = Mechanize.new
    page  = agent.get('https://kdp.amazon.com/self-publishing/signin')
    inner_page = page.link_with(:dom_class => 'new-signInButton').click
    
    form = inner_page.forms.first
    
    form.email = "email@gmail.com"
    form.password = "password"
    
    inner_page = agent.submit(form)
    
    reports_page = Mechanize::Page
    inner_page.links.each do |link|
      reports_page = link.click if link.text == "Reports"
    end
    
    scraper = Mechanize.new
    #scraper.cookie_jar = Mechanize::CookieJar
    agent.cookies.each do |cookie|
      scraper.cookie_jar.add!(cookie)
    end
    
    url = "[url]https://kdp.amazon.com/self-publishing/reports/transactionSummary?_=[/url]" + Time.now.to_i.to_s + rand(9).to_s + rand(9).to_s + rand(9).to_s + "&marketplaceID=ATVPDKIKX0DER"
    
    response = scraper.get(url)
    
    results = JSON.parse(response.body)
    
    output = ""
    results["aaData"].each_with_index do |entry, i|
      if i != results["aaData"].length - 1
        output << entry[1] + "<br>"
      else
        output << "<center>TOTAL SOLD: #{entry[12]}</center>"
      end
    end
    
    myStr = output
    aFile = File.new("afterlogin.html", "w")
    aFile.write(myStr)
    aFile.close

As soon as I place it inside of a app.rake file in /lib/tasks/ inside of my rails app

    #require stuff here

    task('app:scrape' => :environment) do
    
    #code here
    
    end

It stops working:

kindle > rake app:scrape
    WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.7.3
    rake aborted!
    undefined method `dom_class' for #<Mechanize::Page::Link:0x0000010218f080>
   
    Tasks: TOP => app:scrape
    (See full trace by running task with --trace)

Curiously, if you re-structure the code to use a more primitive loop which then finds the link with the appropriate href (rather than finding it by dom_class) and then calling .click on it, everything goes smoothly up until the part where you call .add!(cookie) on cookie_jar of the custom request url.

The error seems to be that add!() method doesn't exist. Simply .add exists, but that's not exactly what I need, I don't think. << doesn't exist either.

It has to be inside of a rake task - It's gonna be a daily scrape for multiple users in my database.

What's going on? Why do they exist outside of rails\rake but don't otherwise? I'm completely baffled.

PS. Running OSX, if it matters.



Thanks in advance for taking a look... smile

Re: Missing methods when running Mechanize inside a rake task

My one of close friend wants to set his career as motorcycle mechanic in a garage and I am here wanted to know about the salary of motorcycle mechanic. Please update me with complete information.

motorcycle mechanic