picture_from 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe PictureFrom::Apis::FacebookApi do
4
+
5
+ describe '#image_url_by_username' do
6
+ subject { described_class.new }
7
+
8
+ describe 'when username is blank' do
9
+ let(:url) { subject.image_url_by_username('') }
10
+
11
+ it 'returns nil' do
12
+ fake('http://graph.facebook.com//picture',
13
+ status: 400,
14
+ message: 'Bad Request')
15
+
16
+ expect(url).to be_nil
17
+ end
18
+ end
19
+
20
+ describe 'when username does not exist' do
21
+ let(:url) { subject.image_url_by_username('g_carreiro') }
22
+
23
+ it 'returns nil' do
24
+ fake('http://graph.facebook.com/g_carreiro/picture',
25
+ status: 404,
26
+ message: 'Not Found')
27
+
28
+ expect(url).to be_nil
29
+ end
30
+ end
31
+
32
+ describe 'when username is valid' do
33
+ let(:url) { subject.image_url_by_username('karreiro') }
34
+
35
+ it 'returns a valid url' do
36
+ fake('http://graph.facebook.com/karreiro/picture',
37
+ status: 302,
38
+ message: 'Found',
39
+ location: 'https://fbcdn-profile-a.akamaihd.net/image.png')
40
+
41
+ expect(url).to eq('http://graph.facebook.com/karreiro/picture')
42
+ end
43
+ end
44
+ end
45
+
46
+ end
@@ -2,16 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe PictureFrom::Crawlers::FacebookCrawler do
4
4
 
5
- describe '#query' do
5
+ describe '#image_url_by_user_info' do
6
6
  subject { described_class.new }
7
7
 
8
8
  describe 'when some profiles are found' do
9
9
  it 'returns an image url' do
10
- search_url = 'https://www.facebook.com/search.php?q=karreiro@gmail.com'
11
- search_response = 'facebook_crawler.response'
12
- fake(search_url, search_response)
10
+ fake('https://www.facebook.com/search.php?q=karreiro@gmail.com',
11
+ file: 'facebook_crawler.response')
13
12
 
14
- username = subject.query('karreiro@gmail.com')
13
+ username = subject.image_url_by_user_info('karreiro@gmail.com')
15
14
 
16
15
  expect(username).to eq('karreiro')
17
16
  end
@@ -19,51 +18,58 @@ describe PictureFrom::Crawlers::FacebookCrawler do
19
18
 
20
19
  describe 'when any profile is found' do
21
20
  it 'does not return an image url' do
22
- search_url = 'https://www.facebook.com/search.php?q=karreiro@gmail.com'
23
- search_response = 'facebook_crawler_empty.response'
24
- fake(search_url, search_response)
21
+ fake('https://www.facebook.com/search.php?q=guilherme@gmail.com',
22
+ file: 'facebook_crawler_empty.response')
25
23
 
26
- username = subject.query('karreiro@gmail.com')
24
+ username = subject.image_url_by_user_info('guilherme@gmail.com')
27
25
 
28
26
  expect(username).to be_nil
29
27
  end
30
28
  end
31
-
32
29
  end
33
30
 
34
- describe '#username_from_url' do
35
- subject { described_class.new }
31
+ describe PictureFrom::Crawlers::FacebookCrawler::SearchPage do
32
+
33
+ describe '#username_from_url' do
36
34
 
37
- describe 'when receives valid URLs' do
38
- let(:urls) do
39
- [
40
- 'https://www.facebook.com/karreiro',
41
- 'http://www.facebook.com/karreiro',
42
- 'http://facebook.com/karreiro/',
43
- 'http://facebook.com/karreiro'
44
- ]
35
+ subject do
36
+ fake('https://www.facebook.com/search.php?q=')
37
+ described_class.new('')
45
38
  end
46
39
 
47
- it 'returns a valid username' do
48
- urls.each do |url|
49
- expect(subject.send(:username_from_url, url)).to eq('karreiro')
40
+ describe 'when receives valid URLs' do
41
+ let(:urls) do
42
+ [
43
+ 'https://www.facebook.com/karreiro',
44
+ 'http://www.facebook.com/karreiro',
45
+ 'http://facebook.com/karreiro/',
46
+ 'http://facebook.com/karreiro'
47
+ ]
50
48
  end
51
- end
52
- end
53
49
 
54
- describe 'when receives invalid URLs' do
55
- let(:urls) do
56
- [
57
- 'https://orkut.com/karreiro',
58
- 'http://fb.com/karreiro'
59
- ]
50
+ it 'returns a valid username' do
51
+ urls.each do |url|
52
+ expect(subject.send(:username_from_url, url)).to eq('karreiro')
53
+ end
54
+ end
60
55
  end
61
56
 
