user-agent-randomizer 0.1.2 → 0.2.0
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 +4 -4
 - data/README.md +21 -10
 - data/benchmark/generate.rb +26 -0
 - data/lib/user_agent_randomizer.rb +19 -3
 - data/lib/user_agent_randomizer/user_agent.rb +4 -5
 - data/lib/user_agent_randomizer/version.rb +1 -1
 - data/user_agent_randomizer.gemspec +1 -1
 - metadata +3 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: b1da61f53ea46a7db46dcb9c06e1e818a3e7b20f
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 4ea424da7f7426c52ef4521471d29c61900a03ff
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: d06f2088b10ffa1497a4d372951e7af1664d83f5d62563e6999e17ea84a8223c9fdd45b1501d8c95046bda731d2d95fad99cd9eaf166e3375d80b5b44873f934
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 1a15e25a49920f5a63ce3e8273799f2fe7bc0b784da6e2d5d80b1652fe26eb9db7a60c41c3263d5a369b5d42ccc9f4c338ee1390a9dd1b1c97e4ebea443844e6
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -36,23 +36,34 @@ And then execute: 
     | 
|
| 
       36 
36 
     | 
    
         | 
| 
       37 
37 
     | 
    
         
             
            Or install it manually:
         
     | 
| 
       38 
38 
     | 
    
         | 
| 
       39 
     | 
    
         
            -
                $ gem install  
     | 
| 
      
 39 
     | 
    
         
            +
                $ gem install user-agent-randomizer
         
     | 
| 
       40 
40 
     | 
    
         | 
| 
       41 
41 
     | 
    
         
             
            ## Usage
         
     | 
| 
       42 
42 
     | 
    
         | 
| 
       43 
43 
     | 
    
         
             
            The usage is pretty simple. You can fetch a random HTTP User-Agent string from the entire pool as well as one from a specified category (see the list above):
         
     | 
| 
       44 
44 
     | 
    
         | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
       46 
     | 
    
         
            -
            require 'user-agent-randomizer'
         
     | 
| 
      
 45 
     | 
    
         
            +
            Open an IRB and require the gem first of all:
         
     | 
| 
       47 
46 
     | 
    
         | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
     | 
    
         
            -
             
     | 
| 
       50 
     | 
    
         
            -
            # => "AppEngine-Google; (+http://code.google.com/appengine; appid: longbows-hideout)"
         
     | 
| 
      
 47 
     | 
    
         
            +
                irb> require 'user_agent_randomizer'
         
     | 
| 
      
 48 
     | 
    
         
            +
                # => true
         
     | 
| 
       51 
49 
     | 
    
         | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
             
     | 
| 
      
 50 
     | 
    
         
            +
            Fetch a random HTTP User-Agent string from the entire pool:
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                irb> ua_all = UserAgentRandomizer::UserAgent.fetch
         
     | 
| 
      
 53 
     | 
    
         
            +
                # => #<UserAgentRandomizer::UserAgent:0x007fe1f30a6658 @type="crawler", @string="CatchBot/1.0;  http://www.catchbot.com">
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
            Fetch a random HTTP User-Agent for a desktop browser:
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                irb> ua_desktop = UserAgentRandomizer::UserAgent.fetch(type: "desktop_browser")
         
     | 
| 
      
 58 
     | 
    
         
            +
                # => #<UserAgentRandomizer::UserAgent:0x007fe1f2282b58 @type="desktop_browser", @string="Mozilla/4.6 [en] (WinNT; I)">
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
            Retrieve the type and the string from the objects above:
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                irb> puts "UA general -> type: '#{ua_all.type}', string: '#{ua_all.string}'"
         
     | 
| 
      
 63 
     | 
    
         
            +
                # => "UA general -> type: 'crawler', string: 'CatchBot/1.0;  http://www.catchbot.com'"
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
                irb> puts "UA desktop browser -> type: '#{ua_desktop.type}', string: '#{ua_desktop.string}'"
         
     | 
| 
      
 66 
     | 
    
         
            +
                # => "UA desktop browser -> type: 'desktop_browser', string: 'Mozilla/4.6 [en] (WinNT; I)'"
         
     | 
| 
       56 
67 
     | 
    
         | 
| 
       57 
68 
     | 
    
         
             
            ## Contributing
         
     | 
| 
       58 
