Skip to content

Commit

Permalink
Adding rubocop style preferences for project, and ensuring consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
rposborne committed Sep 20, 2016
1 parent 4a8ebf4 commit 44bb45d
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 244 deletions.
18 changes: 18 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-09-19 23:23:44 -0400 using RuboCop version 0.43.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 101

# Offense count: 64
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
# URISchemes: http, https
Metrics/LineLength:
Max: 483
52 changes: 24 additions & 28 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,29 @@ task :update_yaml_structure do

d = Dir['lib/countries/data/subdivisions/*.yaml']
d.each do |file|

puts "checking : #{file}"
data = YAML.load_file(file)
country_key = File.basename(file, ".yaml")


data = data.inject({}) do |a, (k,subd)|
a[k] ||= {}
a[k]["unofficial_names"] = subd.delete("names")
a[k]["translations"] = {'en' => subd['name']}
a[k]["geo"] = {
"latitude" => subd.delete("latitude"),
"longitude" => subd.delete("longitude"),
"min_latitude" => subd.delete("min_latitude"),
"min_longitude" => subd.delete("min_longitude"),
"max_latitude" => subd.delete("max_latitude"),
"max_longitude" => subd.delete("max_longitude")
}

a[k] = a[k].merge(subd)
a
end
File.open(file, 'w') { |f| f.write data.to_yaml }
begin
rescue
puts "failed to read #{file}: #{$ERROR_INFO}"

data = data.each_with_object({}) do |(k, subd), a|
a[k] ||= {}
a[k]['unofficial_names'] = subd.delete('names')
a[k]['translations'] = { 'en' => subd['name'] }
a[k]['geo'] = {
'latitude' => subd.delete('latitude'),
'longitude' => subd.delete('longitude'),
'min_latitude' => subd.delete('min_latitude'),
'min_longitude' => subd.delete('min_longitude'),
'max_latitude' => subd.delete('max_latitude'),
'max_longitude' => subd.delete('max_longitude')
}

a[k] = a[k].merge(subd)
end
File.open(file, 'w') { |f| f.write data.to_yaml }
begin
rescue
puts "failed to read #{file}: #{$ERROR_INFO}"
end
end
end

Expand All @@ -53,9 +49,9 @@ task :update_cache do
require 'yaml'
require 'i18n_data'

codes = Dir['lib/countries/data/countries/*.yaml'].map {|x| File.basename(x, File.extname(x))}.uniq
codes = Dir['lib/countries/data/countries/*.yaml'].map { |x| File.basename(x, File.extname(x)) }.uniq
data = {}
empty_translations_hash = {}

corrections = YAML.load_file(File.join(File.dirname(__FILE__), 'lib', 'countries', 'data', 'translation_corrections.yaml')) || {}

I18nData.languages.keys.each do |locale|
Expand Down Expand Up @@ -91,7 +87,7 @@ Geocoder.configure(always_raise: :all)
# @param [String] query string to geocode
# @return [Hash] first valid result or nil
def geocode(query)
Retryable.retryable(tries: 3, sleep: lambda { |n| 2**n }) do
Retryable.retryable(tries: 3, sleep: ->(n) { 2**n }) do
Geocoder.search(query).first
end
rescue => e
Expand Down Expand Up @@ -128,6 +124,6 @@ task :fetch_subdivisions do
state_data[code]['geo']['max_longitude'] = geometry['bounds']['northeast']['lng']
end
# Write updated YAML for current country
File.open(File.join(File.dirname(__FILE__), 'lib','countries', 'data', 'subdivisions', "#{c.alpha2}.yaml"), 'w+') { |f| f.write state_data.to_yaml }
File.open(File.join(File.dirname(__FILE__), 'lib', 'countries', 'data', 'subdivisions', "#{c.alpha2}.yaml"), 'w+') { |f| f.write state_data.to_yaml }
end
end
6 changes: 3 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "countries"
require 'bundler/setup'
require 'countries'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +10,5 @@ require "countries"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start
1 change: 0 additions & 1 deletion countries.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ Gem::Specification.new do |gem|
gem.add_dependency('unicode_utils', '~> 1.4')
gem.add_development_dependency('rspec', '>= 3')
gem.add_development_dependency('activesupport', '>= 3')