62
- it 'does not return any username' do
63
- urls.each do |url|
64
- expect(subject.send(:username_from_url, url)).to be_nil
57
+ describe 'when receives invalid URLs' do
58
+ let(:urls) do
59
+ [
60
+ 'https://orkut.com/karreiro',
61
+ 'http://fb.com/karreiro',
62
+ 'https://www.facebook.com/pages/Mark-Zuckerberg/112845672063384'
63
+ ]
64
+ end
65
+
66
+ it 'does not return any username' do
67
+ urls.each do |url|
68
+ expect(subject.send(:username_from_url, url)).to be_nil
69
+ end
65
70
  end
66
71
  end
72
+
67
73
  end
68
74
 
69
75
  end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe PictureFrom::Crawlers::TwitterCrawler do
4
+
5
+ describe '#image_url_by_username' do
6
+ subject { described_class.new }
7
+
8
+ describe 'when some profiles are found' do
9
+ it 'returns an image url' do
10
+ fake('https://twitter.com/g_carreiro', file: 'twitter_crawler.response')
11
+
12
+ image_url = subject.image_url_by_username('g_carreiro')
13
+
14
+ expect(image_url).to eq('https://pbs.twimg.com/profile_images/524721838002143233/nNdh6ftm_400x400.jpeg')
15
+ end
16
+ end
17
+
18
+ describe 'when any profile is found' do
19
+ it 'does not return an image url' do
20
+ fake('https://twitter.com/g_carreiro_fake',
21
+ status: 404,
22
+ message: 'Not Found')
23
+
24
+ image_url = subject.image_url_by_username('g_carreiro_fake')
25
+
26
+ expect(image_url).to be_nil
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -11,17 +11,17 @@ describe PictureFrom::FacebookPicture do
11
11
  end
12
12
  end
13
13
 
14
- describe '#picture_from_user_info' do
15
- subject { described_class.new }
14
+ describe 'online tests' do
16
15
 
17
- it 'returns the image url' do
18
- search_url = 'https://www.facebook.com/search.php?q=karreiro@gmail.com'
19
- search_response = 'facebook_crawler.response'
20
- fake(search_url, search_response)
16
+ describe '#picture_from_user_info' do
17
+ subject { described_class.new }
21
18
 
22
- link = subject.picture_from_user_info('karreiro@gmail.com')
23
- expect(link).to eq('http://graph.facebook.com/karreiro/picture')
19
+ it 'returns the image url' do
20
+ link = subject.picture_from_user_info('Mark Zuckerberg')
21
+ expect(link).to eq('http://graph.facebook.com/zuck/picture')
22
+ end
24
23
  end
24
+
25
25
  end
26
26
 
27
27
  end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe PictureFrom::Keyword do
4
+
5
+ describe '#email?' do
6
+
7
+ describe 'when a valid email is passed' do
8
+ subject { described_class.new('karreiro@gmail.com') }
9
+
10
+ it 'returns true' do
11
+ expect(subject.type).to eq(:email)
12
+ end
13
+ end
14
+
15
+ describe 'when a twitter username is passed' do
16
+ subject { described_class.new('@g_carreiro') }
17
+
18
+ it 'returns true' do
19
+ expect(subject.type).to eq(:username)
20
+ end
21
+ end
22
+
23
+ describe 'when a facebook username is passed' do
24
+ subject { described_class.new('karreiro') }
25
+
26
+ it 'returns true' do
27
+ expect(subject.type).to eq(:username)
28
+ end
29
+ end
30
+
31
+ describe 'when an user info is passed' do
32
+ subject { described_class.new('Guilherme Carreiro') }
33
+
34
+ it 'returns true' do
35
+ expect(subject.type).to eq(:user_info)
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe PictureFrom::SearchEngine do
4
+
5
+ describe '#picture_from_username' do
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe PictureFrom::TwitterPicture do
4
+
5
+ describe 'online tests' do
6
+
7
+ describe '#picture_from_username' do
8
+ subject { described_class.new }
9
+
10
+ it 'returns the image url' do
11
+ link = subject.picture_from_username('unclebobmartin')
12
+ expect(link).to eq('https://pbs.twimg.com/profile_images/1102364992/clean_code_72_color_400x400.png')
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe PictureFrom do
4
+ subject { PictureFrom }
5
+
6
+ describe 'facebook engine' do
7
+ it 'returns the image url' do
8
+ fake('http://graph.facebook.com/karreiro/picture',
9
+ status: 302,
10
+ message: 'Found',
11
+ location: 'https://fbcdn-profile-a.akamaihd.net/image.png')
12
+ link = subject.url('karreiro')
13
+ expect(link).to eq('http://graph.facebook.com/karreiro/picture')
14
+ end
15
+ end
16
+
17
+ describe 'twitter engine' do
18
+ it 'returns the image url' do
19
+ fake('http://graph.facebook.com/g_carreiro/picture', status: 404)
20
+ fake('https://twitter.com/g_carreiro', file: 'twitter_crawler.response')
21
+ link = subject.url('g_carreiro')
22
+ expect(link).to eq('https://pbs.twimg.com/profile_images/524721838002143233/nNdh6ftm_400x400.jpeg')
23
+ end
24
+ end
25
+
26
+ describe 'gravatar engine' do
27
+ it 'returns the image url' do
28
+ link = subject.url('karreiro@gmail.com')
29
+ expect(link).to eq('http://www.gravatar.com/avatar/16be5820a296f5bce151f0c1b5e16fc8')
30
+ end
31
+ end
32
+ end
@@ -1,14 +1,21 @@
1
1
  module FakeWebHelper