69 
     | 
    
         | 
| 
         @@ -0,0 +1,26 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #!/usr/bin/env ruby
         
     | 
| 
      
 2 
     | 
    
         
            +
            # coding: utf-8
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/..')
         
     | 
| 
      
 5 
     | 
    
         
            +
            $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            # require 'rubygems'
         
     | 
| 
      
 8 
     | 
    
         
            +
            # require 'bundler'
         
     | 
| 
      
 9 
     | 
    
         
            +
            require 'benchmark'
         
     | 
| 
      
 10 
     | 
    
         
            +
            require 'user_agent_randomizer'
         
     | 
| 
      
 11 
     | 
    
         
            +
            include Benchmark
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            times = ARGV[0] ? ARGV[0].to_i : 100_000
         
     | 
| 
      
 14 
     | 
    
         
            +
            puts "Starting benchmark generating #{times} HTTP User-Agent strings ..."
         
     | 
| 
      
 15 
     | 
    
         
            +
            Benchmark.benchmark(CAPTION, 40, FORMAT, nil) do |x|
         
     | 
| 
      
 16 
     | 
    
         
            +
            	x.report("UserAgent.fetch") do
         
     | 
| 
      
 17 
     | 
    
         
            +
                times.times do
         
     | 
| 
      
 18 
     | 
    
         
            +
                  UserAgentRandomizer::UserAgent.fetch
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
              end
         
     | 
| 
      
 21 
     | 
    
         
            +
              x.report("UserAgent.fetch(type: 'desktop_browser'") do
         
     | 
| 
      
 22 
     | 
    
         
            +
                times.times do
         
     | 
| 
      
 23 
     | 
    
         
            +
                  UserAgentRandomizer::UserAgent.fetch(type: 'desktop_browser')
         
     | 
| 
      
 24 
     | 
    
         
            +
                end
         
     | 
| 
      
 25 
     | 
    
         
            +
              end
         
     | 
| 
      
 26 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -23,12 +23,28 @@ end 
     | 
|
| 
       23 
23 
     | 
    
         | 
| 
       24 
24 
     | 
    
         
             
            UserAgentRandomizer.config do
         
     | 
| 
       25 
25 
     | 
    
         
             
              parameter :user_agent_types
         
     | 
| 
       26 
     | 
    
         
            -
              parameter : 
     | 
| 
      
 26 
     | 
    
         
            +
              parameter :user_agents_hash
         
     | 
| 
      
 27 
     | 
    
         
            +
              parameter :user_agents_array
         
     | 
| 
      
 28 
     | 
    
         
            +
            end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
            if Gem.loaded_specs['user-agent-randomizer'].nil?
         
     | 
| 
      
 31 
     | 
    
         
            +
              gem_path = File.expand_path("../..", __FILE__)
         
     | 
| 
      
 32 
     | 
    
         
            +
            else
         
     | 
| 
      
 33 
     | 
    
         
            +
              gem_path = Gem.loaded_specs['user-agent-randomizer'].full_gem_path
         
     | 
| 
      
 34 
     | 
    
         
            +
            end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
            user_agents_hash = YAML.load_file(File.join(gem_path, 'data', 'user_agents.yml'))
         
     | 
| 
      
 37 
     | 
    
         
            +
            user_agents_array = []
         
     | 
| 
      
 38 
     | 
    
         
            +
            user_agents_hash.each_pair do |type, ua_strings|
         
     | 
| 
      
 39 
     | 
    
         
            +
              ua_strings.each do |ua_string|
         
     | 
| 
      
 40 
     | 
    
         
            +
                user_agents_array << {type: type, string: ua_string}
         
     | 
| 
      
 41 
     | 
    
         
            +
              end
         
     | 
| 
       27 
42 
     | 
    
         
             
            end
         
     | 
| 
       28 
43 
     | 
    
         | 
| 
       29 
44 
     | 
    
         
             
            UserAgentRandomizer.config do
         
     | 
| 
       30 
45 
     | 
    
         
             
              user_agent_types ["crawler", "desktop_browser", "mobile_browser", "console",
         
     | 
| 
       31 
46 
     | 
    
         
             
                  "offline_browser", "email_client", "link_checker", "email_collector",
         
     | 
| 
       32 
     | 
    
         
            -
                  "validator", "feed_reader", "library", "cloud_platform", "other"]
         
     | 