end
4 changes: 2 additions & 2 deletions lib/countries.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "unicode_utils/downcase"
require "sixarm_ruby_unaccent"
require 'unicode_utils/downcase'
require 'sixarm_ruby_unaccent'

require 'countries/version'

Expand Down
8 changes: 4 additions & 4 deletions lib/countries/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def initialize

def default_locales
locales = if Object.const_defined?('I18n') && I18n.respond_to?(:available_locales)
I18n.available_locales
else
[:en]
end
I18n.available_locales
else
[:en]
end

locales.empty? ? [:en] : locales
end
Expand Down
34 changes: 17 additions & 17 deletions lib/countries/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class Country
include Emoji
attr_reader :data

ISO3166::DEFAULT_COUNTRY_HASH.each do |method_name, type|
ISO3166::DEFAULT_COUNTRY_HASH.each do |method_name, _type|
define_method method_name do
data[method_name.to_s]
end
end

ISO3166::DEFAULT_COUNTRY_HASH["geo"].each do |method_name, type|
ISO3166::DEFAULT_COUNTRY_HASH['geo'].each do |method_name, _type|
define_method method_name do
data["geo"][method_name.to_s]
data['geo'][method_name.to_s]
end
end

Expand All @@ -25,11 +25,11 @@ def valid?
!(data.nil? || data.empty?)
end

alias_method :zip, :postal_code
alias_method :zip?, :postal_code
alias_method :postal_code?, :postal_code
alias_method :languages, :languages_official
alias_method :names, :unofficial_names
alias zip postal_code
alias zip? postal_code
alias postal_code? postal_code
alias languages languages_official
alias names unofficial_names

def ==(other)
other == data
Expand All @@ -53,7 +53,7 @@ def subdivisions
end
end

alias_method :states, :subdivisions
alias states subdivisions

def in_eu?
data['eu_member'].nil? ? false : data['eu_member']
Expand Down Expand Up @@ -89,20 +89,20 @@ def local_name

def reload
@data = if @country_data_or_code.is_a?(Hash)
@country_data_or_code
else
ISO3166::Data.new(@country_data_or_code).call
end
@country_data_or_code
else
ISO3166::Data.new(@country_data_or_code).call
end
end

private

def subdivision_data
@subdivision_data ||= if subdivisions?
YAML.load_file(subdivision_file_path)
else
{}
end
YAML.load_file(subdivision_file_path)
else
{}
end
end

def subdivision_file_path
Expand Down
17 changes: 7 additions & 10 deletions lib/countries/country/class_methods.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module ISO3166
def ISO3166::Country(country_data_or_country)
def self::Country(country_data_or_country)
case country_data_or_country
when ISO3166::Country
country_data_or_country
when String, Symbol
ISO3166::Country.search(country_data_or_country)
else
fail TypeError, "can't convert #{country_data_or_country.class.name} into ISO3166::Country"
raise TypeError, "can't convert #{country_data_or_country.class.name} into ISO3166::Country"
end
end

Expand All @@ -24,16 +24,14 @@ def all(&blk)
ISO3166::Data.cache.map(&blk)
end

alias_method :countries, :all
alias countries all

def all_translated(locale = 'en')
translations(locale).values
end

def all_names_with_codes(locale = 'en')
ISO3166::Country.all.map do |c|
[(c.translation(locale) || c.name).html_safe, c.alpha2]
end.sort_by { |d| d[0] }
Country.all.map { |c| [(c.translation(locale) || c.name).html_safe, c.alpha2] }.sort
end

def translations(locale = 'en')
Expand All @@ -42,7 +40,7 @@ def translations(locale = 'en')

def search(query)
country = new(query.to_s.upcase)
(country && country.valid?) ? country : nil
country && country.valid? ? country : nil
end

def [](query)
Expand All @@ -62,7 +60,7 @@ def find_all_by(attribute, val)

