Module: Brauser::BrowserMethods::Register::ClassMethods

Defined in:
lib/brauser/browser.rb

Overview

Class methods.

Instance Method Summary collapse

Instance Method Details

#register_browser(name, name_match = nil, version_match = nil, label = nil) ⇒ Boolean

Registers a new browser that can be recognized.

Parameters:

  • name (Symbol|Array)

    The browser name or a list of browser (a list of array with [name, name_match, version_match, label] entries).

  • name_match (String|Regexp|Block) (defaults to: nil)

    The matcher for the name. If a block, it will be yield with the user agent and must return true if the name was recognized.

  • version_match (String|Regexp|Block) (defaults to: nil)

    The match for the version. If a block, it will be yield with the browser name and the user agent and must return the browser version.

  • label (String) (defaults to: nil)

    A human readable name of the browser.

Returns:

  • (Boolean)

    true if at least one browser has been added, false otherwise.



186
187
188
189
# File 'lib/brauser/browser.rb', line 186

def register_browser(name, name_match = nil, version_match = nil, label = nil)
  @browsers ||= []
  register_entries(@browsers, (name.is_a?(Array) ? name : [[name.ensure_string, name_match, version_match, label]]))
end

#register_default_browsersBoolean

Registers the default list of browsers that can be recognized.

Returns:

  • (Boolean)

    true if at least one browser has been added, false otherwise.



20
21
22
23
24
25
26
27
# File 'lib/brauser/browser.rb', line 20

def register_default_browsers
  @browsers = nil

  register_mobile_browsers
  register_desktop_browsers

  @browsers.present? ? true : false
end

#register_default_languagesBoolean

Registers the default list of languages that can be recognized.

Returns:

  • (Boolean)

    true if at least one language has been added, false otherwise.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/brauser/browser.rb', line 57

def register_default_languages
  @languages = nil

  self.register_language({
    "af" => "Afrikaans",
    "sq" => "Albanian",
    "eu" => "Basque",
    "bg" => "Bulgarian",
    "be" => "Byelorussian",
    "ca" => "Catalan",
    "zh" => "Chinese",
    "zh-cn" => "Chinese/China",
    "zh-tw" => "Chinese/Taiwan",
    "zh-hk" => "Chinese/Hong Kong",
    "zh-sg" => "Chinese/singapore",
    "hr" => "Croatian",
    "cs" => "Czech",
    "da" => "Danish",
    "nl" => "Dutch",
    "nl-nl" => "Dutch/Netherlands",
    "nl-be" => "Dutch/Belgium",
    "en" => "English",
    "en-gb" => "English/United Kingdom",
    "en-us" => "English/United States",
    "en-au" => "English/Australian",
    "en-ca" => "English/Canada",
    "en-nz" => "English/New Zealand",
    "en-ie" => "English/Ireland",
    "en-za" => "English/South Africa",
    "en-jm" => "English/Jamaica",
    "en-bz" => "English/Belize",
    "en-tt" => "English/Trinidad",
    "et" => "Estonian",
    "fo" => "Faeroese",
    "fa" => "Farsi",
    "fi" => "Finnish",
    "fr" => "French",
    "fr-be" => "French/Belgium",
    "fr-fr" => "French/France",
    "fr-ch" => "French/Switzerland",
    "fr-ca" => "French/Canada",
    "fr-lu" => "French/Luxembourg",
    "gd" => "Gaelic",
    "gl" => "Galician",
    "de" => "German",
    "de-at" => "German/Austria",
    "de-de" => "German/Germany",
    "de-ch" => "German/Switzerland",
    "de-lu" => "German/Luxembourg",
    "de-li" => "German/Liechtenstein",
    "el" => "Greek",
    "he" => "Hebrew",
    "he-il" => "Hebrew/Israel",
    "hi" => "Hindi",
    "hu" => "Hungarian",
    "ie-ee" => "Internet Explorer/Easter Egg",
    "is" => "Icelandic",
    "id" => "Indonesian",
    "in" => "Indonesian",
    "ga" => "Irish",
    "it" => "Italian",
    "it-ch" => "Italian/ Switzerland",
    "ja" => "Japanese",
    "km" => "Khmer",
    "km-kh" => "Khmer/Cambodia",
    "ko" => "Korean",
    "lv" => "Latvian",
    "lt" => "Lithuanian",
    "mk" => "Macedonian",
    "ms" => "Malaysian",
    "mt" => "Maltese",
    "no" => "Norwegian",
    "pl" => "Polish",
    "pt" => "Portuguese",
    "pt-br" => "Portuguese/Brazil",
    "rm" => "Rhaeto-Romanic",
    "ro" => "Romanian",
    "ro-mo" => "Romanian/Moldavia",
    "ru" => "Russian",
    "ru-mo" => "Russian /Moldavia",
    "sr" => "Serbian",
    "sk" => "Slovack",
    "sl" => "Slovenian",
    "sb" => "Sorbian",
    "es" => "Spanish",
    "es-do" => "Spanish",
    "es-ar" => "Spanish/Argentina",
    "es-co" => "Spanish/Colombia",
    "es-mx" => "Spanish/Mexico",
    "es-es" => "Spanish/Spain",
    "es-gt" => "Spanish/Guatemala",
    "es-cr" => "Spanish/Costa Rica",
    "es-pa" => "Spanish/Panama",
    "es-ve" => "Spanish/Venezuela",
    "es-pe" => "Spanish/Peru",
    "es-ec" => "Spanish/Ecuador",
    "es-cl" => "Spanish/Chile",
    "es-uy" => "Spanish/Uruguay",
    "es-py" => "Spanish/Paraguay",
    "es-bo" => "Spanish/Bolivia",
    "es-sv" => "Spanish/El salvador",
    "es-hn" => "Spanish/Honduras",
    "es-ni" => "Spanish/Nicaragua",
    "es-pr" => "Spanish/Puerto Rico",
    "sx" => "Sutu",
    "sv" => "Swedish",
    "sv-se" => "Swedish/Sweden",
    "sv-fi" => "Swedish/Finland",
    "ts" => "Thai",
    "tn" => "Tswana",
    "tr" => "Turkish",
    "uk" => "Ukrainian",
    "ur" => "Urdu",
    "vi" => "Vietnamese",
    "xh" => "Xshosa",
    "ji" => "Yiddish",
    "zu" => "Zulu"
  })

  @languages.present? ? true : false
