Class: FDroid::App
- Inherits:
-
Object
- Object
- FDroid::App
- Defined in:
- lib/fdroid/App.rb
Class Method Summary collapse
-
.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).
-
.format_description_to_html(string) ⇒ Object
Ensure double newlines “nn” are converted to “<br />” tags.
- .localized(available_locales, localized, field) ⇒ string
-
.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/”.
-
.localized_graphic_path(available_locales, localized, field) ⇒ Object
Prefixes the result with “chosen_locale/” before returning.
-
.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..
-
.replace_fdroid_app_links(string) ⇒ string
Finds all “fdroid.app:” schemes in a particular string, and replaces with “/packages/”.
Instance Method Summary collapse
- #description ⇒ Object
- #icon ⇒ Object
-
#initialize(app, packages, locale) ⇒ App
constructor
A new instance of App.
- #name ⇒ Object
- #package_name ⇒ Object
- #suggested_version_code ⇒ Object
- #summary ⇒ Object
-
#to_data ⇒ Hash
Generates a hash of dumb strings to be used in templates.
- #to_s ⇒ Object
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.
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 238 239 240 |
# File 'lib/fdroid/App.rb', line 210 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 double newlines “nn” are converted to “<br />” tags.
138 139 140 141 142 |
# File 'lib/fdroid/App.rb', line 138 def self.format_description_to_html(string) string .gsub("\n\n", '<br />') .gsub(/\r?\n/, ' ') end |
.localized(available_locales, localized, field) ⇒ string
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/fdroid/App.rb', line 148 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/”.
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/fdroid/App.rb', line 178 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.
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/fdroid/App.rb', line 162 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.
121 122 123 124 125 126 127 128 |
# File 'lib/fdroid/App.rb', line 121 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 |
.replace_fdroid_app_links(string) ⇒ string
Finds all “fdroid.app:” schemes in a particular string, and replaces with “/packages/”.
133 134 135 |
# File 'lib/fdroid/App.rb', line 133 def self.replace_fdroid_app_links(string) string.gsub /fdroid\.app:([\w._]*)/, '/packages/\1' end |
Instance Method Details
#description ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/fdroid/App.rb', line 55 def description desc = App.localized(@available_locales, @app['localized'], 'description') || field('description') if desc != nil desc = App.process_app_description(desc) end return desc end |
#icon ⇒ Object
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}" else "icons-640/#{field('icon')}" end end |
#name ⇒ Object
47 48 49 |
# File 'lib/fdroid/App.rb', line 47 def name App.localized(@available_locales, @app['localized'], 'name') || field('name') end |
#package_name ⇒ Object
30 31 32 |
# File 'lib/fdroid/App.rb', line 30 def package_name field 'packageName' end |
#suggested_version_code ⇒ Object
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 |
#summary ⇒ Object
51 52 53 |
# File 'lib/fdroid/App.rb', line 51 def summary App.localized(@available_locales, @app['localized'], 'summary') || field('summary') end |
#to_data ⇒ Hash
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.
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 |
# File 'lib/fdroid/App.rb', line 80 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'), 'bitcoin' => field('bitcoin'), 'donate' => field('donate'), 'flattr' => field('flattr'), '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_s ⇒ Object
34 35 36 |
# File 'lib/fdroid/App.rb', line 34 def to_s package_name end |