Class: FDroid::App

Inherits:
Object
  • Object
show all
Defined in:
lib/fdroid/App.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, packages, locale) ⇒ App

Returns a new instance of App.



22
23
24
25
26
27
28
# File 'lib/fdroid/App.rb', line 22

def initialize(app, packages, locale)
  # Sort packages in reverse-chronological order
  @packages = packages.map { |p| Package.new(p) }
  @app = app
  @locale = locale
  @available_locales = app.key?('localized') ? App.available_locales(locale, app['localized']) : nil
end

Class Method Details

.available_locales(desired_locale, localized_data) ⇒ Array

Given the desired_locale, searches through the list of localized_data entries and finds those with keys which match either:

* The desired locale exactly
* The same language as the desired locale (but different region)
* Any English language (so if the desired language is not there it will suffice)

These will be sorted in order of preference:

* Exact matches (language and region)
* Language portion matches but region is absent/doesn't match.
* en-US
* en
* en-*

It is intentionally liberal in searching for either “_” or “-” to separate language and region, because they both mean (in different context) to split langugae on the left, and region on the right, and it is cheap to do so.

Parameters:

  • desired_locale (string)
  • localized_data (Hash)

Returns:

  • (Array)


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/fdroid/App.rb', line 207

def self.available_locales(desired_locale, localized_data)
  parts = desired_locale.split(/[_-]/)
  desired_lang = parts[0]

  locales = localized_data.keys.select do |available_locale|
    parts = available_locale.split(/[_-]/)
    available_lang = parts[0]
    available_lang == desired_lang || available_lang == 'en'
  end

  measure_locale_goodness = lambda do |locale|
    parts = locale.split(/[_-]/)
    lang = parts[0]
    region = parts.length > 1 ? parts[1] : nil
    if locale == desired_locale
      return 1
    elsif lang == desired_lang
      return 2
    elsif locale == 'en-US'
      return 3
    elsif lang == 'en' && region.nil?
      return 4
    elsif lang == 'en'
      return 5
    end
  end

  locales.sort do |a, b|
    measure_locale_goodness.call(a) <=> measure_locale_goodness.call(b)
  end
end

.format_description_to_html(string) ⇒ Object

Ensure newlines in descriptions are preserved (converted to “<br />” tags) Handles UNIX, Windows and MacOS newlines, with a one-to-one replacement



141
142
143
# File 'lib/fdroid/App.rb', line 141

def self.format_description_to_html(string)
  string.gsub(/(?:\n\r?|\r\n?)/, '<br />')
end

.localized(available_locales, localized, field) ⇒ string

Parameters:

  • available_locales (string)
  • localized (string)
  • field (string)

Returns:

  • (string)


149
150
151
152
153
154
155
156
157
158
159
# File 'lib/fdroid/App.rb', line 149

def self.localized(available_locales, localized, field)
  return nil unless available_locales != nil

  available_locales.each do |l|
    if localized[l].key?(field)
      return localized[l][field]
    end
  end

  return nil
end

.localized_graphic_list_paths(available_locales, localized, field) ⇒ Object

Similar to localized_graphic_path, but prefixes each item in the resulting array with “chosen_locale/field/”.



177
178
179
180
181
182
183
184
185
# File 'lib/fdroid/App.rb', line 177

def self.localized_graphic_list_paths(available_locales, localized, field)
  return nil unless available_locales != nil
  available_locales.each do |l|
    if localized[l].key?(field)
      return localized[l][field].map { |val| "#{l}/#{field}/#{val}" }
    end
  end
  return nil
end

.localized_graphic_path(available_locales, localized, field) ⇒ Object

Prefixes the result with “chosen_locale/” before returning.

See Also:



163
164
165
166
167
168
169
170
171
# File 'lib/fdroid/App.rb', line 163

def self.localized_graphic_path(available_locales, localized, field)
  return nil unless available_locales != nil
  available_locales.each do |l|
    if localized[l].key?(field)
      return "#{l}/#{localized[l][field]}"
    end
  end
  return nil
end

.process_app_description(string) ⇒ Object

