Skip to content

Commit

Permalink
added tests and support for consoles [xbox, playstation, nintendo ds,…
Browse files Browse the repository at this point in the history
… wii]
  • Loading branch information
SidOfc committed May 29, 2016
1 parent a6d23f4 commit 6567172
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 7 deletions.
Empty file added grep
Empty file.
4 changes: 3 additions & 1 deletion lib/browserino.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
require 'browserino/maps/windows_phone'

require 'browserino/core/patterns'
require 'browserino/core/helpers'
require 'browserino/core/questions'
require 'browserino/core/helpers'
require 'browserino/core/lies'
require 'browserino/core/alias'

Expand All @@ -24,6 +24,7 @@
require 'browserino/browser'
require 'browserino/engine'
require 'browserino/operating_system'
require 'browserino/console'

# require_relative "../spec/user_agents"
# require_relative "../spec/user_agents_bots"
Expand All @@ -49,6 +50,7 @@ def fetch_info(ua, name)
system_name: OperatingSystem.name(ua),
system_version: OperatingSystem.version(ua),
system_architecture: OperatingSystem.architecture(ua),
console_name: Console.name(ua),
locale: OperatingSystem.locale(ua) }
end

Expand Down
13 changes: 8 additions & 5 deletions lib/browserino/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def system_name(opts = {})
end
end

alias_method :console_name, :system_name

def system_version
@info[:system_version]
end
Expand All @@ -53,6 +51,10 @@ def system_architecture
@info[:system_architecture]
end

def console_name
@info[:console_name]
end

def locale
@info[:locale]
end
Expand Down Expand Up @@ -91,7 +93,7 @@ def hash_for_to_s
end

def to_a
@info.keys.each_with_object([]) { |f, a| a.push([f, send(f)]) }.compact
@info.keys.each_with_object([]) { |f, a| a.push([f, send(f)]) }
end

def to_h
Expand All @@ -100,15 +102,16 @@ def to_h

def method_missing(method_sym, *args, &block)
name = method_sym.to_s.tr('?', '')
invertable case agent_or_system?(method_sym)
invertable case type_id(method_sym)
when :system then correct_system?(name, *args)
when :agent then correct_agent?(name, *args)
when :console then correct_console?(name, *args)
else super
end
end

def respond_to?(method_sym, *args, &block)
agent_or_system?(method_sym).nil? ? false : true
type_id(method_sym).nil? ? false : true
end

private
Expand Down
10 changes: 10 additions & 0 deletions lib/browserino/console.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Browserino
module Console
def self.name(ua)
Browserino.extract_match(
ua.match(Core::PATTERNS[:console][:name]),
:name
)
end
end
end
4 changes: 4 additions & 0 deletions lib/browserino/core/alias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module Core
'windows_phone' => /windows\sphone/,
'ios' => /ip(?:[ao]d|hone)/
},
console_name: {
'nintendo_ds' => /nintendo\s\d?ds/,
'wii' => /wiiu?/
},
system_architecture: {
'x64' => /(?:x86_|amd|wow)?64/,
'x32' => /(?:(?:x86_)?32|i[36]8[36])/
Expand Down
8 changes: 7 additions & 1 deletion lib/browserino/core/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ def correct_agent?(name, version = nil)
end
end

def agent_or_system?(method_sym)
def correct_console?(name)
Core::Questions::CONSOLES.include?(name.to_sym)
end

def type_id(method_sym)
name = method_sym.to_s.tr('?', '')
supported = Core::PATTERNS[:browser].merge(Core::PATTERNS[:bot]).keys
if supported.include?(name.to_sym)
:agent
elsif Mapping.const?(name.upcase.to_sym)
:system
elsif name == 'console'
:console
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/browserino/core/patterns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ module Core
|blackberry|iemobile|fennec|bada|meego|vodafone
|t\-mobile|opera\sm(?:ob|in)i/xi,
locale: /\s(?<locale>\w{2}(?:\-\w{2})?)[;\)]/
},

console: {
name: /(?<name>xbox|playstation|nintendo\s3?ds|wiiu?)/i,
}
}.freeze
end
Expand Down
7 changes: 7 additions & 0 deletions lib/browserino/core/questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Questions

BROWSERS = (Core::PATTERNS[:browser].keys + [:ff]).freeze

CONSOLES = [:xbox, :playstation, :nintendo_ds, :wii].freeze

OPERATING_SYSTEMS = (Browserino::Mapping
.constants(:true)
.map(&:downcase) + [:osx, :bb, :win]).freeze
Expand Down Expand Up @@ -63,6 +65,11 @@ def bot?(name = nil)
invertable is_bot && is_name
end

def console?(name = nil)
arg = (name.nil? ? console_name : name).to_s.to_sym
invertable CONSOLES.include?(arg)
end

def search_engine?(name = nil)
arg = (name.nil? ? search_engine_name : name).to_s.to_sym
invertable SEARCH_ENGINES.include?(arg)
Expand Down
24 changes: 24 additions & 0 deletions spec/browserino_console_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'
require 'user_agents'
require 'user_agents_consoles'

describe 'Browserino console identification' do
UserAgents::Consoles.constants(true).each do |console|
agents = UserAgents::Consoles.const_get(console)
agents.each do |ua, criteria|
agent = Browserino.parse(ua)
criteria.each do |prop, val|
case prop
when :console_name
it "Expects agent.#{prop} to be #{val} for #{console.downcase}" do
expect(agent.send(prop).downcase).to eq val.downcase
end
when :console?
it "Expects agent.#{prop} to be #{val} for #{console.downcase}" do
expect(agent.send(prop)).to eq val
end
end
end
end
end
end
35 changes: 35 additions & 0 deletions spec/user_agents_consoles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module UserAgents
module Consoles
XBOX = {
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; Xbox)' => {
console_name: 'xbox',
console?: true
}
}

PLAYSTATION = {
'Mozilla/5.0 (PLAYSTATION 3; 3.55)' => {
console_name: 'playstation',
console?: true
},
'Mozilla/4.0 (PS3 (PlayStation 3); 1.00)' => {
console_name: 'playstation',
console?: true
}
}

NINTENDO_DS = {
'Bunjalloo/0.7.6(Nintendo DS;U;en)' => {
console_name: 'nintendo_ds',
console?: true
}
}

WII = {
'Opera/9.30 (Nintendo Wii; U; ; 2047-7; de)' => {
console_name: 'wii',
console?: true
}
}
end
end

0 comments on commit 6567172

Please sign in to comment.