Showing posts with label Ruby (programming language). Show all posts
Showing posts with label Ruby (programming language). Show all posts

Tuesday, August 30, 2016

Ruby: Print Out Multiple Lines

Command:

$ printf "1,name1,address1\n2,name2,address2" | ruby -e "while status = STDIN.gets do = STDIN.gets do
print status
end"


Result:

1,name1,address1
2,name2,address2

Friday, February 5, 2016

Version of Ruby

$ ruby --version
ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]

Friday, January 29, 2016

Cron: Tweet a Line Every 10-19 Minutes

Cron - Tweet a line every 30-40 mins:

*/10 * * * * perl -le 'sleep rand 540' && head -$[${RANDOM} \% `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/lines.rb

lines.rb - using standard input:

require 'json'
require 'oauth'

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)

# STATUS
status = STDIN.gets

# POST
response = endpoint.post('https://api.twitter.com/1.1/statuses/update.json', status: status )
result = JSON.parse(response.body)

Getting Random Line Number -1

Bash line to generate random line number from 1 to max - 1.
echo $(ruby -e 'print rand') $(wc -l < "./lines.txt") | awk '{printf("%d\n", $1*$2)}'

Thursday, January 28, 2016

Ruby: Tweeting Using OAuth gem

Package needed:

gem install oauth

Ruby script: tweet.rb

require 'json'
require 'oauth'

consumer_key = '' # Your consumer key
consumer_secret = '' # Your consumer secret
access_token = '' # Your access token
access_token_secret = '' # Your access token secret

consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)

# STATUS
status = ARGV[0]

# POST
response = endpoint.post('https://api.twitter.com/1.1/statuses/update.json', status: status )
result = JSON.parse(response.body)



Execute the ruby script:

ruby ./tweet.rb "Good morning! (おはようございます。)"