Any transformations which are required to turn the “description” into something which is displayable via HTML is done here (e.g. replacing “fdroid.app:” schemes, formatting new lines, etc.



123
124
125
126
127
128
129
130
# File 'lib/fdroid/App.rb', line 123

def self.process_app_description(string)
  if string == nil
    return nil
  end

  string = self.replace_fdroid_app_links(string)
  self.format_description_to_html(string)
end

Finds all “fdroid.app:” schemes in a particular string, and replaces with “/packages/”.

Parameters:

  • string (string)

Returns:

  • (string)


135
136
137
# File 'lib/fdroid/App.rb', line 135

def self.replace_fdroid_app_links(string)
  string.gsub /fdroid\.app:([\w._]*)/, '/packages/\1'
end

Instance Method Details

#descriptionObject



55
56
57
58
59
60
61
62
63
# File 'lib/fdroid/App.rb', line 55

def description
  desc = field('description') || App.localized(@available_locales, @app['localized'], 'description')

  if desc != nil
    desc = App.process_app_description(desc)
  end

  return desc
end

#iconObject



38
39
40
41
42
43
44
45
# File 'lib/fdroid/App.rb', line 38

def icon
  localized = App.localized_graphic_path(@available_locales, @app['localized'], 'icon')
  if localized
    "#{package_name}/#{localized}"
  elsif field('icon')
    "icons-640/#{field('icon')}"
  end
end

#nameObject



47
48
49
# File 'lib/fdroid/App.rb', line 47

def name
  field('name') || App.localized(@available_locales, @app['localized'], 'name')
end

#package_nameObject



30
31
32
# File 'lib/fdroid/App.rb', line 30

def package_name
  field 'packageName'
end

#suggested_version_codeObject



65
66
67
68
69
70
71
# File 'lib/fdroid/App.rb', line 65

def suggested_version_code
  code = field('suggestedVersionCode')
  if code != nil
    code = Integer(code)
  end
  return code
end

#summaryObject



51
52
53
# File 'lib/fdroid/App.rb', line 51

def summary
  field('summary') || App.localized(@available_locales, @app['localized'], 'summary')
end

#to_dataHash

Generates a hash of dumb strings to be used in templates. If a specific value is not present, then it will have a nil value. If a value can be localized, then it will choose the most appropriate translation based on @available_locales and @locale. The ‘packages’ key is an array of Package.to_data hashes.

Returns:

  • (Hash)


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
# File 'lib/fdroid/App.rb', line 79

def to_data
  {
    # These fields are taken as is from the metadata. If not present, they are
    'package_name' => package_name,
    'author_email' => field('authorEmail'),
    'author_name' => field('authorName'),
    'author_website' => field('authorWebSite'),
    'translation' => field('translation'),
    'bitcoin' => field('bitcoin'),
    'litecoin' => field('litecoin'),
    'donate' => field('donate'),
    'flattrID' => field('flattrID'),
    'liberapayID' => field('liberapayID'),
    'categories' => field('categories'),
    'anti_features' => field('anti_features'),
    'suggested_version_code' => suggested_version_code,
    'suggested_version_name' => @packages.detect { |p| p.version_code == suggested_version_code }&.version_name,
    'issue_tracker' => field('issueTracker'),
    'changelog' => field('changelog'),
    'license' => field('license'),
    'source_code' => field('sourceCode'),
    'website' => field('webSite'),
    'added' => field('added'),
    'last_updated' => field('lastUpdated'),
    'whats_new' => App.process_app_description(App.localized(@available_locales, @app['localized'], 'whatsNew')),

    'icon' => icon,
    'title' => name,
    'summary' => summary,

    'description' => description,
    'feature_graphic' => App.localized_graphic_path(@available_locales, @app['localized'], 'featureGraphic'),
    'phone_screenshots' => App.localized_graphic_list_paths(@available_locales, @app['localized'], 'phoneScreenshots'),
    'seven_inch_screenshots' => App.localized_graphic_list_paths(@available_locales, @app['localized'], 'sevenInchScreenshots'),

    'packages' => @packages.sort.reverse.map { |p| p.to_data },

    'beautiful_url' => "/packages/#{package_name}"
  }
end

#to_sObject



34
35
36
# File 'lib/fdroid/App.rb', line 34

def to_s
  package_name
end