Sha256: 79d2ad3d2c61d845bb257e4285600f554f98f21810b9bbf84adff470421ea8a6

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require 'yaml'

module Webdriver
  module UserAgent
    module Devices
      
      def devices
        YAML.load_file devices_file
      end

      def resolution_for(device_name, orientation, user_width, user_height)
        return [user_width.to_i, user_height.to_i] if ((user_width.to_i + user_height.to_i) > 1)
        
        device = devices[device_name.downcase][orientation.downcase]
        [device[:width],device[:height]]
      end

      def agent_string_for(device)
        device = (device ? device.downcase : :iphone)
        user_agent_string = (device == :random ? random_user_agent : devices[device][:user_agent])
        raise "Unsupported user agent: '#{device}'." unless user_agent_string
        user_agent_string
      end

      private
      
      def random_user_agent
        File.foreach(user_agents_file).each_with_index.reduce(nil) do |picked,pair|
          rand < 1.0/(1+pair[1]) ? pair[0] : picked
        end
      end

      def user_agents_file
        File.expand_path("../../device-info/user_agents.txt", __FILE__)
      end

      def devices_file
        File.expand_path("../../device-info/devices.yaml", __FILE__)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
webdriver-user-agent-7.5 lib/webdriver-user-agent/devices.rb
webdriver-user-agent-7.4 lib/webdriver-user-agent/devices.rb
webdriver-user-agent-7.3 lib/webdriver-user-agent/devices.rb