Class: Mbrowser::Browser

Inherits:
Base
  • Object
show all
Defined in:
lib/mbrowser/browser.rb

Constant Summary collapse

RESERVED_CHARS =
['[',']','+','=','/','#','&',':']
METHOD_GROUPS =
["get", "post", "put", "delete"]
HTML =
"html"
JSON =
"json"

Constants inherited from Base

Mbrowser::Base::ACCEPT, Mbrowser::Base::ACCEPT_ENCODING, Mbrowser::Base::USER_AGENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Browser

Returns a new instance of Browser.



15
16
17
18
19
20
21
# 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

Instance Attribute Details

#curlObject

Returns the value of attribute curl.



13
14
15
# File 'lib/mbrowser/browser.rb', line 13

def curl
  @curl
end

Instance Method Details

#body_strObject



42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



55
56
57
58
59
60
61
# 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_strObject



38
39
40
# File 'lib/mbrowser/browser.rb', line 38

def header_str
	@curl.header_str || ""
end

#openObject



23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



63
64
65
66
# File 'lib/mbrowser/browser.rb', line 63

def pretty_response format = HTML      
  formatted_response format
  @_response
end

#response_codeObject



34
35
36
# File 'lib/mbrowser/browser.rb', line 34

def response_code
	@curl.response_code
end