In my rails app I want to use country-code, currency-code, ISO locale code to fetch some data from API. How can I get this information dynamically when user visit my site from anywhere?
I have used geocoder gem so by request.location I will get location's information and using this gem I can get country-code. Now I am not getting how can I get remaining information such as currency-code & ISO locale code?? Can anyone please help me or guide me??
I have seen this money gem but not sure it will provide me all these information.
Thanks in advance :)
I have tried #Prakash Murthy's answer. But there are many issue in this http://www.currency-iso.org/dam/downloads/table_a1.xml I found there is not proper name of all countries and some country has multiple currency_code which made me confused. But finally I found the solution by this single countries gem without creating any database.
Here is how I achieved the solution:
country_name = request.location.data['country_name'] # got country name
c = Country.find_country_by_name(country_name) # got currency details
currency_code = c.currency['code'] # got currency code
Sorry to answer my own question but I have posted here so in future if anyone stuck like me for the same issue then his/her time not wasted.
I found a way that makes it really easy:
Add currency gem to Gemfile, then bundle install
def currency_for_country(currency_iso_code)
ISO3166::Country.new(currency_iso_code).currency_code
end
Then:
currency_for_country('US')
=> "USD"
currency_for_country('AU')
=> "AUD"
This info is based off the countries gem readme
currency-code & ISO locale code are static data which change very rarely - if at all, and are best handled as static information within the system by storing them within the database tables. Might even be a good idea to provide a CRUD interface for managing these data.
One possible source for Currency code : http://www.currency-iso.org/en/home/tables/table-a1.html
List of All Locales and Their Short Codes? has details about getting the list of all locale codes.
Related
#zipcode = ZipCodes.identify(params[:zip_code])
I am new to rails. Can anyone tell me what does the keyword 'identify' do. Is this related to active record query?
Thank you in advance.
The gem zip-codes provides the ZipCodes class with a identify class method that simply reads a zip code and returns the state, city and timezone.
ZipCodes.identify('30301')
# => {:state_code=>"GA", :state_name=>"Georgia", :city=>"Atlanta", :time_zone=>"America/New_York"}
How do I/you know this? I guess you are new to the project where you saw this line:
First look inside your project? Do you have a class called ZipCodes? Yes, look in there.
No, look into your Gemfile, do you have anything mentioning ZipCodes or similar? Yes, search for the gem documentation.
No, Google a code snippet ZipCodes.identify and search among the results.
Here is the documentation for zip-codes: https://github.com/monterail/zip-codes
I have a lot of members in database and I want them sort in Rails by last_name, but the trick is that last name contain croatian letters like (Č,Ć,Š,Đ,Ž).
Some of last_names are for example: Antić, Čekić, Živad, Đurak, Perić...
I used ffi-locale gem but i failed to sort it. So any help and advice is good!
If your database has a correct configuration, this should work:
Member.sort(last_name: :asc).all
If you want to sort the members in your application, after fetching them from the database, you could use sort function (it works fine with utf strings):
members.sort_by!(&:last_name)
I have the following error message on my en.yml file:
invalid_transition: "Cannot %{action} a %{state} timesheet"
Problem is that sometimes the state can be approved, other times it can be rejected.
With that, the error can end up being mispelled like "Cannot submit a approved timesheet", instead of "an approved timesheet".
Does Rails provide this flexibility in I18n?
The most simple answer to your question, I think, would be to pass in the entire state with the correct indefinite article together.
There's a question that looks at how to prepend "a" or "an" depending on a given word. A short answer is that there's a gem indefinite_article that does it.
Your translation then becomes:
invalid_transition: "Cannot %{action} %{state_with_article} timesheet"
Then call the I18n.t and pass in "a rejected" or "an approved" as a variable to interpolate.
However, if you want to get your hands a bit dirtier you may be able to use the i18n-inflector gem and it's companion i18n-inflector-rails gem. For many languages the choice is more complicated than in English because of different genders and tenses affecting the choice of indefinite article. (Disclaimer: I've not used either of those gems but they look like they would help you solve the problem).
I'm sure this has to do with the complexity of Spree's loading.
But my main problem is that Spree can not load a Country. For me it,s Country.find(214). If I check it in my remote console, I find it no worries. All the countries, all the states are there.
But if I try to set Country.find(214) in the controller for states_controller#index, or make a before_load method that does that, or put it in the view itself, it always returns :Error (Couldn't find Country with ID=214).
Crazy, right? I can't think of what to do at this point. If I do Country = Country.first. I can sort of get it to work by just loading an empty template of index.haml. So that means that some sort of country exists that it is tapping into.
Anyone have any theoretical thoughts as to why this is happening? And how I might be able to circumvent it?
Sorry this is a little late, but I stumbled onto your post and figured out what was causing the problem.
Country 214 is USA, and for some reason Spree defaults to that country. Which means that if you don't have USA loaded, you'll hit into that problem.
To get around it, you'll have to manually set up a default country in your initializer:
Spree.config do |config|
Spree::Config.default_country_id = Spree::Country.find_by_name("Singapore").id
end
I hope you've solved this by now though. :)
Its Pretty much easy in Spree2.0.0 should work for every Spree version too.
Spree.config do |config| # Set Country name and Currency like this(Note: you will need to run 'rake db:seed' before this. Change country name in Spree::Country.find_by_name('United Kingdom') replace united kingdom to your desire one)
config.currency = 'EUR'
country = Spree::Country.find_by_name('United Kingdom')
config.default_country_id = country.id if country.present?
# You can also set following options too.
config.site_name = "Teamer Store"
config.override_actionmailer_config = true
config.enable_mail_delivery = true
end
I'm writing an app for a company that uses Google Calendar internally and would need to use events they already have in their calendar in the app. So I need to get read only access to their calendars from the app (namely I need the events title, start and end dates and attendee emails for all future events).
What is the simplest way to do this in ruby (I would need it to work relatively seamlessly on Heroku)?
I tried using the GCal4Ruby gem which seemed the least outdated of the ones I found but I'm unable to even authenticate through the library (HTTPRequestFailed - Captcha required error) let alone get the info I need.
Clarification: What I'm talking about is the Google Apps version of the calendar, not the one at calendar.google.com.
OK I got the api via GCal4Ruby working. I'm not exactly sure what went wrong the first time. Thanks to Mike and James for their suggestions. This is sample code I used for anyone interested:
require "rubygems"
require "gcal4ruby"
serv = GCal4Ruby::Service.new
serv.authenticate "username#example.com", "password"
events = GCal4Ruby::Event.find serv, {'start-min' => Time.now.utc.xmlschema,
:calendar => 'example-cal%40example.com'}
events.each do |event|
puts event.title
puts event.attendees.join ", "
puts event.start_time
puts event.end_time
puts '-----------------------'
end
You should be able to use the Google Calendar private xml address feature to pull out the needed data.
You could then parse it with hpricot or nokogiri to extract whatever fields you need.