Sha256: 1b1c40a6e6d7441cb4eaa277d92d2490a94162b4f69ce508da3556b36c7ca560

Contents?: true

Size: 829 Bytes

Versions: 1

Compression:

Stored size: 829 Bytes

Contents

require 'langtastic/version'
require 'net/http'

module Langtastic
  extend self

  def subtags
    raw_subtags.map do |subtag|
      @last_field = []
      subtag.map do |field|
        if field.split(': ').size > 1
          @this_field = field.split(': ')
          if duplicate_field?
            @last_field[1] += ", #{@this_field[1]}"
            nil
          else
            @last_field = @this_field
          end
        else
          @last_field[1] += field
          nil
        end
      end.compact.to_h
    end
  end

  private

  def duplicate_field?
    @this_field[0] == @last_field[0]
  end

  def raw_subtags
    data.split('%%').map(&:strip).map { |subtag| subtag.split("\n") }
  end

  def data
    Net::HTTP.get('www.iana.org', '/assignments/language-subtag-registry/language-subtag-registry')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
langtastic-0.1.0 lib/langtastic.rb