Class: Cani::Api::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/cani/api/browser.rb

Constant Summary collapse

ABBR_MAP =
{ 'ios' => 'saf.ios' }.freeze
LABEL_MAP =
{
  'ie'      => 'Internet Explorer',
  'edge'    => 'Edge',
  'ff'      => 'Firefox',
  'chr'     => 'Chrome',
  'saf'     => 'Safari',
  'op'      => 'Opera',
  'saf.ios' => 'IOS Safari',
  'o.mini'  => 'Opera Mini',
  'and'     => 'Android Browser',
  'bb'      => 'BlackBerry Browser',
  'o.mob'   => 'Opera Mobile',
  'chr.and' => 'Chrome for Android',
  'ff.and'  => 'Firefox for Android',
  'ie.mob'  => 'Internet Explorer Mobile',
  'uc'      => 'UC Browser for android',
  'ss'      => 'Samsung Internet',
  'qq'      => 'QQ Browser',
  'baidu'   => 'Baidu Browser'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Browser

Returns a new instance of Browser.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cani/api/browser.rb', line 28

def initialize(attributes = {})
  abbr = attributes['abbr'].downcase.gsub(/^\.+|\.+$/, '').tr '/', '.'

  @abbr     = ABBR_MAP.fetch abbr, abbr
  @label    = LABEL_MAP.fetch abbr, abbr
  @name     = attributes[:name].downcase
  @title    = attributes['browser'].downcase
  @prefix   = attributes['prefix'].downcase
  @type     = attributes['type'].downcase
  @usage    = attributes['usage_global'].each_with_object({}) do |(v, u), h|
    v.split('-').each { |ver| h[ver] = u }
  end
  @eras = attributes['versions'].each_with_object([]) do |v, a|
    if v
      v.split('-').each { |ver| a << ver }
    else
      a << v
    end
  end
  @versions = @usage.keys
  @features = {}
end

Instance Attribute Details

#abbrObject (readonly)

Returns the value of attribute abbr.



4
5
6
# File 'lib/cani/api/browser.rb', line 4

def abbr
  @abbr
end

#erasObject (readonly)

Returns the value of attribute eras.



4
5
6
# File 'lib/cani/api/browser.rb', line 4

def eras
  @eras
end

#labelObject (readonly)

Returns the value of attribute label.



4
5
6
# File 'lib/cani/api/browser.rb', line 4

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/cani/api/browser.rb', line 4

def name
  @name
end

#prefixObject (readonly)

Returns the value of attribute prefix.



4
5
6
# File 'lib/cani/api/browser.rb', line 4

def prefix
  @prefix
end

#titleObject (readonly)

Returns the value of attribute title.



4
5
6
# File 'lib/cani/api/browser.rb', line 4

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/cani/api/browser.rb', line 4

def type
  @type
end

#usageObject (readonly)

Returns the value of attribute usage.



4
5
6
# File 'lib/cani/api/browser.rb', line 4

def usage
  @usage
end

#versionsObject (readonly)

Returns the value of attribute versions.



4
5
6
# File 'lib/cani/api/browser.rb', line 4

def versions
  @versions
end

Instance Method Details

#features_for(version) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/cani/api/browser.rb', line 59

def features_for(version)
  @features[version] ||= Cani.api.features.each_with_object({}) do |ft, h|
    type = ft.support_in name, version
    (h[type] ||= []) << { support: type, title: ft.title,
                          status: ft.status, percent: ft.percent }
  end
end

#max_column_widthObject



55
56
57
# File 'lib/cani/api/browser.rb', line 55

def max_column_width
  [name.size, versions.map(&:size).max].max
end


51
52
53
# File 'lib/cani/api/browser.rb', line 51

def most_popular_era_idx
  eras.find_index usage.sort_by { |_, v| -v }.first.first
end