Sha256: 7cdcd32acbb76081b6199e587aac03fdf73518ab8652f1fb8b69f07a3894ca72

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'mechanize'
require 'open-uri'

module RPaste
  #
  # Returns the RPaste user-agent
  #
  def RPaste.user_agent
    @user_agent
  end

  #
  # Sets the RPaste user-agent to the specified _agent_.
  #
  def RPaste.user_agent=(agent)
    @user_agent = agent
  end

  #
  # Opens the _uri_ with the given _opts_. The contents of the _uri_ will
  # be returned.
  #
  #   RPaste.open('http://www.hackety.org/')
  #   RPaste.open('http://tenderlovemaking.com/',
  #     :user_agent_alias => 'Linux Mozilla')
  #   RPaste.open('http://www.wired.com/', :user_agent => 'the future')
  #
  def RPaste.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 RPaste.user_agent
      headers['User-Agent'] = RPaste.user_agent
    end

    return Kernel.open(uri,headers)
  end

  #
  # Creates a new Mechanize agent with the given _opts_.
  #
  #   RPaste.http_agent
  #   RPaste.http_agent(:user_agent_alias => 'Linux Mozilla')
  #   RPaste.http_agent(:user_agent => 'wooden pants')
  #
  def RPaste.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 RPaste.user_agent
      agent.user_agent = RPaste.user_agent
    end

    return agent
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rpaste-0.0.9 lib/rpaste/rpaste.rb