Sha256: eb9fb9b74b3fd3f4f29cf3b1fad3f4251541e179481d0a6a651b233e8bdb8a2b

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require 'mechanize'
require 'open-uri'

module GScraper
  #
  # Returns the supported GScraper User-Agent Aliases.
  #
  def GScraper.user_agent_aliases
    WWW::Mechanize::AGENT_ALIASES
  end

  #
  # Returns the GScraper User-Agent
  #
  def GScraper.user_agent
    @user_agent ||= nil
  end

  #
  # Sets the GScraper User-Agent to the specified _agent_.
  #
  def GScraper.user_agent=(agent)
    @user_agent = agent
  end

  #
  # Opens the _uri_ with the given _opts_. The contents of the _uri_ will be
  # returned.
  #
  #   GScraper.open('http://www.hackety.org/')
  #
  #   GScraper.open('http://tenderlovemaking.com/',
  #     :user_agent_alias => 'Linux Mozilla')
  #   GScraper.open('http://www.wired.com/', :user_agent => 'the future')
  #
  def GScraper.open(uri,opts={})
    headers = {}

    if opts[:user_agent_alias]
      headers['User-Agent'] = WWW::Mechanize::AGENT_ALIASES[opts[:user_agent_alias]]
    elsif opts[:user_agent]
      headers['User-Agent'] = opts[:user_agent]
    elsif GScraper.user_agent
      headers['User-Agent'] = GScraper.user_agent
    end

    return Kernel.open(uri,headers)
  end

  #
  # Creates a new Mechanize agent with the given _opts_.
  #
  #   GScraper.http_agent
  #   GScraper.http_agent(:user_agent_alias => 'Linux Mozilla')
  #   GScraper.http_agent(:user_agent => 'wooden pants')
  #
  def GScraper.http_agent(opts={})
    agent = WWW::Mechanize.new

    if opts[:user_agent_alias]
      agent.user_agent_alias = opts[:user_agent_alias]
    elsif opts[:user_agent]
      agent.user_agent = opts[:user_agent]
    elsif GScraper.user_agent
      agent.user_agent = GScraper.user_agent
    end

    return agent
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gscraper-0.1.4 lib/gscraper/gscraper.rb
gscraper-0.1.5 lib/gscraper/gscraper.rb