ISO3166::Data.cache.select do |_, v|
attributes.map do |attr|
Array(Country.new(v).send(attr)).any? { |n| value === sterlize_value(n)}
Array(Country.new(v).send(attr)).any? { |n| value === sterlize_value(n) }
end.include?(true)
end
end
Expand All @@ -78,7 +76,7 @@ def sterlize_value(v)
protected

def parse_attributes(attribute, val)
fail "Invalid attribute name '#{attribute}'" unless instance_methods.include?(attribute.to_sym)
raise "Invalid attribute name '#{attribute}'" unless instance_methods.include?(attribute.to_sym)

attributes = Array(attribute.to_s)
if attributes == ['name']
Expand All @@ -96,5 +94,4 @@ def find_by(attribute, value, obj = nil)
end
end
end

end
55 changes: 28 additions & 27 deletions lib/countries/country/emoji.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
module ISO3166
module Emoji
CODE_POINTS = {
"a" => "🇦",
"b" => "🇧",
"c" => "🇨",
"d" => "🇩",
"e" => "🇪",
"f" => "🇫",
"g" => "🇬",
"h" => "🇭",
"i" => "🇮",
"j" => "🇯",
"k" => "🇰",
"l" => "🇱",
"m" => "🇲",
"n" => "🇳",
"o" => "🇴",
"p" => "🇵",
"q" => "🇶",
"r" => "🇷",
"s" => "🇸",
"t" => "🇹",
"u" => "🇺",
"v" => "🇻",
"w" => "🇼",
"x" => "🇽",
"y" => "🇾",
"z" => "🇿" }.freeze
'a' => '🇦',
'b' => '🇧',
'c' => '🇨',
'd' => '🇩',
'e' => '🇪',
'f' => '🇫',
'g' => '🇬',
'h' => '🇭',
'i' => '🇮',
'j' => '🇯',
'k' => '🇰',
'l' => '🇱',
'm' => '🇲',
'n' => '🇳',
'o' => '🇴',
'p' => '🇵',
'q' => '🇶',
'r' => '🇷',
's' => '🇸',
't' => '🇹',
'u' => '🇺',
'v' => '🇻',
'w' => '🇼',
'x' => '🇽',
'y' => '🇾',
'z' => '🇿'
}.freeze

def emoji_flag
alpha2.downcase.chars.map { |c| CODE_POINTS[c] }.join("")
alpha2.downcase.chars.map { |c| CODE_POINTS[c] }.join('')
end
end
end
12 changes: 5 additions & 7 deletions lib/countries/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class << self
def register(data)
alpha2 = data[:alpha2].upcase
@@registered_data[alpha2] = \
data.inject({}) { |a,(k,v)| a[k.to_s] = v; a }
@@registered_data[alpha2]["translations"] = \
data.each_with_object({}) { |(k, v), a| a[k.to_s] = v }
@@registered_data[alpha2]['translations'] = \
Translations.new.merge(data[:translations] || {})
@@cache = cache.merge(@@registered_data)
end
Expand Down Expand Up @@ -51,11 +51,9 @@ def update_cache
@@cache
end

private

def load_data!
return @@cache unless @@cache.size == loaded_codes || @@cache.keys.empty?
@@cache = load_cache %w(cache countries.json )
@@cache = load_cache %w(cache countries.json)
@@cache = @@cache.merge(@@registered_data)
@@cache
end
Expand Down Expand Up @@ -84,7 +82,7 @@ def internal_codes
end

def cache_flush_required?
locales_to_load.size != 0 || locales_to_remove.size != 0
!locales_to_load.empty? || !locales_to_remove.empty?
end

def locales_to_load
Expand Down Expand Up @@ -123,7 +121,7 @@ def unload_translations(locale)

def load_cache(file_array)
file_path = datafile_path(file_array)
File.exist?(file_path) ? JSON.load(File.binread(file_path)) : {}
File.exist?(file_path) ? JSON.parse(File.binread(file_path)) : {}
end

def datafile_path(file_array)
Expand Down
Loading

0 comments on commit 44bb45d

Please sign in to comment.