mbrowser 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0173dcefeb282ea61f419fe551c4bbaf6b9e27ab
4
- data.tar.gz: 7080c42159d090d1051ae5e097722284f3b3774a
3
+ metadata.gz: 5749be3be28a438f76b0f59fc8860200a716a6c7
4
+ data.tar.gz: d7b84f26567c4bee2440471ec95a778b66da6de8
5
5
  SHA512:
6
- metadata.gz: a07b8cee604b44b6f62acdc64e40abf042c5461f1e2535cb84c53c30c011ebc7fec59c063ca38bd66c39397973512cdf4ec3effdc081184f34e3efdcbb206198
7
- data.tar.gz: 417718863c4d50ae6513f597dee03b118b5d78c3df4f6c2de46cbaa40a18b5b3c0f9d61828235349e281651e238e3471d1375aa202bf07f6bd2e16f1c728de80
6
+ metadata.gz: f7444681bf7fe9b98613c62a983489085d86bfd2d4a13075318d47736cd0fba83591c1db7c7835080d28d21942f4e0d1526f1e7c41abe9ba9ea4bd26e7fc0185
7
+ data.tar.gz: 2f05805a3c8e7b5ec0f045293136cdbf8136247bc55cae8ba982199b225554ff546756001e964a8c1ba9a03ec6919efcca24445368144ffd2417b01ec82ac501
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+
4
+ # Specify your gem's dependencies in mbrowser.gemspec
5
+ gemspec
6
+
7
+ gem 'curb'
8
+ gem 'nokogiri'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 lvpython
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Mbrowser
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mbrowser'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mbrowser
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require "mbrowser/version"
2
+ require "mbrowser/browser"
3
+ module Mbrowser
4
+ #Demo
5
+ #Mbrowser::Browser.new(method: "get", url: "https://apps.yottaa.com")
6
+ end
@@ -0,0 +1,57 @@
1
+ require 'mbrowser/cookie'
2
+ require 'uri'
3
+ require 'curl'
4
+ class Curl::Easy
5
+ attr_accessor :domain
6
+ end
7
+ module Mbrowser
8
+ class Base
9
+
10
+ USER_AGENT = { "firefox" => "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:16.0) Gecko/20100101 Firefox/16.0"
11
+ }
12
+ ACCEPT = {"html+xml" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
13
+ "json" => "application/json, text/javascript, */*; q=0.01"}
14
+ ACCEPT_ENCODING = {"default" => "", "gzip" => "gzip,deflate,sdch"}
15
+
16
+ def initialize(attrs = {})
17
+ domain = attrs[:host]
18
+ my_host = URI.parse attrs[:url]
19
+ domain ||= my_host.host
20
+ @is_gzip = true
21
+ @curl = Curl::Easy.new(attrs[:url])
22
+ @curl.domain = domain
23
+ @curl.follow_location = attrs[:follow_location] || true
24
+ @curl.enable_cookies = true
25
+ @curl.post_body = attrs[:post_body] if attrs[:post_body]
26
+ @curl.cookies = Mbrowser::Cookie.export_cookies domain
27
+ @curl.dns_cache_timeout= attrs[:dns_timeout] || 60
28
+ @curl.max_redirects = attrs[:redirect_num] || 10
29
+ @curl.use_ssl = attrs[:use_ssl] || Curl::CURL_USESSL_TRY
30
+ @curl.ssl_verify_host = attrs[:verify_host] || false
31
+ @curl.ssl_verify_peer = attrs[:verify_peer] || false
32
+ @curl.useragent = attrs[:user_agent] || USER_AGENT["firefox"]
33
+
34
+ custom_headers = {"Accept-Encoding" => ACCEPT_ENCODING["gzip"]}
35
+ custom_headers.merge!({"Referer" => attrs[:header_referer]}) if attrs[:header_referer]
36
+ if attrs[:header_accept]
37
+ if attrs[:header_accept] == "json"
38
+ custom_headers.merge!({"X-Requested-With" => "XMLHttpRequest"})
39
+ end
40
+ custom_headers.merge!({"Accept" => ACCEPT[attrs[:header_accept]]})
41
+ end
42
+
43
+ if attrs[:accept_encoding]
44
+ custom_headers.merge!({"Accept-Encoding" => ACCEPT_ENCODING[attrs[:accept_encoding]]})
45
+ if attrs[:accept_encoding] != "gzip"
46
+ @is_gzip = false
47
+ end
48
+ end
49
+ custom_headers.merge!({"X-CSRF-Token" => attrs[:header_csrf]}) if attrs[:header_csrf]
50
+ custom_headers.merge!({"Host" => domain })
51
+ custom_headers.merge!({"X-CSRF-Token" => attrs[:host]}) if attrs[:host]
52
+ custom_headers.merge! attrs[:other_headers] if attrs[:other_headers]
53
+ @curl.headers = custom_headers.map{|key,value| "#{key}: #{value}"}
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,84 @@
1
+ require 'mbrowser/base'
2
+ require 'mbrowser/cookie'
3
+ require 'zlib'
4
+ require 'nokogiri'
5
+ require 'json'
6
+
7
+ module Mbrowser
8
+ class Browser < Mbrowser::Base
9
+ RESERVED_CHARS=['[',']','+','=','/','#','&',':']
10
+ METHOD_GROUPS = ["get", "post", "put", "delete"]
11
+ HTML = "html"
12
+ JSON = "json"
13
+ attr_accessor :curl
14
+
15
+ def initialize(attrs = {})
16
+ super(attrs)
17
+ @method = attrs[:method]
18
+ @payload = attrs[:payload] || {}
19
+ @_response = ""
20
+ raise "unsupport method #{@method}" unless METHOD_GROUPS.include? @method
21
+ end
22
+
23
+ def open
24
+ unless @payload.empty?
25
+ post_data = @payload.map{|key,value| "#{encode_data(key)}=#{encode_data(value)}"}.join("&")
26
+ @curl.send("http_#{@method}", post_data)
27
+ else
28
+ @curl.send("http_#{@method}")
29
+ end
30
+ Mbrowser::Cookie.import_cookies @curl
31
+ self
32
+ end
33
+
34
+ def response_code
35
+ @curl.response_code
36
+ end
37
+
38
+ def header_str
39
+ @curl.header_str || ""
40
+ end
41
+
42
+ def body_str
43
+ if @is_gzip
44
+ begin
45
+ gz = Zlib::GzipReader.new(StringIO.new(@curl.body_str))
46
+ responses = gz.read
47
+ rescue
48
+ @curl.body_str
49
+ end
50
+ else
51
+ @curl.body_str
52
+ end
53
+ end
54
+
55
+ def formatted_response format = HTML
56
+ if format == JSON
57
+ @_response = JSON.parse(body_str)
58
+ elsif format == HTML
59
+ @_response = Nokogiri::HTML(body_str)
60
+ end
61
+ end
62
+
63
+ def pretty_response format = HTML
64
+ formatted_response format
65
+ @_response
66
+ end
67
+
68
+ private
69
+
70
+ def encode_data(value)
71
+ value.to_s.strip.split('').map{ |char|
72
+ if RESERVED_CHARS.include? char
73
+ "%#{char.unpack('H*')[0].upcase}"
74
+ else
75
+ char
76
+ end
77
+ }.reduce(:+)
78
+ end
79
+
80
+ def header_token_value(token)
81
+ header_str.split("\r\n").map{|v| v.split(": ")}.select{|item| item[0].downcase==token.downcase}
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,56 @@
1
+ require 'yaml'
2
+ module Mbrowser
3
+ class Cookie
4
+ $session_cookies = {}
5
+ COOKIE_FILE = '/tmp/mbrowser_cookie.yml'
6
+ class << self
7
+ def load_cookie_from_disk
8
+ $session_cookies = YAML::load_file(COOKIE_FILE) rescue {}
9
+ end
10
+
11
+ def dump_cookie_to_file
12
+ File.open(COOKIE_FILE, 'w') {|f| f.write $session_cookies.to_yaml }
13
+ end
14
+
15
+ def export_cookies domain
16
+ load_cookie_from_disk if $session_cookies.empty?
17
+ cookie_store = $session_cookies.select{ |suffix_domain,value| domain.include? suffix_domain}.inject({}){|m,n| m.merge!(n[1]);m}
18
+ cookies_str = cookie_store.clone.keys.map{ |key|
19
+ "#{cookie_store[key].empty? ? "" : "#{key}=#{cookie_store[key]};"}"
20
+ }.join(" ")
21
+ cookies_str
22
+ end
23
+
24
+ def import_cookies curl
25
+ cookie_headers= curl.header_str.split("\r\n").map{|v| v if v =~ /^Set-Cookie:.*/ }.compact
26
+
27
+ cookie_hashs = cookie_headers.map do |v|
28
+ v = v[11..-1]
29
+ cookie_hash = v.split(";").inject({}){|m,item| m.merge!({item.split("=")[0].strip.to_sym=>item.split("=")[1..-1].join("=").to_s});m}
30
+ domain = cookie_hash[:domain]
31
+ domain ||= curl.domain
32
+ cookie_hash.delete(:domain)
33
+ cookie_hash.delete(:expires)
34
+ cookie_hash.delete(:path)
35
+ cookie_hash.delete(:HttpOnly)
36
+ $session_cookies[domain] ||= {}
37
+ cookie_hash.each do |key,value|
38
+ if value.to_s.downcase.strip != "deleted"
39
+ $session_cookies[domain][key] = value.to_s
40
+ else
41
+ $session_cookies[domain].delete key
42
+ end
43
+ end
44
+ end
45
+ dump_cookie_to_file
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ class NilClass
52
+ def map
53
+ end
54
+ def each
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Mbrowser
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mbrowser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mbrowser"
8
+ spec.version = Mbrowser::VERSION
9
+ spec.authors = ["lvpython"]
10
+ spec.email = ["lvpython@gmail.com"]
11
+ spec.description = %q{CURL}
12
+ spec.summary = %q{CURL}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", ">= 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "curb", ">= 0.8.3"
24
+ spec.add_dependency "nokogiri", ">= 1.6.0"
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbrowser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - lvpython
@@ -72,7 +72,18 @@ email:
72
72
  executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
- files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/mbrowser.rb
82
+ - lib/mbrowser/base.rb
83
+ - lib/mbrowser/browser.rb
84
+ - lib/mbrowser/cookie.rb
85
+ - lib/mbrowser/version.rb
86
+ - mbrowser.gemspec
76
87
  homepage: ''
77
88
  licenses:
78
89
  - MIT