HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

A Twitter bot that fetches Forecast.io data and tweets out the current weather with text and emojis

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thewithweathertextforecastbotfetchesemojisthatcurrent

Problem

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)

Solution

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,
}

Code Snippets

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 = "Currently it's %{temperature}° & %{summary} %{icon}" % {
  temperature: current_weather.temperature.round, 
  summary: current_weather.summary.downcase,
  icon: icon,
}

Context

StackExchange Code Review Q#144482, answer score: 4

Revisions (0)

No revisions yet.