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

Entering and authorizing Twitter credentials

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

Problem

I'm fairly new to Ruby, and I've been refactoring this code (it's a silly app, but it's helping me learn) as I learn more and more best practices and features new to Ruby 1.9 (e.g., hashrocket alternative). I figure having more experienced Rubyists give feedback on the short program will help me learn faster. So, with that in mind, is there anything in this code that isn't a best practice, or anything not so clean about it?

#!/usr/bin/env ruby

require 'twitter'

# Enter your creds
creds = {username:       'YourUsername',
         consum_key:     'SomeLongKeyBlahBlah', 
         consum_secret:  'AnotherLongKeyButASecretOneThisTime',
         oauth_token:    'OAuthTokenKeyBlahBlah',
         oauth_secret:   'OAuthSecretTokenOfSecrets'}

# Twitter auth
def twitterAuth(creds)
  Twitter.configure do |config|
      config.consumer_key       = creds[:consum_key] 
      config.consumer_secret    = creds[:consum_secret]
      config.oauth_token        = creds[:oauth_token]
      config.oauth_token_secret = creds[:oauth_secret]
  end
end

def sendTweet(creds)
  # Make string
  msg = '@' + creds[:username] + ' ' + ARGV.join(' ') 
  # Send!
  Twitter.update(msg) 
  # Return tweet
  puts 'Your tweet: ' + msg 
end

if __FILE__ == $0
  twitterAuth(creds)
  sendTweet(creds)
end

Solution

Generally Rubyists define methods in the underscore-lowercase pattern: def twitter_auth, def send_tweet. Additionally most that I've encountered prefer to drop the () from method calls: twitter_authenticate creds; send_tweet creds

Otherwise this looks OK!

Context

StackExchange Code Review Q#8710, answer score: 5

Revisions (0)

No revisions yet.