class Mbrowser::Browser

Constants

HTML
JSON
METHOD_GROUPS
RESERVED_CHARS

Attributes

curl[RW]

Public Class Methods

new(attrs = {}) click to toggle source
Calls superclass method Mbrowser::Base::new
# File lib/mbrowser/browser.rb, line 15
  def initialize(attrs = {})
          super(attrs)
          @method = attrs[:method]
          @payload = attrs[:payload] || {}
@_response = ""
          raise "unsupport method #{@method}" unless METHOD_GROUPS.include? @method
  end

Public Instance Methods

body_str() click to toggle source
# File lib/mbrowser/browser.rb, line 42
  def body_str
if @is_gzip
  begin
            gz = Zlib::GzipReader.new(StringIO.new(@curl.body_str))    
    responses = gz.read
  rescue
    @curl.body_str
  end
else
  @curl.body_str
end
  end
formatted_response(format = HTML) click to toggle source
# File lib/mbrowser/browser.rb, line 55
def formatted_response format = HTML
  if format == JSON
    @_response = JSON.parse(body_str)
  elsif format == HTML
    @_response = Nokogiri::HTML(body_str)
  end
end
header_str() click to toggle source
# File lib/mbrowser/browser.rb, line 38
def header_str
        @curl.header_str || ""
end
open() click to toggle source
# File lib/mbrowser/browser.rb, line 23
  def open
          unless @payload.empty?
  post_data = @payload.map{|key,value| "#{encode_data(key)}=#{encode_data(value)}"}.join("&")
    @curl.send("http_#{@method}", post_data)
          else
                   @curl.send("http_#{@method}")
          end
          Mbrowser::Cookie.import_cookies @curl
self
  end
pretty_response(format = HTML) click to toggle source
# File lib/mbrowser/browser.rb, line 63
def pretty_response format = HTML      
  formatted_response format
  @_response
end
response_code() click to toggle source
# File lib/mbrowser/browser.rb, line 34
def response_code
        @curl.response_code
end

Private Instance Methods

encode_data(value) click to toggle source
# File lib/mbrowser/browser.rb, line 70
def encode_data(value)
    value.to_s.strip.split('').map{ |char|
       if RESERVED_CHARS.include? char
         "%#{char.unpack('H*')[0].upcase}"
       else
         char
       end
    }.reduce(:+)
end
header_token_value(token) click to toggle source
# File lib/mbrowser/browser.rb, line 80
def header_token_value(token)
  header_str.split("\r\n").map{|v| v.split(": ")}.select{|item| item[0].downcase==token.downcase}
end