2
- def fake(uri, file, verb = :get)
3
- FakeWeb.register_uri(verb, uri, response: response(file))
2
+ def fake(uri, options = {})
3
+ verb = options[:verb] || :get
4
+ FakeWeb.register_uri(verb, uri, response: response(options))
4
5
  end
5
6
 
6
- def header(html)
7
- "HTTP/1.1 200 OK\n\n#{html}"
7
+ def response(options)
8
+ header(options) + body(options)
8
9
  end
9
10
 
10
- def response(file)
11
- html = File.read("spec/fixtures/#{file}")
12
- header(html)
11
+ def header(options)
12
+ status = options[:status] || 200
13
+ message = options[:message] || 'OK'
14
+ location = options[:location] || ''
15
+ "HTTP/1.1 #{status} #{message}\nLocation: #{location}\n\n"
16
+ end
17
+
18
+ def body(options)
19
+ options[:file] ? File.read("spec/fixtures/#{options[:file]}") : ''
13
20
  end
14
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picture_from
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Carreiro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-25 00:00:00.000000000 Z
11
+ date: 2014-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -139,16 +139,29 @@ files:
139
139
  - README.md
140
140
  - Rakefile
141
141
  - lib/picture_from.rb
142
+ - lib/picture_from/apis/facebook_api.rb
143
+ - lib/picture_from/crawlers/base_crawler.rb
142
144
  - lib/picture_from/crawlers/facebook_crawler.rb
145
+ - lib/picture_from/crawlers/twitter_crawler.rb
143
146
  - lib/picture_from/facebook_picture.rb
144
147
  - lib/picture_from/gravatar_picture.rb
148
+ - lib/picture_from/keyword.rb
149
+ - lib/picture_from/search_engine.rb
150
+ - lib/picture_from/twitter_picture.rb
145
151
  - lib/picture_from/version.rb
146
152
  - picture_from.gemspec
147
153
  - spec/fixtures/facebook_crawler.response
148
154
  - spec/fixtures/facebook_crawler_empty.response
155
+ - spec/fixtures/twitter_crawler.response
156
+ - spec/lib/picture_from/apis/facebook_api_spec.rb
149
157
  - spec/lib/picture_from/crawlers/facebook_crawler_spec.rb
158
+ - spec/lib/picture_from/crawlers/twitter_crawler_spec.rb
150
159
  - spec/lib/picture_from/facebook_picture_spec.rb
151
160
  - spec/lib/picture_from/gravatar_picture_spec.rb
161
+ - spec/lib/picture_from/keyword_spec.rb
162
+ - spec/lib/picture_from/search_engine_spec.rb
163
+ - spec/lib/picture_from/twitter_picture_spec.rb
164
+ - spec/lib/picture_from_spec.rb
152
165
  - spec/spec_helper.rb
153
166
  - spec/support/coverage.rb
154
167
  - spec/support/fake_web_helper.rb
@@ -179,9 +192,16 @@ summary: PictureFrom is the most efficient library for getting profile pictures.
179
192
  test_files:
180
193
  - spec/fixtures/facebook_crawler.response
181
194
  - spec/fixtures/facebook_crawler_empty.response
195
+ - spec/fixtures/twitter_crawler.response
196
+ - spec/lib/picture_from/apis/facebook_api_spec.rb
182
197
  - spec/lib/picture_from/crawlers/facebook_crawler_spec.rb
198
+ - spec/lib/picture_from/crawlers/twitter_crawler_spec.rb
183
199
  - spec/lib/picture_from/facebook_picture_spec.rb
184
200
  - spec/lib/picture_from/gravatar_picture_spec.rb
201
+ - spec/lib/picture_from/keyword_spec.rb
202
+ - spec/lib/picture_from/search_engine_spec.rb
203
+ - spec/lib/picture_from/twitter_picture_spec.rb
204
+ - spec/lib/picture_from_spec.rb
185
205
  - spec/spec_helper.rb
186
206
  - spec/support/coverage.rb
187
207
  - spec/support/fake_web_helper.rb