| 
       33 
     | 
    
         
            -
               
     | 
| 
      
 47 
     | 
    
         
            +
                  "validator", "feed_reader", "library", "cloud_platform", "other"]      
         
     | 
| 
      
 48 
     | 
    
         
            +
              user_agents_hash user_agents_hash
         
     | 
| 
      
 49 
     | 
    
         
            +
              user_agents_array user_agents_array
         
     | 
| 
       34 
50 
     | 
    
         
             
            end
         
     | 
| 
         @@ -12,13 +12,12 @@ module UserAgentRandomizer 
     | 
|
| 
       12 
12 
     | 
    
         
             
                def self.fetch(options = {})
         
     | 
| 
       13 
13 
     | 
    
         
             
                  type = options[:type]
         
     | 
| 
       14 
14 
     | 
    
         
             
                  ua_types = UserAgentRandomizer.user_agent_types
         
     | 
| 
       15 
     | 
    
         
            -
                   
     | 
| 
      
 15 
     | 
    
         
            +
                  user_agents_hash = UserAgentRandomizer.user_agents_hash
         
     | 
| 
       16 
16 
     | 
    
         
             
                  if ua_types.include?(type)
         
     | 
| 
       17 
     | 
    
         
            -
                    UserAgentRandomizer::UserAgent.new(type: type, string:  
     | 
| 
      
 17 
     | 
    
         
            +
                    UserAgentRandomizer::UserAgent.new(type: type, string: user_agents_hash[type].sample)
         
     | 
| 
       18 
18 
     | 
    
         
             
                  else
         
     | 
| 
       19 
     | 
    
         
            -
                     
     | 
| 
       20 
     | 
    
         
            -
                     
     | 
| 
       21 
     | 
    
         
            -
                    UserAgentRandomizer::UserAgent.new(type: ua_type, string: ua_string)
         
     | 
| 
      
 19 
     | 
    
         
            +
                    ua = UserAgentRandomizer.user_agents_array.sample
         
     | 
| 
      
 20 
     | 
    
         
            +
                    UserAgentRandomizer::UserAgent.new(type: ua[:type], string: ua[:string])
         
     | 
| 
       22 
21 
     | 
    
         
             
                  end
         
     | 
| 
       23 
22 
     | 
    
         
             
                end
         
     | 
| 
       24 
23 
     | 
    
         
             
              end
         
     | 
| 
         @@ -6,7 +6,7 @@ require 'user_agent_randomizer/version' 
     | 
|
| 
       6 
6 
     | 
    
         
             
            Gem::Specification.new do |spec|
         
     | 
| 
       7 
7 
     | 
    
         
             
              spec.name          = "user-agent-randomizer"
         
     | 
| 
       8 
8 
     | 
    
         
             
              spec.version       = UserAgentRandomizer::VERSION
         
     | 
| 
       9 
     | 
    
         
            -
              spec.date          = "2014- 
     | 
| 
      
 9 
     | 
    
         
            +
              spec.date          = "2014-10-22"
         
     | 
| 
       10 
10 
     | 
    
         
             
              spec.authors       = ["Christoph Pilka"]
         
     | 
| 
       11 
11 
     | 
    
         
             
              spec.email         = ["c.pilka@asconix.com"]
         
     | 
| 
       12 
12 
     | 
    
         
             
              spec.summary       = %q{Random HTTP User-Agent string generator}
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: user-agent-randomizer
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.2.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Christoph Pilka
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2014- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2014-10-22 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: bundler
         
     | 
| 
         @@ -107,6 +107,7 @@ files: 
     | 
|
| 
       107 
107 
     | 
    
         
             
            - LICENSE
         
     | 
| 
       108 
108 
     | 
    
         
             
            - README.md
         
     | 
| 
       109 
109 
     | 
    
         
             
            - Rakefile
         
     | 
| 
      
 110 
     | 
    
         
            +
            - benchmark/generate.rb
         
     | 
| 
       110 
111 
     | 
    
         
             
            - data/user_agents.yml
         
     | 
| 
       111 
112 
     | 
    
         
             
            - lib/user_agent_randomizer.rb
         
     | 
| 
       112 
113 
     | 
    
         
             
            - lib/user_agent_randomizer/user_agent.rb
         
     |