Module: IuguSDKBaseController
- Defined in:
- lib/iugusdk/iugusdk_base_controller.rb
Class Method Summary collapse
Instance Method Summary collapse
- #configure_locale ⇒ Object
- #get_compatible_locale(http_accepted_languages) ⇒ Object
- #http_preferred_languages(header) ⇒ Object
- #locale_to_currency(locale) ⇒ Object
- #preferred_language_from(array, http_accepted_languages) ⇒ Object
- #select_account ⇒ Object
- #verify_api_key ⇒ Object
Class Method Details
.included(receiver) ⇒ Object
3 4 5 |
# File 'lib/iugusdk/iugusdk_base_controller.rb', line 3 def self.included(receiver) receiver.append_before_filter :configure_locale end |
Instance Method Details
#configure_locale ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/iugusdk/iugusdk_base_controller.rb', line 37 def configure_locale if(params[:hl]) locale = params[:hl] if AvailableLanguage.all.values.include? params[:hl] end unless locale @matched_locale_from_browser = get_compatible_locale( request.env['HTTP_ACCEPT_LANGUAGE'] ) if user_signed_in? if current_user.locale.blank? locale = "en" else locale = current_user.locale end else locale = @matched_locale_from_browser end else @matched_locale_from_browser = locale end I18n.locale = locale end |
#get_compatible_locale(http_accepted_languages) ⇒ Object
33 34 35 |
# File 'lib/iugusdk/iugusdk_base_controller.rb', line 33 def get_compatible_locale(http_accepted_languages) preferred_language_from( AvailableLanguage.all.values, http_accepted_languages) end |
#http_preferred_languages(header) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/iugusdk/iugusdk_base_controller.rb', line 11 def http_preferred_languages(header) @user_preferred_languages ||= header.split(/\s*,\s*/).collect do |l| if l =~ /\?\?\?/i ["none",0] else l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/ l.split(';q=') end end.sort do |x,y| raise "Not correctly formatted" unless x.first =~ /^[a-z\-]+$/i y.last.to_f <=> x.last.to_f end.collect do |l| l.first.downcase.gsub(/-[a-z]+$/i) { |x| x.upcase } end rescue Exception => e [] end |
#locale_to_currency(locale) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/iugusdk/iugusdk_base_controller.rb', line 62 def locale_to_currency(locale) { 'pt-BR' => "BRL", 'en' => "USD" }[locale.to_s] end |
#preferred_language_from(array, http_accepted_languages) ⇒ Object
29 30 31 |
# File 'lib/iugusdk/iugusdk_base_controller.rb', line 29 def preferred_language_from(array, http_accepted_languages) (http_preferred_languages(http_accepted_languages) & array.collect { |i| i.to_s }).first end |
#select_account ⇒ Object
7 8 9 |
# File 'lib/iugusdk/iugusdk_base_controller.rb', line 7 def select_account set_account(current_user) if current_user end |
#verify_api_key ⇒ Object
58 59 60 |
# File 'lib/iugusdk/iugusdk_base_controller.rb', line 58 def verify_api_key raise ActionController::RoutingError.new("iws_api_key missing") unless IuguSDK::iws_api_key end |