Top Level Namespace
Instance Method Summary collapse
- #create_default_config_file(full_path_to_config_file) ⇒ Object
- #get_access_token(consumer_key, consumer_secret) ⇒ Object
- #get_new_avatar ⇒ Object
-
#load_config(full_path) ⇒ Object
Functions ====.
- #reuse_token(config) ⇒ Object
- #save_config_file(myconfig, full_path_to_config_file) ⇒ Object
Instance Method Details
#create_default_config_file(full_path_to_config_file) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'bin/twitter-avatar-update', line 25 def create_default_config_file(full_path_to_config_file) # create our directory if it doesn't exist. # abort if we can't create the directory begin config_dir = File.dirname( full_path_to_config_file ) # get the absolute path to the config file config_filename = File.basename( full_path_to_config_file ) # get the actual file name since it should be the last element and cast it to a string Dir.mkdir( config_dir ) unless File::directory?( config_dir ) rescue abort "Can't create directory: #{config_dir}. Maybe it's already a file or exists but isn't writable by you?" end # create our YAML config file and populate it with all the stuff we'll eventually save back to it # abort if we can't create this file begin my_config = Hash.new my_config['oauth'] = Hash.new my_config['oauth']['consumer_key'] = '3LNaD1KM1s8TNgH5CBWBDA' my_config['oauth']['consumer_secret'] = 'jEP4tpHKXUZaZJrxhrgj2E2Q3SShgGvZnflPCFMA3A' #my_config['oauth']['access_token'] = false #my_config['oauth']['access_secret'] = false File.open(full_path_to_config_file, 'w') do |out| YAML::dump( my_config, out ) end rescue abort "Can't create/modify default config file: #{full_path_to_config_file}. Maybe it's already a file or exists but isn't writable by you?" end end |
#get_access_token(consumer_key, consumer_secret) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'bin/twitter-avatar-update', line 62 def get_access_token(consumer_key, consumer_secret) # returns: access token and secret as a 2-item list # you should probably do like access_token, access_secret = get_access_token # WARNING: this blocks while the user opens a browser and logs in to twitter. consumer = OAuth::Consumer.new consumer_key, consumer_secret, { :site => 'http://twitter.com/', :request_token_path => '/oauth/request_token', :access_token_path => '/oauth/access_token', :authorize_path => '/oauth/authorize'} request_token = consumer.get_request_token puts "OK!" print "Visit \"#{request_token.}\" with your web browser and authorize this app, then enter the PIN displayed: " pin = STDIN.gets.strip print "Thanks! Now, hang tight while I send this back to twitter for verification..." access_token = request_token.get_access_token(:oauth_verifier => pin) return access_token.token, access_token.secret end |
#get_new_avatar ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'bin/twitter-avatar-update', line 95 def get_new_avatar() # this function CREATES the local array 'avatars'... # and populates it with the EightShit.me avatar image URLs from Page 1 avatar_urls = Array.new #create the new global array $avatars avatar_filename = "avatar-#{Time.now.to_i.to_s}.png" # generate a filename that is hopefully unique (nix timestamp format) doc = Nokogiri::HTML( open( 'http://eightshit.me/' ) ) # open the eightshit.me page as DOM so we can parse it doc.xpath( '/html/body/ul/li/a/img' ).each do |node| # use xpath to get each image avatar_urls.push( "http://eightshit.me#{node['src']}" ) # push the image source URL onto the avatars array end single_avatar_url = avatar_urls[rand( avatar_urls.size )] # get a random avatar source URL #TODO make the following block less confusing open( single_avatar_url ) do |src| # open the avatar URL open( avatar_filename, "wb" ) do |dst| # open the local file we're saving the avatar to dst.write( src.read ) # read from the internet and write to the local avatar file end end puts "Downloaded #{single_avatar_url} as #{avatar_filename}" return open( avatar_filename, "r" ) # return a file object with our shiny new avatar end |
#load_config(full_path) ⇒ Object
Functions ====
17 18 19 20 21 22 23 |
# File 'bin/twitter-avatar-update', line 17 def load_config(full_path) begin return YAML::load_file(full_path) rescue abort "Can't read file: #{full_path_to_config_file}. Maybe you don't have access to read it or the file doesn't exist (file a bug report at #{$PROJECT_HOMEPAGE} in this case)?" end end |
#reuse_token(config) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'bin/twitter-avatar-update', line 80 def reuse_token(config) # note that config must be a populated config array read from the YAML config file begin client = TwitterOAuth::Client.new( :consumer_key => config['oauth']['consumer_key'], :consumer_secret => config['oauth']['consumer_secret'], :token => config['oauth']['access_token'], :secret => config['oauth']['access_secret'] ) rescue abort "Couldn't establish an API connect to Twitter. Maybe you need to delete your config YAML and re-do the OAuth dance, or maybe you don't have an internet connection?" end return client end |
#save_config_file(myconfig, full_path_to_config_file) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'bin/twitter-avatar-update', line 53 def save_config_file(myconfig, full_path_to_config_file) # note that you need to update the config hash OUTSIDE of this and just pass begin YAML::dump(myconfig, File.open(full_path_to_config_file, 'w')) rescue abort "Can't create/modify file that I'm trying to save: #{full_path_to_config_file}. Maybe it's already a file or exists but isn't writable by you?" end end |