Display output of ruby script requiring Mechanize on MacOS Desktop using GeekTools?

287 views Asked by At

I have written a working ruby script using mechanize for fetching my

  1. Stackoverflow reputation and badges
  2. Twitter tweets and followers

and printing the output to screen.

I run this command on the terminal first :

/Users/cyclotrojan/.rvm/rubies/ruby-1.9.2-p290/bin/ruby /Users/cyclotrojan/Desktop/Snippets/get_so_reputation_and_tw_followers.rb

The output is correctly displayed like this on the terminal:

StackOverflow :
    Reputation is : 406
    Total Badges  : 10
Twitter :
    Tweets     : 1,494
    Followers  : 137

Now I wish to display the output of my script on my desktop using GeekTools and set refresh time to 30 minutes, so it keeps running the script over and over thus updating my stats. However when I paste the same command on a geektool shell, no output comes.

Here is the script :

File get_so_reputation_and_tw_followers.rb

require 'mechanize'

def get_stackoverflow_data

  url_so = 'https://stackoverflow.com/users/898346/cyclotrojan'

  begin
    so_page = Mechanize.new.get(url_so)
    so_reputation = so_page.link_with(:href => "/users/898346/cyclotrojan?tab=reputation").to_s
    so_badges = so_page.search(".badgecount").first

    print "StackOverflow :" + "\n"
    print "    Reputation is : " + so_reputation + "\n"
    print "    Total Badges  : " + so_badges + "\n"
  rescue
    print "StackOverflow :" + "\n"
    print "\tReputation is : " + "NA" + "\n"
    print "\tTotal Badges  : " +  "NA" + "\n"
  end

end


def get_twitter_data

  url_tw = 'https://twitter.com/cyclotrojan'

  begin
    tw_page =  Mechanize.new.get(url_tw)
    tw_tweets = tw_page.link_with( :href => "/" + url_tw.split("/").last).to_s.split(" ").first
    tw_followers = tw_page.link_with( :href => "/#!/" + url_tw.split("/").last + "/followers").to_s.split(" ").first

    print "Twitter :" + "\n"
    print "\tTweets     : " + tw_tweets + "\n"
    print "\tFollowers  : " + tw_followers + "\n"
  rescue
    print "Twitter :" + "\n"
    print "\tTweets     : " + "NA" + "\n"
    print "\tFollowers  : " + "NA" + "\n"
  end

end # end function


  get_stackoverflow_data()
  get_twitter_data()

However, the output simply doesnt show up on my desktop. I know that the problem is not with detecting ruby, since I tested it out using another file test.rb

File test.rb

puts "hello"

This command will work on GeekTool shell and display hello correctly :

/Users/cyclotrojan/.rvm/rubies/ruby-1.9.2-p290/bin/ruby /Users/cyclotrojan/Desktop/Snippets/test.rb;

Output on geektool shell: hello

I have done some debugging and found that if I remove the line require 'mechanize' then the script runs correctly and due to exception the following output is displayed :

StackOverflow :
    Reputation is : NA
    Total Badges  : NA
Twitter :
    Tweets     : NA
    Followers  : NA

So the issue is with including mechanize in the script. Please help me in resolving this. Thnx !

These questions didnt help : How to run Ruby scripts in Geektool? , Ruby, Mac, Geektool question, file access rights?

1

There are 1 answers

0
pguardiario On

You would be able to tell if you didn't try to rescue the error. It looks like our old friend ssl verify error so try this:

@agent = Mechanize.new
@agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
tw_page = @agent.get url_tw

I should add that you might want to make sure you understand the consequences of OpenSSL::SSL::VERIFY_NONE

Also here is a tip:

tweets = tw_page.at('a[@data-element-term="tweet_stats"] strong').text rescue 'N/A'
followers = tw_page.at('a[@data-element-term="follower_stats"] strong').text rescue 'N/A'
puts "Twitter :" + "\n"
puts "\tTweets     : #{tweets}"
puts "\tFollowers  : #{followers}"