Skip to content

Instantly share code, notes, and snippets.

@Hiromi-Kai
Forked from anonymous/namechanger.rb
Last active December 27, 2015 16:19
Show Gist options
  • Save Hiromi-Kai/7353988 to your computer and use it in GitHub Desktop.
Save Hiromi-Kai/7353988 to your computer and use it in GitHub Desktop.
original source by @sasamijp
# -*- encoding: utf-8 -*-
require 'twitter'
class UpdateString < String
def delete_account_name(sasamljp)
self.sub("(#{sasamljp})",'')
end
def delete_at_mark
self.gsub(/(@|@)/,'')
end
def escape_text
CGI.unescapeHTML(self)
end
end
class NameChanger
def initialize(status)
@status=status
@sasamljp='@sasamijp'
@text=UpdateString.new(status.text)
@user=status.user
end
public
def update
if is_change_order?
@[email protected]_account_name(@sasamljp).delete_at_mark.escape_text
send_update(@text)
end
end
private
def send_update(update_text)
Twitter.update_profile(name:update_text)
Twitter.update(get_notice_text(update_text),{'in_reply_to_status_id'=>status.id.to_s})
end
def get_notice_text(text)
"@#{@user.screen_name} #{text}に改名しました"
end
def is_change_order?
include_reply? and not is_unofficial_retweet?
end
def include_reply?
@text.include?("(#{@sasamljp})")
end
def is_unofficial_retweet?
@text.include?('RT')
end
end
# -*- encoding: utf-8 -*-
require 'rspec'
require './namechanger'
describe 'UpdateString Test' do
it 'リプライが消せる' do
update_string=UpdateString.new('ササムルジェイピー(@sasamijp)')
update_string.delete_account_name('@sasamijp').should eq 'ササムルジェイピー'
end
it 'アットマークが消える' do
update_string=UpdateString.new('@sasamijp')
update_string.delete_at_mark.should eq 'sasamijp'
end
it 'エスケープできる' do
update_string=UpdateString.new('&lt;sasamijp&gt;')
update_string.escape_text.should eq '<sasamijp>'
end
it 'メソッドチェーンできる' do
update_string=UpdateString.new('&lt;&lt;&lt;&lt;@@@@@@ササムルジェイピー(@sasamijp)')
update_string.delete_account_name('@sasamijp').delete_at_mark.escape_text.should eq '<<<<ササムルジェイピー'
end
end
# -*- encoding: utf-8 -*-
require 'rubygems'
require 'tweetstream'
require 'twitter'
require './namechanger'
require './key.rb'
Twitter.configure do |config|
config.consumer_key = Const::CONSUMER_KEY
config.consumer_secret = Const::CONSUMER_SECRET
config.oauth_token = Const::ACCESS_TOKEN
config.oauth_token_secret = Const::ACCESS_TOKEN_SECRET
end
TweetStream.configure do |config|
config.consumer_key = Const::CONSUMER_KEY
config.consumer_secret = Const::CONSUMER_SECRET
config.oauth_token = Const::ACCESS_TOKEN
config.oauth_token_secret = Const::ACCESS_TOKEN_SECRET
config.auth_method = :oauth
end
client = TweetStream::Client.new
client.userstream do |status|
name_changer=NameChanger.new(status)
name_changer.update
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment