Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created April 19, 2012 07:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rummelonp/2419318 to your computer and use it in GitHub Desktop.
Save rummelonp/2419318 to your computer and use it in GitHub Desktop.
Ruby で Tweet するやつ
# -*- coding: utf-8 -*-
require 'rubygems'
require 'twitter'
require 'oauth'
require 'yaml'
require 'thor/group'
class Tweet < Thor::Group
include Thor::Actions
CREDENTIALS_PATH = File.expand_path '~/.tweet.yml'
def authenticate
return if File.exists? CREDENTIALS_PATH
@credentials = {}
@credentials[:consumer_key] = ask 'consumer key を入れてにゃん:'
@credentials[:consumer_secret] = ask 'consumer secret 入れてにゃん:'
consumer = OAuth::Consumer.new(*@credentials.values, :site => 'https://api.twitter.com')
request_token = consumer.get_request_token
system 'open', request_token.authorize_url
pin = ask 'pin を入れてにゃん:'
access_token = request_token.get_access_token :auth_verifier => pin
@credentials[:oauth_token] = access_token.token
@credentials[:oauth_token_secret] = access_token.secret
create_file CREDENTIALS_PATH, YAML.dump(@credentials)
chmod CREDENTIALS_PATH, 0600
end
def setup
unless @credentials
@credentials = YAML.load_file CREDENTIALS_PATH
end
@client = Twitter.new @credentials
@text = ARGV.join ' '
end
def tweet
@client.update @text
puts "アップデートしたにゃん: #{@text}"
rescue
puts "失敗したにゃん: #{$!.class} #{$!.message}"
end
end
Tweet.start ARGV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment