iana-data 1.2.1 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,145 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # NOTE
4
- # RFC 4646 described below has been obseleted by RFC5646. Tests still run OK
5
- # but I have no idea (yet) what this new RFC has changed in terms of the LSR
6
- # file format or langugae tags generally.
7
- # See http://tools.ietf.org/html/rfc5646 (particularly section 8).
8
- # 2011-04-18
9
- module IANA
10
- # Get the Language Subtag Registry data from the IANA site or a text file
11
- # and load it into an array of hashes for further processing.
12
- #
13
- # IANA's Language Subtag Registry incorporates multiple standards for the
14
- # definition of language- and locale-defining strings, including:
15
- # ISO 3166: English country names and code elements
16
- # ( http://www.iso.org/iso/english_country_names_and_code_elements )
17
- # ISO 639.2: Codes for the Representation of Names of Languages
18
- # ( http://www.loc.gov/standards/iso639-2/php/code_list.php )
19
- # The registry's format is specified in RFC 4646
20
- # ( http://www.ietf.org/rfc/rfc4646.txt )
21
- class LanguageSubtagRegistry
22
- # Entries always have these keys: Type, [Tag or Subtag], Description, Added
23
- # All keys used in the IANA format:
24
- Keys = [
25
- "Type",
26
- "Tag",
27
- "Subtag",
28
- "Description",
29
- "Added",
30
- "Preferred-Value",
31
- "Deprecated",
32
- "Suppress-Script",
33
- "Prefix",
34
- "Comments" ]
35
- IANAHost = 'www.iana.org'
36
- IANAPath = '/assignments/language-subtag-registry'
37
- IANAFile = File.dirname(__FILE__) + "/../../example-data/lsr.txt"
38
- @@tags = []
39
- @@file_date = ""
40
-
41
- def self.tags
42
- @@tags
43
- end
44
-
45
- def self.file_date
46
- @@file_date
47
- end
48
-
49
- # Load data directly from IANA site
50
- def self.get(path=IANAPath, host=IANAHost)
51
- require 'net/http'
52
- site = Net::HTTP.new host
53
- response = site.request_get path
54
- self.load(response.body)
55
- end
56
-
57
- # Load data from a text file
58
- def self.open(filename=IANAFile)
59
- file = File.open(filename, 'r')
60
- self.load(file.read)
61
- end
62
-
63
- # Load data from text string
64
- def self.load(text)
65
- @@tags = []
66
- @@file_date = ""
67
- item = {}
68
- prev = []
69
- text.each do |line|
70
- line.chomp!
71
- case line
72
- when /^%%$/
73
- (@@tags << item; item = {}) unless item.empty? # separator; append last entry, if present
74
- when /^File-Date: .*/
75
- @@file_date = line.gsub('File-Date: ','') # File-date entry
76
- when /^ [^ ].*/
77
- item[prev].kind_of?(Array) ? item[prev].last += " " + line.strip : item[prev] += " " + line.strip # continuation line
78
- else # everything else (the actual key: value pairs)
79
- key, val = line.split(':', 2) # the main pair (ie, "Subtag: en")
80
- if /\.\./.match val # value specifies a range, not simply a string
81
- start, finish = val.strip.split('..', 2)
82
- val = Range.new(start, finish)
83
- else # otherwise it's just a string
84
- val.strip!
85
- end
86
- if item.has_key?(key) # append to array if this key already exists
87
- item[key] = [item[key]] unless item[key].kind_of? Array
88
- item[key] << val
89
- else # otherwise simply assign the item
90
- item[key] = val
91
- end
92
- prev = key # in case of continuation (wrapped text)
93
- end
94
- end
95
- @@tags << item
96
- nil
97
- end
98
-
99
- # Dump data to:
100
- # IANA's flat text format (String)
101
- # YAML (String)
102
- # A hash of hashes, with the keys being the tags (Hash)
103
- def self.dump(format = "iana")
104
- begin
105
- format = format.strip.downcase
106
- rescue
107
- raise ArgumentError, "Invalid argument type; expected String"
108
- end
109
- case format
110
- when "iana"
111
- new_text = "File-Date: " + @@file_date + $/ + "%%" + $/
112
- new_text << @@tags.map do |item|
113
- Keys.map do |key|
114
- val = item[key]
115
- if val
116
- if val.kind_of? Array
117
- val.map { |i| key + ": " + i.to_s }.join($/)
118
- else
119
- key + ": " + val.to_s
120
- end
121
- else
122
- nil
123
- end
124
- end.compact.join($/) + $/
125
- end.join('%%' + $/)
126
- when "yaml"
127
- require 'yaml'
128
- YAML::dump([@@file_date, @@tags])
129
- when "hash"
130
- taghash = {}
131
- @@tags.each do |tag|
132
- tag = tag.clone
133
- val = tag.delete("Tag") || tag.delete("Subtag")
134
- taghash[val.to_s] = tag
135
- end
136
- taghash
137
- else
138
- raise ArgumentError, "Invalid format option"
139
- end
140
- end
141
-
142
- end
143
- end
144
-
145
- raise RuntimeError, 'This library is for require only' if $0 == __FILE__