summaryrefslogtreecommitdiff
path: root/web/lib/api/test/langtag.rb
blob: 4268bd84b6c141e84d72baf6018feca2e7c07355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
# web/lib/api/v4/langtag.rb
class Taginfo < Sinatra::Base

    api(0, 'keys/name', {
        :description => 'Get list of keys from the database that contain the string "name".',
        :parameters => { :query => 'Only show keys matching this query (substring match, optional).' },
        :paging => :optional,
        :sort => %w( key count_all ),
        :result => {
            :key           => :STRING,
            :count_all     => :INT,
            :in_wiki       => :BOOL,
            :prefix        => :STRING,
            :type          => :STRING,
            :langtag       => :STRING,
            :langtag_state => :STRING,
            :lang          => :STRING,
            :lang_state    => :STRING,
            :lang_name     => :STRING,
            :script        => :STRING,
            :script_state  => :STRING,
            :region        => :STRING,
            :region_state  => :STRING,
            :notes         => :STRING
        },
        :example => { :page => 1, :rp => 10, :sortname => 'key', :sortorder => 'asc' },
        :ui => '/reports/name_tags'
    }) do

        total = @db.count('db.keys').
            condition("key LIKE '%name%'").
            condition_if("key LIKE '%' || ? || '%'", params[:query]).
            get_first_value().to_i

        res = @db.select('SELECT * FROM db.keys').
            condition("key LIKE '%name%'").
            condition_if("key LIKE '%' || ? || '%'", params[:query]).
            order_by(@ap.sortname, @ap.sortorder) { |o|
                o.key
                o.count_all
            }.
            paging(@ap).
            execute()

        return JSON.generate({
            :page  => @ap.page,
            :rp    => @ap.results_per_page,
            :total => total,
            :data  => res.map{ |row|
                nt = BCP47::Nametag.new(@db, row['key'])
                {
                :key           => row['key'],
                :count_all     => row['count_all'].to_i,
                :in_wiki       => row['in_wiki'].to_i == 1 ? true : false,
                :prefix        => nt.prefix,
                :type          => nt.type,
                :langtag       => nt.langtag,
                :langtag_state => nt.langtag_state,
                :lang          => nt.lang,
                :lang_state    => nt.lang_state,
                :lang_note     => nt.lang_note,
                :script        => nt.script,
                :script_state  => nt.script_state,
                :script_note   => nt.script_note,
                :region        => nt.region,
                :region_state  => nt.region_state,
                :region_note   => nt.region_note,
                :notes         => nt.notes
            }}
        }, json_opts(params[:format]))
    end

    @@bcp47_filters = {};
    BCP47::SUBTAG_TYPES.each do |type|
        @@bcp47_filters[type.to_sym] = { :expr => type, :doc => "Show entries of type '#{type}' only." };
    end

    api(0, 'langtags', {
        :description => 'Get official subtags from the IETF BCP47 registry.',
        :parameters => { :query => 'Only show entries matching this query (case insensitive substring match on subtags and description; optional).' },
        :paging => :optional,
        :filter => @@bcp47_filters,
        :sort => %w( subtag description added ),
        :result => {
            :type        => :STRING,
            :subtag      => :STRING,
            :description => :STRING,
            :added       => :STRING,
            :notes       => :STRING
        },
        :example => { :page => 1, :rp => 10, :sortname => :subtag, :sortorder => 'asc' },
        :ui => '/reports/name_tags#bcp47'
    }) do

        @filter_type = BCP47.get_filter(params[:filter])

        total = @db.count('languages.subtags').
                condition_if("stype = ?", @filter_type).
                get_first_value().to_i

        res = @db.select('SELECT * FROM languages.subtags').
            condition_if("stype = ?", @filter_type).
            condition_if("subtag LIKE '%' || ? || '%' OR description LIKE '%' || ? || '%'", params[:query], params[:query]).
            order_by(@ap.sortname, @ap.sortorder) { |o|
                o.subtag
                o.description
                o.added
            }.
            paging(@ap).
            execute()

        return JSON.generate({
            :page  => @ap.page,
            :rp    => @ap.results_per_page,
            :total => total,
            :data  => res.map{ |row|
                notes = ''
                if row['suppress_script']
                    notes += "Default script: #{ row['suppress_script'] }"
                end
                unless row['prefix'].empty?
                    notes += "Prefixes: #{ row['prefix'] }"
                end
                {
                :type        => row['stype'].titlecase,
                :subtag      => row['subtag'],
                :description => row['description'],
                :added       => row['added'],
                :notes       => notes
            }}
        }, json_opts(params[:format]))
    end

end