Class: Cani::Api::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/cani/api/feature.rb,
lib/cani/api/feature/viewer.rb

Defined Under Namespace

Classes: Viewer

Constant Summary collapse

STATUSES =
{
  'rec'   => 'rc',
  'unoff' => 'un',
  'other' => 'ot'
}.freeze
TYPES =
{
  'y' => {symbol: '+', name: :default,     short: :sup},
  'a' => {symbol: '~', name: :partial,     short: :prt},
  'n' => {symbol: '-', name: :unsupported, short: :not},
  'p' => {symbol: '#', name: :polyfill,    short: :ply},
  'x' => {symbol: '@', name: :prefix,      short: :pfx},
  'd' => {symbol: '!', name: :flag,        short: :flg},
  'u' => {symbol: '?', name: :unknown,     short: :unk}
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Feature

Returns a new instance of Feature.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cani/api/feature.rb', line 22

def initialize(attributes = {})
  @name    = attributes[:name].to_s.downcase
  @title   = attributes['title']
  @status  = STATUSES.fetch attributes['status'], attributes['status']
  @spec    = attributes['spec']
  @percent = attributes['usage_perc_y']
  @notes   = attributes['notes'].split "\n"
  @notes_by_num = attributes['notes_by_num']
  @stats, @browser_note_nums = attributes['stats'].each_with_object([{}, {}]) do |(browser, info), (stts, notes)|
    stts[browser], notes[browser] = info.each_with_object([{}, {}]) do |(version, stat), (st, nt)|
      version.split('-').each do |v|
        nt[v] = stat.scan(/#(\d+)/).flatten
        st[v] = stat[' d '] ? 'd' : stat[0]
      end
    end
  end
end

Instance Attribute Details

#browser_note_numsObject (readonly)

Returns the value of attribute browser_note_nums.



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

def browser_note_nums
  @browser_note_nums
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#notesObject (readonly)

Returns the value of attribute notes.



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

def notes
  @notes
end

#notes_by_numObject (readonly)

Returns the value of attribute notes_by_num.



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

def notes_by_num
  @notes_by_num
end

#percentObject (readonly)

Returns the value of attribute percent.



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

def percent
  @percent
end

#specObject (readonly)

Returns the value of attribute spec.



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

def spec
  @spec
end

#statsObject (readonly)

Returns the value of attribute stats.



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

def stats
  @stats
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Class Method Details

.support_legendObject



57
58
59
# File 'lib/cani/api/feature.rb', line 57

def self.support_legend
  TYPES.map { |_, v| "#{v[:short]}(#{v[:symbol]})" }.join ' '
end

Instance Method Details

#current_supportObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cani/api/feature.rb', line 40

def current_support
  @current_support ||= Cani.config.browsers.map do |browser|
    bridx = Cani.api.browsers.find_index { |brs| brs.name == browser }
    brwsr = Cani.api.browsers[bridx] unless bridx.nil?
    syms  = stats[browser].values.compact.last(Cani.config.versions)
                          .map { |s| TYPES[s][:symbol] || '' }
                          .join.rjust Cani.config.versions

    syms + brwsr.abbr
  end
end

#support_in(browser, version) ⇒ Object



52
53
54
55
# File 'lib/cani/api/feature.rb', line 52

def support_in(browser, version)
  TYPES.fetch(stats[browser.to_s][version.to_s.downcase], {})
       .fetch :name, :unknown
end