4
\$\begingroup\$

This is my first attempt at creating a Ruby script. I set up a Cron job to run this script every hour. I'm requesting constructive critiques and suggestions for making this better.

Sidenote: I've hard coded the location to my hometown. I don't plan on changing it to another location.

Here's an example of the expected output/tweet.

#!/usr/bin/env ruby
require 'twitter'
require 'forecast_io'
require 'gemoji'

client = Twitter::REST::Client.new do |c|
  c.consumer_key        = 'CONSUMER_KEY'
  c.consumer_secret     = 'CONSUMER_SECRET'
  c.access_token        = 'ACCESS_TOKEN'
  c.access_token_secret = 'ACCESS_TOKEN_SECRET'
end

ForecastIO.api_key = 'FORECAST_API_SECRET'
location           = ForecastIO.forecast(27.770, -82.638)
current_weather    = location.currently

icon =
  case current_weather.icon
  when 'clear-day'           then Emoji.find_by_alias('sunny').raw
  when 'clear-night'         then Emoji.find_by_alias('milky-way').raw
  when 'rain'                then Emoji.find_by_alias('umbrella').raw
  when 'snow'                then Emoji.find_by_alias('snowflake').raw
  when 'sleet'               then Emoji.find_by_alias('snowman').raw
  when 'wind'                then Emoji.find_by_alias('cyclone').raw
  when 'fog'                 then Emoji.find_by_alias('foggy').raw
  when 'cloudy'              then Emoji.find_by_alias('cloud').raw
  when 'partly-cloudy-day'   then Emoji.find_by_alias('partly_sunny').raw
  when 'partly-cloudy-night' then Emoji.find_by_alias('partly_sunny').raw
  when 'hail'                then Emoji.find_by_alias('cat').raw
  when 'thunderstorm'        then Emoji.find_by_alias('zap').raw
  when 'tornado'             then Emoji.find_by_alias('bangbang').raw
  else Emoji.find_by_alias('sunny').raw
  end

tweet = "Currently in #StPete it's #{current_weather.temperature.round}° & #{current_weather.summary.downcase} #{icon}"

client.update(tweet)
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

It looks pretty good to me. Just some minor notes:

  • Not that it's bad or anything, but in Ruby it's not usual to align code.

  • You can DRY the case block by creating a hash:

    emojis_by_icon = {
      'clear-day' => 'sunny', 
      'clear-night' => 'milky-way',
      ...
    }
    
    emoji_alias = emojis_by_icon.fetch(current_weather.icon, 'sunny')
    icon = Emoji.find_by_alias(emoji_alias).raw
    
  • tweet = ... With long lines, I prefer to use the String#% interpolation:

    tweet = "Currently it's %{temperature}° & %{summary} %{icon}" % {
      temperature: current_weather.temperature.round, 
      summary: current_weather.summary.downcase,
      icon: icon,
    }
    
\$\endgroup\$
1
  • \$\begingroup\$ Nice tip on the string interpolation. I haven't seen it used that way before. Thanks! \$\endgroup\$
    – Tony Gaeta
    Oct 17, 2016 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.