Tag Archives: ruby

Push news to LinkedIn Stream in your behalf

PrintBack in the past I promised a follow up article to explain how to push data to your LinkedIn stream. The time as come and here we go, so before we take off for this journey maybe you would like to create a LinkedIn fake account for your tests, instead of using your official one.

Just go the official page: https://www.linkedin.com and follow the usual steps for the account creation, once done continue with the tutorial.

In my case I don’t care too much about flooding my timeline since I get every now and then annoying spam mail and maybe that could be the right time to taste my revenge.

Jokes a part the core of this tutorial is represented by the developers API: https://www.linkedin.com/secure/developer. Those API permits you to interface your program to LinkedIn account and perform several actions for example, read/write your profile, share news on the social stream, share on your groups and basically communicate and access Job search.

Okay, more or less simple, let’s move ahead and click on the link, you will land in an almost empty page:

image

Just click on add new application and fill the form. If everything went well it will be prompted the following page:

image

Hurrah! Well done, that was the toughest part! Please take note (a screenshot) of those information since you will need them later during the hacking session.

Now the funniest part comes, in command line or in you preferred ruby editor create the ruby file, in my case I call it linkedin.rb. To consume the API we will use a gem called (guess what?) linkedin, so copy:

gem "linkedin"

To your gemfile (in the same directory as the linkedin.rb) and run bundle install in the command line. In case you do not have any gemfile just run gem install linkedin and it will install the gem for you.

Back to our code, first of all require the linkedin and json gems:

#!/usr/bin/env ruby
require 'linkedin' # gem install linkedin
require 'json'

Then setup a configuration hash map containing your keys and your tokens:

config = {
    your_consumer_key: '77ntor37ib7qbp',      #api key
    your_consumer_secret: 'HLFdYIBeA9TTglz3',  #secret key
    oauth_user_token: 'eeabc242-ff1c-474a-a6f2-eb519c967d09', #OAuth Token
    oauth_user_secret: '51ba5c4f-92ef-4e0f-bec0-ac0b1e2937c1' #OAuth Secret:
}

Instantiate and authorize the client:

client = LinkedIn::Client.new(
    config[:your_consumer_key],
    config[:your_consumer_secret]
)
 
client.authorize_from_access(
    config[:oauth_user_token],
    config[:oauth_user_secret]
)

And now the final step, define the data that you would like to share on your stream:

data={
    "comment"=>"Check out my article about LinkedIn Share API!",
    "content"=>{"title"=>"LinkedIn Developers API consumed via Ruby",
                "description"=>"Programmatically leverage the Share API to maximize engagement on user-generated content on LinkedIn",
                "submitted-url"=>"https://giovanni.degiorgi.me/2014/12/01/push-news-to-linkedin-stream-in-your-behalf/",
                "submitted-image-url"=>"https://giovanni.degiorgi.me/wp-content/uploads/2012/02/800x140xcropped-me.jpg.pagespeed.ic.QDRtxDC7uW.jpg"},
    "visibility"=> {"code"=> "anyone"}
}

Final step, use the client to share the data:

client.add_share(data)

Run the code and you will be able to find the news in you social stream:

image

As easy as drinking a glass of water, I hope you enjoyed the article, for any question or suggestions feel free to drop my a couple of lines.

I’ve just updated my professional profile on LinkedIn. Connect with me and view my profile.

LinkedIn Profile

Tagged , , , ,

Ruby Koala Gem to Interact with Facebook

Today I started playing around with the Koala gem:

https://github.com/arsduo/koala

Koala is a Facebook library for Ruby, supporting the Graph API (including the batch requests and photo uploads), the REST API, realtime updates, test users, and OAuth validation.

To install the gem just:

gem install koala
Or put it in the Gemfile and use the bundle command ‘bundle install’:
gem "koala", "~> 1.8.0rc1"
Before we can connect to Facebook, we need to create an access token for the authentication, go on the following link:
https://developers.facebook.com/tools/explorer
And create your key with user_groups and user_photos privileges.
image
Here the code to post to specific groups, substitute the key with the one you got in the previous step:
#!/usr/bin/env ruby
require 'koala'
 
oauth_access_token = 'xxxxxxxxxxxxxxxxxx'
group_filtering_words = ['gangofruby', 'gangofrubyII']
image_path = 'ruby.jpg' #change to your image path
message = 'Ruby is the most beautiful gem.' # your message
 
graph = Koala::Facebook::API.new(oauth_access_token)
 
# getting groups of interest
groups = graph.get_connections("me", "groups").select do |group|
  group_filtering_words.any? do |word|
    group["name"].downcase.include? word
  end
end
 
 
groups.each_with_index { |group,i|
  puts "[#{i+1}/#{groups.size}] Posting to group #{group["name"]}."
  graph.put_picture( image_path, {message: message}, group['id'])
}
If it is returning an SSL error like:
/lib/ruby/2.0.0/net/http.rb:918:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 
read server certificate B: certificate verify failed (Faraday::SSLError)
Then you need to download the cacert.pem file from http://curl.haxx.se/ca/cacert.pem. 
Save the file to C:\RailsInstaller\cacert.pem and add it to your system as system variable:
SSL_CERT_FILE=C:\RailsInstaller\cacert.pem 



		                		
Tagged , , ,

Setup Rails development environment on Windows 7 machine

Today I decided to go back to one of my previous interests and start to build a Rails application. I already set up my machine in the past, but since then I formatted it at least three times and I scare nothing left from this setup.

So I decided to have a look at the official guide online, you can find good information on http://rubyonrails.org/, a very useful portal with most of the information you need.

If you click on the “get started” icon you will forwarded to the download page which contains the steps to reach a fully working development environment.

First step:

rubyInstall Ruby (Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. from Wikipedia.org) which is the core of Rails and makes everything possible. I installed the version Ruby 1.9.3 using the windows installer rubyinstaller-1.9.3-p0.exe.

Second step:

rubygemsThe official guide says that we have to install RubyGem, the standard Ruby package manager, but it was already present on my system after the Ruby installation. Anyway if you want to install it by hand just download it from RubyForge: rubygems-1.8.15.zip. After the installation update the currently gems by running:

  gem update --system

and then:

 gem install rubygems-update
 update_rubygems

Third step:

Thru RubyGem you can easily install all the gems via cmdline just running:

gem install package

in that case it should be:

gem install rails

And all the dependencies and the related gems will be installed automagically. RubyGem rocks.

Now everything is in place to start building your new Rails application. I personally suggest RadRails as eclipse plugin (there is also a standalone IDE) as development IDE.

EDIT:

Do not forget to install the DevKit, otherwise you will not be able to start your application, since some gem will be missing. The latest DevKit can be found here: http://rubyinstaller.org/downloads/

Tagged , , , ,