class SpiderBot::Http::Client

Constants

USER_AGENT

Supported User-Agent

  • Linux Firefox (3.6.1)

  • Linux Konqueror (3)

  • Linux Mozilla

  • Linux Chrome

  • Mac Firefox

  • Mac Mozilla

  • Mac Chrome

  • Mac Safari

  • Mechanize (default)

  • Windows IE 6

  • Windows IE 7

  • Windows IE 8

  • Windows IE 9

  • Windows Mozilla

  • iPhone (3.0)

  • iPad

  • Android

Attributes

conn_build[RW]
connection[RW]

return connection for HttpClient

headers[R]
options[RW]
url[R]

return url for HttpClient

user_agent[R]

return http user_agent for HttpClient

Public Class Methods

new(uri = nil, options = nil) { |self| ... } click to toggle source

Initialize a new HttpClient

@param uri [String] the uri with

@param options [Hash] the options to create a http with configure @option options [String] :header set the http request headers @yield [builder]

@example

http = HttpClient.new

http = HttpClient.new do |http|
  http.user_agent= "Mac Safri"
  http.url= "http://example.com"
end
# File lib/spider_bot/http/client.rb, line 76
def initialize(uri = nil, options = nil, &block)
  @url = uri
  @options = options
  @user_agent ||= USER_AGENT['bot']
  yield self if block_given?
end

Public Instance Methods

builder(&block) click to toggle source
# File lib/spider_bot/http/client.rb, line 83
def builder(&block)
  @conn_build = block
end
get(uri, query = {}, &block) click to toggle source

Handle get request with HttpClient

@param uri [String] URL path for request @param query [Hash] additional query parameters for the URL of the request

# File lib/spider_bot/http/client.rb, line 155
def get(uri, query = {}, &block) 
  request(:get, uri, query, &block)
end
headers=(headers) click to toggle source

Set the headers for HttpClient

@param headers [String] the HttpClient url @return [String]

# File lib/spider_bot/http/client.rb, line 101
def headers=(headers)
  @headers = headers.merge({"User-Agent" => user_agent})
end
post(uri, query = {}, &block) click to toggle source

Handle post request with HttpClient @param (see get)

# File lib/spider_bot/http/client.rb, line 161
def post(uri, query = {}, &block)
  request(:post, uri, query, &block)
end
request(verb, uri, query={}) { |request| ... } click to toggle source

Make request with HttpClient

@param verb [Symbol] verb one of :get, :post, :put, :delete @param uri [String] URL path for request @param query [Hash] additional query parameters for the URL of the request

# File lib/spider_bot/http/client.rb, line 133
def request(verb, uri, query={})
  verb == :get ? query_get = query : query_post = query
  uri = connection.build_url(uri, query_get)

  response = connection.run_request(verb, uri, query_post, headers) do |request|
    yield request if block_given?
  end
  response = Response.new(response)
  
  case response.status
  when 301, 302, 303, 307
    request(verb, response.headers['location'], query)
  when 200..299, 300..399
    response
  end
end
url=(uri) click to toggle source

Set the url for HttpClient

@param uri [String] the HttpClient url

# File lib/spider_bot/http/client.rb, line 91
def url=(uri)
  @conn = nil
  @url = uri
end
user_agent=(name) click to toggle source

Set the user agent for HttpClient

@param name [Symbol] the HttpClient user agent

# File lib/spider_bot/http/client.rb, line 109
def user_agent=(name)
  @user_agent = USER_AGENT[name] || USER_AGENT['bot']
end