net 0.2.3 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8a3c1afcfb291edd890db93ce4f1191cd99882e
4
- data.tar.gz: a99341b7f15fd8548eec7a19839e8ce6ee5f5297
3
+ metadata.gz: f2cebbc60522f17c0dcbe932441712682b262dec
4
+ data.tar.gz: ed85fb15cbd70c14983bf52012d38cd763297756
5
5
  SHA512:
6
- metadata.gz: 0c10bba9639036f46e272cc250b30c7f930aff01925b3bbbca43ed38cc30314c4c753da085383a8d9bb1a3ab174a9969c776392a0412ae2f005742b8be25e522
7
- data.tar.gz: b4d18255d9a2e48f48f180e129064e2aa74a89ab490e1fe3015fe595f9577aff0bc91dced2ce5cdfc8605b51e0c0983c4346632f7cff3e02895197601fec4b43
6
+ metadata.gz: 25d08b443a193ce046fd97ae8442e24a8c0807b0e28318b44e976a948dea6ea382d68a8afd7ecd37e9d5fca1c30c552892770e3b235c024c4d8be6b7dd978be6
7
+ data.tar.gz: c2cba985e441c8c27b8a9766809b7a65da5e6ce717e6a4c912840d0a00a5eb36a0f9456d841e770c58702752305773bafc05eec62f8e89cffbeaa913f73f623e
@@ -0,0 +1 @@
1
+ 2.2.2
@@ -34,3 +34,8 @@ If it does, then replace your calls to `followers_count` with `follower_count` (
34
34
  ## 0.2.3 - 2015-08-12
35
35
 
36
36
  * [FEATURE] Add `find_by id: [Integer] and find_by! id: [Integer] to Instagram::User`.
37
+
38
+ ## 0.3.3 - 2015-09-02
39
+
40
+ * [FEATURE] - Add `Facebook::User` supporting `find_by` and `find_by!`
41
+ - Add `pages` method for `Facebook::User` to return list of Facebook Pages
data/README.md CHANGED
@@ -97,6 +97,25 @@ page = Net::Facebook::Page.find_by username: 'fullscreeninc'
97
97
  page.likes #=> 7025
98
98
  ```
99
99
 
100
+ Net::Facebook::User
101
+ --------------------
102
+
103
+ Use [Net::Facebook::User]() to:
104
+
105
+ * retrieve a Facebook user by username
106
+
107
+ ```ruby
108
+ user = Net::Facebook::User.find_by username: '10100829454613149'
109
+ user.first_name #=> Jeremy
110
+ ```
111
+
112
+ * Include a Facebook access_token parameter in order to access pages information for a certain user
113
+
114
+ ```ruby
115
+ user = Net::Facebook::User.find_by username: '10100829454613149', access_token: 'abc123'
116
+ user.pages #=> [{"name"=>"Jeremy Video Game", "id"=>"1627249647512991"}, {"name"=>"Influencer Plus", "id"=>"629655227132365"}]
117
+ ```
118
+
100
119
  *The methods above require a configured Facebook app (see below).*
101
120
 
102
121
  Configuring your Twitter app
@@ -8,12 +8,14 @@ module Net
8
8
  class Request
9
9
  def initialize(attrs = {})
10
10
  @host = 'graph.facebook.com'
11
- @query = attrs[:params] if attrs[:params]
11
+ @query = attrs[:username] if attrs[:username]
12
+ @access_token = attrs[:access_token] if attrs[:access_token]
12
13
  @path = attrs.fetch :path, "/v2.3/#{@query}"
13
14
  @method = attrs.fetch :method, :get
14
15
  end
15
16
 
16
17
  def run
18
+ #print "#{as_curl}\n"
17
19
  case response = run_http_request
18
20
  when Net::HTTPOK
19
21
  JSON.parse(response.body)
@@ -35,12 +37,16 @@ module Net
35
37
  end
36
38
 
37
39
  def uri
38
- @uri ||= URI::HTTPS.build host: @host, path: @path, query: query
40
+ @uri ||= URI::HTTPS.build host: @host, path: @path, query: access_token
39
41
  end
40
42
 
41
- def query
42
- {}.tap do |query|
43
- query.merge! access_token: "#{Net::Facebook.configuration.client_id}|#{Net::Facebook.configuration.client_secret}"
43
+ def access_token
44
+ @access_token ? { access_token: @access_token }.to_param : fb_app_key_params
45
+ end
46
+
47
+ def fb_app_key_params
48
+ {}.tap do |keys|
49
+ keys.merge! access_token: "#{Net::Facebook.configuration.client_id}|#{Net::Facebook.configuration.client_secret}"
44
50
  end.to_param
45
51
  end
46
52
 
@@ -1 +1,2 @@
1
1
  require 'net/facebook/models/page'
2
+ require 'net/facebook/models/user'
@@ -36,13 +36,7 @@ module Net
36
36
  # (case-insensitive).
37
37
  # @raise [Net::Errors::UnknownUser] if the page cannot be found.
38
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
39
+ request = Api::Request.new params
46
40
  new request.run
47
41
  rescue Errors::ResponseError => error
48
42
  case error.response
@@ -0,0 +1,66 @@
1
+ require 'net/facebook/api/request'
2
+ require 'net/facebook/errors'
3
+
4
+ module Net
5
+ module Facebook
6
+ module Models
7
+ class User
8
+ attr_reader :id, :email, :gender, :first_name, :last_name, :access_token
9
+
10
+ def initialize(attrs = {})
11
+ @id = attrs['id']
12
+ @email = attrs['email']
13
+ @gender = attrs['gender']
14
+ @first_name = attrs['first_name']
15
+ @last_name = attrs['last_name']
16
+ @access_token = attrs['access_token']
17
+ end
18
+
19
+ def pages
20
+ request = Api::Request.new access_token: @access_token, path: "/v2.3/#{@id}/accounts"
21
+ page_json = request.run
22
+ page_json['data'].map { |h| h.slice("name", "id") } if page_json['data'].any?
23
+ end
24
+
25
+ # Returns the existing Facebook user matching the provided attributes or
26
+ # nil when the user is not found.
27
+ #
28
+ # @return [Net::Facebook::Models::User] the Facebook user.
29
+ # @ return [nil] when the user cannot be found.
30
+ # @param [Hash] params the attributes to find a user by.
31
+ # @option params [String] :username The Facebook user's username
32
+ # (case-insensitive).
33
+ # @option params [String] :access_token The Facebook user's access_token
34
+ # (case-insensitive).
35
+ def self.find_by(params = {})
36
+ find_by! params
37
+ rescue Errors::UnknownUser
38
+ nil
39
+ end
40
+
41
+ # Returns the existing Facebook user matching the provided attributes or
42
+ # raises an error when the user is not found.
43
+ #
44
+ # @return [Net::Facebook::Models::User] the Facebook user.
45
+ # @param [Hash] params the attributes to find a user by.
46
+ # @option params [String] :username The Facebook user's username
47
+ # (case-insensitive).
48
+ # @option params [String] :access_token The Facebook user's access_token
49
+ # (case-insensitive).
50
+ # @raise [Net::Errors::UnknownUser] if the user cannot be found.
51
+ def self.find_by!(params = {})
52
+ request = Api::Request.new params
53
+ if params[:access_token]
54
+ new request.run.merge!({"access_token" => params[:access_token]})
55
+ else
56
+ new request.run
57
+ end
58
+ rescue Errors::ResponseError => error
59
+ case error.response
60
+ when Net::HTTPNotFound then raise Errors::UnknownUser
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -15,6 +15,7 @@ module Net
15
15
  end
16
16
 
17
17
  def run
18
+ #print "#{as_curl}\n"
18
19
  case response = run_http_request
19
20
  when Net::HTTPOK
20
21
  JSON(response.body)['data']
@@ -15,7 +15,7 @@ module Net
15
15
  end
16
16
 
17
17
  def run
18
- print "#{as_curl}\n"
18
+ #print "#{as_curl}\n"
19
19
 
20
20
  case response = run_http_request
21
21
  when Net::HTTPOK
@@ -1,3 +1,3 @@
1
1
  module Net
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Net::Facebook::User do
4
+ let(:unknown_username) { 'qeqwe09qlkmhkjh' }
5
+ let(:existing_username) { '1453280268327112' }
6
+
7
+ describe '.find_by' do
8
+ subject(:user) { Net::Facebook::User.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 user' do
14
+ expect(user.first_name).to eq 'Jeremy'
15
+ expect(user.last_name).to eq 'Dev'
16
+ expect(user.gender).to eq 'male'
17
+ end
18
+ end
19
+
20
+ context 'given an unknown username' do
21
+ let(:username) { unknown_username }
22
+ it { expect(user).to be_nil }
23
+ end
24
+ end
25
+
26
+ describe '.find_by!' do
27
+ subject(:user) { Net::Facebook::User.find_by! username: username }
28
+
29
+ context 'given an existing (case-insensitive) username' do
30
+ let(:username) { existing_username }
31
+
32
+ it 'returns an object representing the user' do
33
+ expect(user.first_name).to eq 'Jeremy'
34
+ expect(user.last_name).to eq 'Dev'
35
+ expect(user.gender).to eq 'male'
36
+ end
37
+ end
38
+
39
+ context 'given an unknown username' do
40
+ let(:username) { unknown_username }
41
+ it { expect{user}.to raise_error Net::Facebook::UnknownUser }
42
+ end
43
+ end
44
+ end
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.3
4
+ version: 0.3.3
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: 2015-08-13 00:00:00.000000000 Z
12
+ date: 2015-09-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -133,6 +133,7 @@ extra_rdoc_files: []
133
133
  files:
134
134
  - ".gitignore"
135
135
  - ".rspec"
136
+ - ".ruby-version"
136
137
  - CHANGELOG.md
137
138
  - Gemfile
138
139
  - MIT-LICENSE
@@ -148,6 +149,7 @@ files:
148
149
  - lib/net/facebook/errors/unknown_user.rb
149
150
  - lib/net/facebook/models.rb
150
151
  - lib/net/facebook/models/page.rb
152
+ - lib/net/facebook/models/user.rb
151
153
  - lib/net/instagram.rb
152
154
  - lib/net/instagram/api/configuration.rb
153
155
  - lib/net/instagram/api/request.rb
@@ -172,6 +174,7 @@ files:
172
174
  - lib/net/version.rb
173
175
  - net.gemspec
174
176
  - spec/net/facebook/models/pages_spec.rb
177
+ - spec/net/facebook/models/users_spec.rb
175
178
  - spec/net/instagram/models/user_spec.rb
176
179
  - spec/net/twitter/models/user_spec.rb
177
180
  - spec/spec_helper.rb
@@ -218,6 +221,7 @@ specification_version: 4
218
221
  summary: An API Client for social networks
219
222
  test_files:
220
223
  - spec/net/facebook/models/pages_spec.rb
224
+ - spec/net/facebook/models/users_spec.rb
221
225
  - spec/net/instagram/models/user_spec.rb
222
226
  - spec/net/twitter/models/user_spec.rb
223
227
  - spec/spec_helper.rb