Skip to content

nullscreen/social-network

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Net - a Ruby client for social networks API

Net helps you write apps that need to interact with Twitter, Instagram and Facebook.

After configuring your Twitter app, you can run commands like:

user = Net::Twitter::User.find_by screen_name: 'fullscreen'
user.screen_name #=> "Fullscreen"
user.follower_count #=> 48_200

After configuring your Instagram app, you can run commands like:

user = Net::Instagram::User.find_by username: 'fullscreen_inc'
user.username #=> "fullscreen_inc"
user.follower_count #=> 7025

After configuring your Facebook app, you can run commands like:

page = Net::Facebook::Page.find_by username: 'fullscreeninc'
page.username #=> "fullscreeninc"
page.likes #=> 30094

How to install

To install on your system, run

gem install net

To use inside a bundled Ruby project, add this line to the Gemfile:

gem 'net', '~> 0.2.1'

Since the gem follows Semantic Versioning, indicating the full version in your Gemfile (~> major.minor.patch) guarantees that your project won’t occur in any error when you bundle update and a new version of Net is released.

Available resources

Net::Twitter::User

Use Net::Twitter::User to:

  • retrieve a Twitter user by screen name
  • retrieve a list of Twitter users by screen names
  • access the number of followers of a Twitter user
user = Net::Twitter::User.find_by screen_name: 'fullscreen'
user.follower_count #=> 48_200

users = Net::Twitter::User.where screen_name: ['fullscreen', 'brohemian6']
users.map(&:follower_count).sort #=> [12, 48_200]

The methods above require a configured Twitter app (see below).

Net::Instagram::User

Use Net::Instagram::User to:

  • retrieve an Instagram user by username
  • retrieve an Instagram user by id
  • access the number of followers of an Instagram user
user = Net::Instagram::User.find_by username: 'fullscreen'
user.follower_count #=> 24198

user = Net::Instagram::User.find_by id: 270587948
user.follower_count #=> 24198

The methods above require a configured Instagram app (see below).

Net::Facebook::Page

Use Net::Facebook::Page to:

  • retrieve a Facebook page by username
  • access the number of likes of a Facebook user
page = Net::Facebook::Page.find_by username: 'fullscreeninc'
page.likes #=> 7025

Net::Facebook::User

Use Net::Facebook::User to:

  • retrieve a Facebook user by username
user = Net::Facebook::User.find_by username: '10100829454613149'
user.first_name #=> Jeremy
  • Include a Facebook access_token parameter in order to access pages information for a certain user
user = Net::Facebook::User.find_by username: '10100829454613149', access_token: 'abc123'
user.pages #=> [{"name"=>"Jeremy Video Game", "id"=>"1627249647512991"}, {"name"=>"Influencer Plus", "id"=>"629655227132365"}]

The methods above require a configured Facebook app (see below).

Configuring your Twitter app

In order to use Net you must create an app in the Twitter Application Manager.

Once the app is created, copy the API key and secret and add them to your code with the following snippet of code (replacing with your own key and secret) :

Net::Twitter.configure do |config|
  config.apps.push key: 'abcd', secret: 'efgh'
end

Configuring with environment variables

As an alternative to the approach above, you can configure your app with variables. Setting the following environment variables:

export TWITTER_API_KEY='abcd'
export TWITTER_API_SECRET='efgh'

is equivalent to configuring your app with the initializer:

Net::Twitter.configure do |config|
  config.apps.push key: 'abcd', secret: 'efgh'
end

so use the approach that you prefer. If a variable is set in both places, then Net::Twitter.configure takes precedence.

Configuring your Instagram app

In order to use Net you must create an app in the Instagram Client Manager.

Once the app is created, copy the Client ID and add it to your code with the following snippet of code (replacing with your own client id) :

Net::Instagram.configure do |config|
  config.client_id = 'abcdefg'
end

Configuring with environment variables

As an alternative to the approach above, you can configure your app with a variable. Setting the following environment variable:

export INSTAGRAM_CLIENT_ID='abcdefg'

is equivalent to configuring your app with the initializer:

Net::Instagram.configure do |config|
  config.client_id = 'abcdefg'
end

so use the approach that you prefer. If a variable is set in both places, then Net::Instagram.configure takes precedence.

Configuring your Facebook app

In order to use Net you must create an app in the Facebook Application Manager.

Once the app is created, copy the API key and secret and add them to your code with the following snippet of code (replacing with your own key and secret) :

Net::Facebook.configure do |config|
  config.client_id = 'abcdefg'
  config.client_secret = 'abcdefg'
end

Configuring with environment variables

As an alternative to the approach above, you can configure your app with variables. Setting the following environment variables:

export FACEBOOK_CLIENT_ID='abcd'
export FACEBOOK_CLIENT_SECRET='efgh'

is equivalent to configuring your app with the initializer:

Net::Facebook.configure do |config|
  config.client_id = 'abcd'
  config.client_secret = 'efgh'
end

so use the approach that you prefer. If a variable is set in both places, then Net::Facebook.configure takes precedence.

How to test

To run tests, type:

rspec

Net uses VCR so by default tests do not run HTTP requests.

If you need to run tests against the live Twitter API or Instagram API, configure your Twitter app or your Instagram app using environment variables, erase the cassettes, then run rspec.

How to release new versions

If you are a manager of this project, remember to upgrade the Net gem whenever a new feature is added or a bug gets fixed.

Make sure all the tests are passing, document the changes in CHANGELOG.md and README.md, bump the version, then run

rake release

Remember that the net gem follows Semantic Versioning. Any new release that is fully backward-compatible should bump the patch version (0.1.x). Any new version that breaks compatibility should bump the minor version (0.x.0)