net 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +64 -2
- data/lib/net.rb +1 -0
- data/lib/net/facebook.rb +11 -0
- data/lib/net/facebook/api/configuration.rb +14 -0
- data/lib/net/facebook/api/request.rb +60 -0
- data/lib/net/facebook/config.rb +16 -0
- data/lib/net/facebook/errors.rb +2 -0
- data/lib/net/facebook/errors/response_error.rb +14 -0
- data/lib/net/facebook/errors/unknown_user.rb +11 -0
- data/lib/net/facebook/models.rb +1 -0
- data/lib/net/facebook/models/page.rb +55 -0
- data/lib/net/version.rb +1 -1
- data/spec/net/facebook/models/pages_spec.rb +42 -0
- data/spec/support/vcr.rb +1 -0
- metadata +13 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef9c01ac982b2c9fbec6e80f15cea501d3064d64
|
4
|
+
data.tar.gz: 7be28d7fc0c74b4e93116e5d1e190c240f138f29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e24a7eeac08430f4c8ae2362ecfbffd5901b70e8b956e102f712cae4662d310ac76d3ea59796b499c6c7fc2a7c62beb0cb18f9ecabf0700afd3419576a88bc92
|
7
|
+
data.tar.gz: 6d218d97f9d5f6bdb6716f829c2532cd876d5aa5bb43bbbf7e3609b2064f05cefcd8f79c03491ae40b66f76f662f85c1112a13673e415f9e6c54b8c6ee1fb72a
|
data/CHANGELOG.md
CHANGED
@@ -26,3 +26,7 @@ If it does, then replace your calls to `followers_count` with `follower_count` (
|
|
26
26
|
## 0.2.1 - 2014-10-20
|
27
27
|
|
28
28
|
* [ENHANCEMENT] Requiring 'net' auto-loads Net::Twitter and Net::Instagram
|
29
|
+
|
30
|
+
## 0.2.2 - 2015-08-04
|
31
|
+
|
32
|
+
* [FEATURE] Add `Facebook::Page` supporting `find_by` and `find_by!`.
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Net - a Ruby client for social networks API
|
2
2
|
===========================================
|
3
3
|
|
4
|
-
Net helps you write apps that need to interact with Twitter and
|
4
|
+
Net helps you write apps that need to interact with Twitter, Instagram and Facebook.
|
5
5
|
|
6
6
|
|
7
7
|
After [configuring your Twitter app](#configuring-your-twitter-app), you can run commands like:
|
@@ -19,6 +19,14 @@ user.username #=> "fullscreen_inc"
|
|
19
19
|
user.follower_count #=> 7025
|
20
20
|
```
|
21
21
|
|
22
|
+
After [configuring your Facebook app](#configuring-your-facebook-app), you can run commands like:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
page = Net::Facebook::Page.find_by username: 'fullscreeninc'
|
26
|
+
page.username #=> "fullscreeninc"
|
27
|
+
page.likes #=> 30094
|
28
|
+
```
|
29
|
+
|
22
30
|
How to install
|
23
31
|
==============
|
24
32
|
|
@@ -28,7 +36,7 @@ To install on your system, run
|
|
28
36
|
|
29
37
|
To use inside a bundled Ruby project, add this line to the Gemfile:
|
30
38
|
|
31
|
-
gem 'net', '~> 0.
|
39
|
+
gem 'net', '~> 0.2.1'
|
32
40
|
|
33
41
|
Since the gem follows [Semantic Versioning](http://semver.org),
|
34
42
|
indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
|
@@ -73,6 +81,21 @@ user.follower_count #=> 7025
|
|
73
81
|
|
74
82
|
*The methods above require a configured Instagram app (see below).*
|
75
83
|
|
84
|
+
Net::Facebook::Page
|
85
|
+
--------------------
|
86
|
+
|
87
|
+
Use [Net::Facebook::Page]() to:
|
88
|
+
|
89
|
+
* retrieve a Facebook page by username
|
90
|
+
* access the number of likes of a Facebook user
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
page = Net::Facebook::Page.find_by username: 'fullscreeninc'
|
94
|
+
page.likes #=> 7025
|
95
|
+
```
|
96
|
+
|
97
|
+
*The methods above require a configured Facebook app (see below).*
|
98
|
+
|
76
99
|
Configuring your Twitter app
|
77
100
|
============================
|
78
101
|
|
@@ -147,6 +170,45 @@ end
|
|
147
170
|
so use the approach that you prefer.
|
148
171
|
If a variable is set in both places, then `Net::Instagram.configure` takes precedence.
|
149
172
|
|
173
|
+
Configuring your Facebook app
|
174
|
+
============================
|
175
|
+
|
176
|
+
In order to use Net you must create an app in the [Facebook Application Manager](https://developers.facebook.com/apps/).
|
177
|
+
|
178
|
+
Once the app is created, copy the API key and secret and add them to your
|
179
|
+
code with the following snippet of code (replacing with your own key and secret)
|
180
|
+
:
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
Net::Facebook.configure do |config|
|
184
|
+
config.client_id = 'abcdefg'
|
185
|
+
config.client_secret = 'abcdefg'
|
186
|
+
end
|
187
|
+
```
|
188
|
+
|
189
|
+
Configuring with environment variables
|
190
|
+
--------------------------------------
|
191
|
+
|
192
|
+
As an alternative to the approach above, you can configure your app with
|
193
|
+
variables. Setting the following environment variables:
|
194
|
+
|
195
|
+
```bash
|
196
|
+
export FACEBOOK_CLIENT_ID='abcd'
|
197
|
+
export FACEBOOK_CLIENT_SECRET='efgh'
|
198
|
+
```
|
199
|
+
|
200
|
+
is equivalent to configuring your app with the initializer:
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
Net::Facebook.configure do |config|
|
204
|
+
config.client_id = 'abcd'
|
205
|
+
config.client_secret = 'efgh'
|
206
|
+
end
|
207
|
+
```
|
208
|
+
|
209
|
+
so use the approach that you prefer.
|
210
|
+
If a variable is set in both places, then `Net::Facebook.configure` takes precedence.
|
211
|
+
|
150
212
|
How to test
|
151
213
|
===========
|
152
214
|
|
data/lib/net.rb
CHANGED
data/lib/net/facebook.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'net/facebook/errors/response_error'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
|
5
|
+
module Net
|
6
|
+
module Facebook
|
7
|
+
module Api
|
8
|
+
class Request
|
9
|
+
def initialize(attrs = {})
|
10
|
+
@host = 'graph.facebook.com'
|
11
|
+
@query = attrs[:params] if attrs[:params]
|
12
|
+
@path = attrs.fetch :path, "/v2.3/#{@query}"
|
13
|
+
@method = attrs.fetch :method, :get
|
14
|
+
end
|
15
|
+
|
16
|
+
def run
|
17
|
+
case response = run_http_request
|
18
|
+
when Net::HTTPOK
|
19
|
+
JSON.parse(response.body)
|
20
|
+
else
|
21
|
+
raise Errors::ResponseError, response
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def run_http_request
|
27
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
28
|
+
http.request http_request
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def http_request
|
33
|
+
http_class = "Net::HTTP::#{@method.capitalize}".constantize
|
34
|
+
@http_request ||= http_class.new(uri.request_uri)
|
35
|
+
end
|
36
|
+
|
37
|
+
def uri
|
38
|
+
@uri ||= URI::HTTPS.build host: @host, path: @path, query: query
|
39
|
+
end
|
40
|
+
|
41
|
+
def query
|
42
|
+
{}.tap do |query|
|
43
|
+
query.merge! access_token: "#{Net::Facebook.configuration.client_id}|#{Net::Facebook.configuration.client_secret}"
|
44
|
+
end.to_param
|
45
|
+
end
|
46
|
+
|
47
|
+
def as_curl
|
48
|
+
'curl'.tap do |curl|
|
49
|
+
curl << " -X #{http_request.method}"
|
50
|
+
http_request.each_header do |name, value|
|
51
|
+
curl << %Q{ -H "#{name}: #{value}"}
|
52
|
+
end
|
53
|
+
curl << %Q{ -d '#{http_request.body}'} if http_request.body
|
54
|
+
curl << %Q{ "#{@uri.to_s}"}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'net/facebook/models/page'
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'net/facebook/api/request'
|
2
|
+
require 'net/facebook/errors'
|
3
|
+
|
4
|
+
module Net
|
5
|
+
module Facebook
|
6
|
+
module Models
|
7
|
+
class Page
|
8
|
+
attr_reader :username, :likes
|
9
|
+
|
10
|
+
def initialize(attrs = {})
|
11
|
+
@username = attrs['username']
|
12
|
+
@likes = attrs['likes']
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
# Returns the existing Facebook page matching the provided attributes or
|
17
|
+
# nil when the page is not found.
|
18
|
+
#
|
19
|
+
# @return [Net::Facebook::Models::Page] the Facebook page.
|
20
|
+
# @ return [nil] when the page cannot be found.
|
21
|
+
# @param [Hash] params the attributes to find a page by.
|
22
|
+
# @option params [String] :username The Facebook page’s username
|
23
|
+
# (case-insensitive).
|
24
|
+
def self.find_by(params = {})
|
25
|
+
find_by! params
|
26
|
+
rescue Errors::UnknownUser
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns the existing Facebook page matching the provided attributes or
|
31
|
+
# raises an error when the page is not found.
|
32
|
+
#
|
33
|
+
# @return [Net::Facebook::Models::Page] the Facebook page.
|
34
|
+
# @param [Hash] params the attributes to find a page by.
|
35
|
+
# @option params [String] :username The Facebook page’s username
|
36
|
+
# (case-insensitive).
|
37
|
+
# @raise [Net::Errors::UnknownUser] if the page cannot be found.
|
38
|
+
def self.find_by!(params = {})
|
39
|
+
find_by_username! params[:username]
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def self.find_by_username!(username)
|
45
|
+
request = Api::Request.new params: username
|
46
|
+
new request.run
|
47
|
+
rescue Errors::ResponseError => error
|
48
|
+
case error.response
|
49
|
+
when Net::HTTPNotFound then raise Errors::UnknownUser
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/net/version.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Net::Facebook::Page do
|
4
|
+
let(:unknown_username) { 'qeqwe09qlkmhkjh' }
|
5
|
+
let(:existing_username) { 'Fullscreeninc' }
|
6
|
+
|
7
|
+
describe '.find_by' do
|
8
|
+
subject(:page) { Net::Facebook::Page.find_by username: username }
|
9
|
+
|
10
|
+
context 'given an existing (case-insensitive) username' do
|
11
|
+
let (:username) { existing_username }
|
12
|
+
|
13
|
+
it 'returns an object representing the page for that user' do
|
14
|
+
expect(page.username).to eq 'fullscreeninc'
|
15
|
+
expect(page.likes).to be_an Integer
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'given an unknown username' do
|
20
|
+
let(:username) { unknown_username }
|
21
|
+
it { expect(page).to be_nil }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.find_by!' do
|
26
|
+
subject(:page) { Net::Facebook::Page.find_by! username: username }
|
27
|
+
|
28
|
+
context 'given an existing (case-insensitive) username' do
|
29
|
+
let(:username) { existing_username }
|
30
|
+
|
31
|
+
it 'returns an object representing the page for that user' do
|
32
|
+
expect(page.username).to eq 'fullscreeninc'
|
33
|
+
expect(page.likes).to be_an Integer
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'given an unknown username' do
|
38
|
+
let(:username) { unknown_username }
|
39
|
+
it { expect{page}.to raise_error Net::Facebook::UnknownUser }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/spec/support/vcr.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Cohen Hoffing
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-08-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -139,6 +139,15 @@ files:
|
|
139
139
|
- README.md
|
140
140
|
- Rakefile
|
141
141
|
- lib/net.rb
|
142
|
+
- lib/net/facebook.rb
|
143
|
+
- lib/net/facebook/api/configuration.rb
|
144
|
+
- lib/net/facebook/api/request.rb
|
145
|
+
- lib/net/facebook/config.rb
|
146
|
+
- lib/net/facebook/errors.rb
|
147
|
+
- lib/net/facebook/errors/response_error.rb
|
148
|
+
- lib/net/facebook/errors/unknown_user.rb
|
149
|
+
- lib/net/facebook/models.rb
|
150
|
+
- lib/net/facebook/models/page.rb
|
142
151
|
- lib/net/instagram.rb
|
143
152
|
- lib/net/instagram/api/configuration.rb
|
144
153
|
- lib/net/instagram/api/request.rb
|
@@ -162,6 +171,7 @@ files:
|
|
162
171
|
- lib/net/twitter/models/user.rb
|
163
172
|
- lib/net/version.rb
|
164
173
|
- net.gemspec
|
174
|
+
- spec/net/facebook/models/pages_spec.rb
|
165
175
|
- spec/net/instagram/models/user_spec.rb
|
166
176
|
- spec/net/twitter/models/user_spec.rb
|
167
177
|
- spec/spec_helper.rb
|
@@ -207,6 +217,7 @@ signing_key:
|
|
207
217
|
specification_version: 4
|
208
218
|
summary: An API Client for social networks
|
209
219
|
test_files:
|
220
|
+
- spec/net/facebook/models/pages_spec.rb
|
210
221
|
- spec/net/instagram/models/user_spec.rb
|
211
222
|
- spec/net/twitter/models/user_spec.rb
|
212
223
|
- spec/spec_helper.rb
|