Class: Nimbleshop::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/nimbleshop/nimbleshop/util.rb

Class Method Summary collapse

Class Method Details

.config2hash(file) ⇒ Object

loads a yaml based configuration file and returns hash


5
6
7
8
9
10
11
12
13
# File 'lib/nimbleshop/nimbleshop/util.rb', line 5

def self.config2hash(file)
  hash = YAML.load(ERB.new(File.read(file)).result)

  common_hash = hash['common'] || {}
  env_hash = hash[Rails.env.to_s] || {}

  final_hash = common_hash.deep_merge(env_hash)
  Hashr.new(final_hash)
end

.countries_with_shipping_zoneObject


52
53
54
# File 'lib/nimbleshop/nimbleshop/util.rb', line 52

def self.countries_with_shipping_zone
  Nimbleshop::Util.name_and_code_of_countries_with_subregions.select { |_, t| CountryShippingZone.all_country_codes.include?(t)}
end

.countries_without_shipping_zoneObject


40
41
42
# File 'lib/nimbleshop/nimbleshop/util.rb', line 40

def self.countries_without_shipping_zone
  Nimbleshop::Util.name_and_code_of_countries_with_subregions.reject { |_, t| CountryShippingZone.all_country_codes.include?(t) }
end

.disabled_shipping_zone_countriesObject


44
45
46
# File 'lib/nimbleshop/nimbleshop/util.rb', line 44

def self.disabled_shipping_zone_countries
  countries_with_shipping_zone.reduce([]) { |result, element| result << [element[0], element[1], {disabled: :disabled}]}
end

.in_cents(amount) ⇒ Object

Converts the amount in cents and returns an integer.


16
17
18
# File 'lib/nimbleshop/nimbleshop/util.rb', line 16

def self.in_cents(amount)
  (BigDecimal(amount.to_s) * 100).round(0).to_i
end

.localhost2public_url(url, protocol) ⇒ Object

returns public url for a given localhost url

Usage: localhost2public_url ( '/nimbleshop_paypal/notify', 'http' )


24
25
26
27
28
29
30
31
32
# File 'lib/nimbleshop/nimbleshop/util.rb', line 24

def self.localhost2public_url(url, protocol)
  if Nimbleshop.config.use_localhost2public_url
    public_url = Nimbleshop.config.localhost2public_url
    raise "localhost2public_url value is #{public_url}. It must start with http" unless public_url =~ /^http/
    public_url + url
  else
    url
  end
end

.name_and_code_of_countries_with_subregionsObject

returns an array like this [“Timor-Leste”, “TL”], [“Turkmenistan”, “TM”], [“Tunisia”, “TN”], [“Tonga”, “TO”], .……


36
37
38
# File 'lib/nimbleshop/nimbleshop/util.rb', line 36

def self.name_and_code_of_countries_with_subregions
  Carmen::Country.all.select { |c| c.subregions? }.map { |t| [t.name, t.alpha_2_code] }
end

.unconfigured_shipping_zone_countriesObject


48
49
50
# File 'lib/nimbleshop/nimbleshop/util.rb', line 48

def self.unconfigured_shipping_zone_countries
  (Nimbleshop::Util.disabled_shipping_zone_countries + Nimbleshop::Util.countries_without_shipping_zone).sort
end