class GScholar::Utils::Fetcher

Public Class Methods

new() click to toggle source
# File lib/gscholar/utils/fetcher.rb, line 7
def initialize
  @cache = {}
  @agent = Mechanize.new
  @agent.pluggable_parser['text/plain'] = TextPlainParser
  # Without changing user-agent, google will not return utf-8 page
  @agent.user_agent_alias = 'Mac Safari'
  hack
end

Public Instance Methods

fetch(url) click to toggle source
# File lib/gscholar/utils/fetcher.rb, line 21
def fetch(url)
  begin
    @cache[url] ||= @agent.get(url)
  rescue Mechanize::ResponseCodeError => e
    case e.response_code
    when '403'
      reset
      retry
    else
      raise
    end
  end
end
reset() click to toggle source
# File lib/gscholar/utils/fetcher.rb, line 16
def reset
  @agent.reset
  hack
end

Private Instance Methods

hack() click to toggle source

hack to enable BibTeX download

# File lib/gscholar/utils/fetcher.rb, line 37
def hack
  scisig = @agent.get('http://scholar.google.com/scholar_settings').
             form_with(:action => "/scholar_setprefs").field_with(:name => 'scisig').value
  @agent.get("http://scholar.google.com/scholar_setprefs?scisig=#{scisig}&num=20&scis=yes&scisf=4&instq=&save=")
end