Skip to content

Commit

Permalink
more concise naming for internals behind #match blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
SidOfc committed Apr 23, 2017
1 parent 66b5012 commit 78de963
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 38 deletions.
6 changes: 3 additions & 3 deletions lib/browserino.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require_relative 'browserino/methods'
require_relative 'browserino/client'
require_relative 'browserino/version'
require_relative 'browserino/identity'
require_relative 'browserino/matcher'

require_relative 'browserino/definitions/matchers'
require_relative 'browserino/definitions/aliasses'
Expand All @@ -16,8 +16,8 @@
module Browserino
def self.parse(ua)
config.before_parse.each { |b| ua = b.call ua } if config.before_parse.any?
config.identities.each do |identity|
return analyze ua, identity if identity.matches? ua
config.matchers.each do |matcher|
return analyze ua, matcher if matcher.matches? ua
end
analyze ua
end
Expand Down
18 changes: 9 additions & 9 deletions lib/browserino/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ class Config < Options
def define(&block)
instance_eval(&block)

identities.each do |identity|
properties << identity.properties.keys
types << identity.type
names << identity.name
matchers.each do |matcher|
properties << matcher.properties.keys
types << matcher.type
names << matcher.name
end

properties.flatten!.uniq!
Expand Down Expand Up @@ -35,11 +35,11 @@ def match(rgxp = nil, **opts, &block)
opts = @tmp_defaults.merge opts if @tmp_defaults.is_a? Hash

if rgxp && opts[:like]
identities.unshift with_alias(rgxp, opts, &block)
matchers.unshift with_alias(rgxp, opts, &block)
elsif rgxp
identities << Identity.new(rgxp, opts, &block).freeze
matchers << Matcher.new(rgxp, opts, &block).freeze
else
global_identities.unshift Identity.new(&block).freeze
global_matchers.unshift Matcher.new(&block).freeze
end
end

Expand Down Expand Up @@ -79,7 +79,7 @@ def libraries(&block)
end

def with_alias(pattern, **opts, &block)
id = identities.select { |id| id == opts[:like] }.first
id = matchers.select { |id| id == opts[:like] }.first

raise "No alias found for: #{opts[:like] || 'nil'}" unless id

Expand All @@ -88,7 +88,7 @@ def with_alias(pattern, **opts, &block)
base = base.reject { |k| excl.include? k }
end

Identity.new(pattern, base.merge(opts), &block).freeze
Matcher.new(pattern, base.merge(opts), &block).freeze
end
end
end
2 changes: 1 addition & 1 deletion lib/browserino/definitions/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
ua
end

# after an identity is found, it's values are filtered in two stages
# after a matcher is found, it's values are filtered in two stages
# first, filters will parse all statically defined values (e.g.) no regexp
# or block within matchers, after that, smart matcher patterns will be
# conditionally added and parsed with the previously collected values and
Expand Down
14 changes: 7 additions & 7 deletions lib/browserino/definitions/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
smart_match :version, with: ':name[\s/]?([\d\._]+)', flags: [:i]
smart_match :engine_version, with: ':engine/([\d\._]+)', flags: [:i]

# a simple set of global matchers that will be merged
# with an identity the final client object is created
# a simple set of global matchers that will be merged and scanned
# with a specific matcher when the final client object is created
match do
locale %r{(?<!nintendo)[;\s](\w{2}(?:\-\w{2})?)[;)]}i
architecture %r{((?:(?:x|x86_|amd|wow|win)64)|i[36]86)}i
Expand All @@ -33,8 +33,8 @@

# automatically set type to :browser for each defined matcher
browsers do
# a single matcher that will create an Identity for a specific match
# identities are added in an array in order of definition - higher is better
# a single matcher that will create a matcher for a specific match
# matchers are added in an array in order of definition - higher is better
# aliasses are prepended to the list instead of appended (e.g. like blocks)
# this ensures that aliasses will be matched before any regular matcher
match %r{maxthon}i do
Expand Down Expand Up @@ -225,7 +225,7 @@

# inherit properties a standard set of properties by the name of a
# previously defined matcher, overwritten by properties added within matchers
# inherit properties from Identity where name == :chrome, (except :version)
# inherit properties from matcher where name == :chrome, (except :version)
like :chrome, except: [:version] do
match %r{brave}i, name: :brave
match %r{vivaldi}i, name: :vivaldi
Expand All @@ -235,7 +235,7 @@
match %r{comodo_dragon}i, name: :comodo_dragon
end

# inherit properties from Identity where name == :safari, (except :version)
# inherit properties from matcher where name == :safari, (except :version)
like :safari, except: [:version] do
match %r{bolt}i, name: :bolt
match %r{stainless}i, name: :stainless
Expand All @@ -247,7 +247,7 @@
version: %r{(?:version|w(?:eb)?osbrowser)/([\d\.]+)}i
end

# inherit properties from Identity where name == :firefox, (except :version)
# inherit properties from matcher where name == :firefox, (except :version)
like :firefox, except: [:version] do
match %r{prism}i, name: :prism
match %r{waterfox}i, name: :waterfox
Expand Down
4 changes: 2 additions & 2 deletions lib/browserino/identity.rb → lib/browserino/matcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true
module Browserino
class Identity
class Matcher
attr_reader :pattern, :properties

SETTINGS = { name: nil, type: :unknown }.freeze
Expand Down Expand Up @@ -28,7 +28,7 @@ def ===(other)
when Regexp then other =~ properties[:name]
when String then other.to_sym == properties[:name]
when Symbol then other == properties[:name]
when Identity then other.properties[:name] == properties[:name]
when Matcher then other.properties[:name] == properties[:name]
else false
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/browserino/methods.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# frozen_string_literal: true
module Browserino
def self.analyze(user_agent, identity = nil)
@defaults ||= config.global_identities.map(&:properties).reduce(&:merge)
def self.analyze(user_agent, matcher = nil)
@defaults ||= config.global_matchers.map(&:properties).reduce(&:merge)

props = @defaults.merge(identity && identity.properties || {})
props = @defaults.merge(matcher && matcher.properties || {})
like = props.delete :like
props = collect props, user_agent
props = props.merge collect(smart_matchers(props), user_agent)
props = with_labels props

if like
repl = user_agent =~ %r{#{like}}i && '' || like.to_s
like = parse user_agent.gsub identity.pattern, repl
like = parse user_agent.gsub matcher.pattern, repl
end

Client.new props, like
end

def self.config
@config ||= Config.new({ before_parse: [],
global_identities: [],
global_matchers: [],
properties: [],
types: [:unknown],
names: [],
smart_matchers: {},
identities: [],
matchers: [],
labels: Hash.new { |h, k| h[k] = [] },
filters: Hash.new { |h, k| h[k] = [] },
aliasses: Hash.new { |h, k| h[k] = [] } })
Expand Down
10 changes: 0 additions & 10 deletions spec/identity_spec.rb

This file was deleted.

10 changes: 10 additions & 0 deletions spec/matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'spec_helper'

describe 'Browserino::Matcher' do
matcher = Browserino.config.matchers.sample
matcher = Browserino.config.matchers.sample until matcher.name

it 'always responds to missing' do
matcher.respond_to_missing? :whatever
end
end

0 comments on commit 78de963

Please sign in to comment.