Sha256: d7eacdf6789cc925ca3cb2a285fc9ed3b74f3b2487d910f42f54ea754ad727ad

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

module UserAgentParser
  class Cli
    def initialize(user_agent, options = {})
      @user_agent = user_agent
      @options = options
    end

    def run!
      if @options[:family]
        @user_agent.family
      elsif @options[:name]
        @user_agent.name
      elsif @options[:version]
        with_version do |version|
          version.to_s
        end
      elsif @options[:major]
        major
      elsif @options[:minor]
        minor
      elsif @options[:os]
        @user_agent.os.to_s
      elsif format = @options[:format]
        format.
          gsub('%f', @user_agent.family).
          gsub('%n', @user_agent.name).
          gsub('%v', version.to_s).
          gsub('%M', major.to_s).
          gsub('%m', minor.to_s).
          gsub('%o', @user_agent.os.to_s)
      else
        @user_agent.to_s
      end
    end

    private

    def major
      with_version do |version|
        version.major
      end
    end

    def minor
      with_version do |version|
        version.minor
      end
    end

    def version
      @version ||= @user_agent.version
    end

    def with_version(&block)
      block.call(version) if version
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
user_agent_parser-2.4.0 lib/user_agent_parser/cli.rb
user_agent_parser-2.3.2 lib/user_agent_parser/cli.rb
user_agent_parser-2.3.1 lib/user_agent_parser/cli.rb
user_agent_parser-2.3.0 lib/user_agent_parser/cli.rb
user_agent_parser-2.2.0 lib/user_agent_parser/cli.rb