end

#register_default_platformsBoolean

Registers the default list of platforms that can be recognized.

Returns:

  • (Boolean)

    true if at least one platform has been added, false otherwise.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/brauser/browser.rb', line 32

def register_default_platforms
  @platforms = nil

  self.register_platform([
    [:symbian, /s60|symb/i, "Symbian"],
    [:windows_phone, /windows phone/i, "Microsoft Windows Phone"],
    [:kindle, Proc.new { |name, _| name == :kindle }, "Nokia Symbian"],
    [:ios, Proc.new { |name, agent| [:iphone, :ipad, :ipod].include?(name) || agent =~ /ipad|iphone|ipod/i }, "Apple iOS"],
    [:android, /android/i, "Android"],
    [:blackberry, /blackberry/i, "RIM BlackBerry"],
    [:psp, /psp/i, "Sony Playstation Portable"],
    [:ps3, /playstation 3/i, "Sony Playstation 3"],
    [:wii, /wii/i, "Nintendo Wii"],

    [:linux, /linux/i, "Linux"],
    [:osx, /mac|macintosh|mac os x/i, "Apple MacOS X"],
    [:windows, /windows/i, "Microsoft Windows"]
  ])

  @platforms.present? ? true : false
end

#register_language(code, label = nil) ⇒ Boolean

Registers a new language that can be recognized.

Parameters:

  • code (String|Hash)

    The language code or an hash with codes as keys and label as values.

  • label (String) (defaults to: nil)

    The language name. Ignored if code is an Hash.

Returns:

  • (Boolean)

    true if at least one language has been added, false otherwise.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/brauser/browser.rb', line 207

def register_language(code, label = nil)
  @languages ||= {}
  rv = false
  code = {code.ensure_string => label.ensure_string} if !code.is_a?(Hash)

  code.each_pair do |c, l|
    if c.present? && l.present? then
      @languages[c] = l
      rv = true
    end
  end

  rv
end

#register_platform(name, matcher = nil, label = nil) ⇒ Boolean

Registers a new platform that can be recognized.

Parameters:

  • name (Symbol|Array)

    The platform name or a list of platforms (a list of array with [name, matcher, label] entries).

  • matcher (StringRegexp|Block) (defaults to: nil)

    The matcher for the platform. If a block, it will be yielded with the browser name and the user agent and must return true if the platform was recognized.

  • label (String) (defaults to: nil)

    A human readable name of the platform.

Returns:

  • (Boolean)

    true if at least one platform has been added, false otherwise.



197
198
199
200
# File 'lib/brauser/browser.rb', line 197

def register_platform(name, matcher = nil, label = nil)
  @platforms ||= []
  register_entries(@platforms, (name.is_a?(Array) ? name : [[name.ensure_string, matcher, label]]))
end