Sha256: 0707cec1dd1717106f4e163bd38a094dfe2d4f207e45ad1384ad3159406431ed

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 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

2 entries across 2 versions & 1 rubygems

Version Path
webdriver-user-agent-7.8 lib/webdriver-user-agent/devices.rb
webdriver-user-agent-7.6 lib/webdriver-user-agent/devices.rb