flickr_search 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +9 -0
- data/flickr_search.gemspec +30 -0
- data/lib/flickr_search.rb +2 -0
- data/lib/flickr_search/flickr_search.rb +94 -0
- data/lib/flickr_search/version.rb +3 -0
- data/spec/flickr_search/flickr_search_spec.rb +62 -0
- data/spec/spec_helper.rb +8 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ed3e48a1aa0e22b16d83caa13515c0ee3319d2a
|
4
|
+
data.tar.gz: fc494ba54f75d953f36db75d3997fb26ae2c72ff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 48ff419055295d90108569ca257bf408cd4b8ffb55de759a2662c88c022eb7286e9bc8f0c010e63a95d916e02abc44265b309ad56e051bf2a0f79b223dcab370
|
7
|
+
data.tar.gz: 47f1e280261e708386fdec9efb1b4fdc8db0308ac2cbd1bec8d6d8b40deca2616b2323bef5eb88bc869060d805a8ef0ba27efe2197c525691c584485abda4280
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
|
17
|
+
# Capybara features specs
|
18
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
19
|
+
|
20
|
+
# Turnip features and steps
|
21
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
22
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
23
|
+
end
|
24
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Raman Sinha
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# FlickrSearch
|
2
|
+
|
3
|
+
Allows you to use add public images from Flickr into your application using your own Flickr Developer API credentials.
|
4
|
+
|
5
|
+
Please note you are responsible for making sure your usage of the Flickr API adheres to their guidelines which you read when you signed-up for an API key, right? :-)
|
6
|
+
|
7
|
+
Extracted from a little example Ruby 2.1.0p0 / Rails 4.0.2 app that accompanies my talk about Mining for Gems in your Rails App.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'flickr_search', git: 'git://github.com/velobuff/flickr_search.git'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Then create an environment variable (pick your favorite method or read http://railsapps.github.io/rails-environment-variables.html if you don't have one yet) to make your FLICKR_API_KEY available to the gem.
|
20
|
+
|
21
|
+
The quick-and-dirty way to add an environment variable for testing is:
|
22
|
+
```
|
23
|
+
$export FLICKR_API_KEY='YOUR_OWN_DEVELOPER_KEY'
|
24
|
+
```
|
25
|
+
|
26
|
+
Alternatively, you can send in your api key as an optional parameter when you're new-ing up the FlickrSearch object but this is highly discouraged because you'll inadvertently end up sharing your api key when you push your code to GitHub. Plus saving your config within the app is not http://12factor.net - savvy anyway. And you do want to be savvy, don't you?
|
27
|
+
|
28
|
+
|
29
|
+
## Rails Usage
|
30
|
+
|
31
|
+
In your controller, instantiate an instance & pass it a search term.
|
32
|
+
|
33
|
+
```
|
34
|
+
@flickr = FlickrSearch::FlickrSearch.new
|
35
|
+
@flickr.search("Boulder, Colorado")
|
36
|
+
|
37
|
+
#or just
|
38
|
+
|
39
|
+
@flickr = FlickrSearch::FlickrSearch.new.search("Boulder, Colorado")
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
Then use the your instance to populate an img src in your View or something
|
44
|
+
|
45
|
+
```
|
46
|
+
<% if @flickr %>
|
47
|
+
<%= @flickr.num_results %> photos found for <%= @flickr.search_query %>. Here's one:
|
48
|
+
<br/>
|
49
|
+
<%= image_tag @flickr.random_photo_url %>
|
50
|
+
<br/>
|
51
|
+
<%= @flickr.random_photo_description %>
|
52
|
+
<% end %>
|
53
|
+
```
|
54
|
+
|
55
|
+
## TODO
|
56
|
+
|
57
|
+
* More intelligent searching (tags vs. description, AND vs. OR, etc), some image metadata attributes or just METAprogram the parameters @ http://www.flickr.com/services/api/flickr.photos.search.html
|
58
|
+
* Return the url of a local 404 image if there are any errors or timeouts calling Flickr api
|
59
|
+
* Perpetual Refactoring
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it ( http://github.com/velobuff/flickr_search/fork )
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'flickr_search/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "flickr_search"
|
8
|
+
spec.version = FlickrSearch::VERSION
|
9
|
+
spec.authors = ["Raman Sinha"]
|
10
|
+
spec.email = ["velobuff@gmail.com"]
|
11
|
+
spec.summary = %q{Simple photo search for Flickr}
|
12
|
+
spec.description = %q{Search photos on Flickr using your Flickr developer API credentials.}
|
13
|
+
spec.homepage = "http://github.com/velobuff/flickr_search/"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "faraday", "~> 0.9.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec" , "~> 2.14.1"
|
26
|
+
spec.add_development_dependency "vcr"
|
27
|
+
spec.add_development_dependency "webmock", "~> 1.15.0" #vcr complains
|
28
|
+
spec.add_development_dependency "guard-rspec"
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'Faraday'
|
2
|
+
|
3
|
+
module FlickrSearch
|
4
|
+
class FlickrSearch
|
5
|
+
|
6
|
+
def initialize(api_key = nil)
|
7
|
+
@api_key = api_key if api_key
|
8
|
+
end
|
9
|
+
|
10
|
+
def search(query)
|
11
|
+
@search_query = query
|
12
|
+
#https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=8488f3aeb049689b2f6aec5ff0859de5&text=denver&format=json&nojsoncallback=1
|
13
|
+
@response = conn.get do |req|
|
14
|
+
req.url flickr_endpoint, api_key: api_key, method: api_method, text: search_query, format: format, nojsoncallback: 1
|
15
|
+
end
|
16
|
+
|
17
|
+
self #otherwise we'd be returning @response which is a Faraday::Response and we wouldn't be able to chain as expected
|
18
|
+
end
|
19
|
+
|
20
|
+
def search_query
|
21
|
+
@search_query
|
22
|
+
end
|
23
|
+
|
24
|
+
def api_key
|
25
|
+
@api_key ||= ENV['FLICKR_API_KEY']
|
26
|
+
end
|
27
|
+
|
28
|
+
def num_results
|
29
|
+
json_response["photos"]["total"].to_i
|
30
|
+
end
|
31
|
+
|
32
|
+
def random_photo_urls(how_many)
|
33
|
+
photos = Array.new
|
34
|
+
how_many.times.each { photos << photo_url(random_photo) }
|
35
|
+
photos
|
36
|
+
end
|
37
|
+
|
38
|
+
def random_photo_url
|
39
|
+
photo_url random_photo
|
40
|
+
end
|
41
|
+
|
42
|
+
def random_photo_description
|
43
|
+
random_photo["title"]
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def random_photo
|
49
|
+
json_response["photos"]["photo"].sample
|
50
|
+
end
|
51
|
+
|
52
|
+
def photo_url(photo_json, photo_size = nil)
|
53
|
+
|
54
|
+
farm = photo_json["farm"]
|
55
|
+
server = photo_json["server"]
|
56
|
+
id = photo_json["id"]
|
57
|
+
secret = photo_json["secret"]
|
58
|
+
size = photo_size || "_b"
|
59
|
+
|
60
|
+
"http://farm#{farm}.staticflickr.com/#{server}/#{id}_#{secret}#{size}.jpg"
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
def response
|
65
|
+
@response
|
66
|
+
end
|
67
|
+
|
68
|
+
def json_response
|
69
|
+
@json_response ||= JSON.parse(response.body)
|
70
|
+
end
|
71
|
+
|
72
|
+
def api_method
|
73
|
+
@api_method ||= %q{flickr.photos.search}
|
74
|
+
end
|
75
|
+
|
76
|
+
def format
|
77
|
+
@format ||= "json"
|
78
|
+
end
|
79
|
+
|
80
|
+
def conn
|
81
|
+
@conn ||= Faraday.new(:url => flickr_endpoint) do |f|
|
82
|
+
f.request :url_encoded
|
83
|
+
f.response :logger
|
84
|
+
f.adapter Faraday.default_adapter
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def flickr_endpoint
|
89
|
+
@flickr_endpoint ||= %q{https://api.flickr.com/services/rest/}
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FlickrSearch::FlickrSearch do
|
4
|
+
|
5
|
+
let(:api_key) { ENV['FLICKR_API_KEY'] }
|
6
|
+
|
7
|
+
context "Object instantiation" do
|
8
|
+
|
9
|
+
let(:flickr_search) {FlickrSearch::FlickrSearch.new(api_key)}
|
10
|
+
|
11
|
+
it "should load properly}" do
|
12
|
+
flickr_search.should be_kind_of(FlickrSearch::FlickrSearch)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have an api key" do
|
16
|
+
flickr_search.api_key.should eql(api_key)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "api key shouldn't be nil unless the dev forgot to set the env variable" do
|
20
|
+
flickr_search.api_key.should_not eql(nil)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
context "perform search" do
|
26
|
+
|
27
|
+
let(:search_term) { "boulder" }
|
28
|
+
|
29
|
+
let(:flickr_search) {
|
30
|
+
VCR.use_cassette "boulder" do
|
31
|
+
FlickrSearch::FlickrSearch.new(api_key).search(search_term)
|
32
|
+
end
|
33
|
+
}
|
34
|
+
|
35
|
+
it "should remember the search term" do
|
36
|
+
flickr_search.search_query.should eql(search_term)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should have > 1 search results" do
|
40
|
+
flickr_search.num_results.should_not eql(0)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should have alot of search results" do
|
44
|
+
flickr_search.num_results.should be > 1000
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return a photo's URL" do
|
48
|
+
flickr_search.random_photo_url.should_not eql(nil)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return a photo's description" do
|
52
|
+
flickr_search.random_photo_description.should_not eql(nil)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should return 5 photos" do
|
56
|
+
urls=flickr_search.random_photo_urls(5)
|
57
|
+
urls.count.should be 5
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flickr_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Raman Sinha
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.14.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.14.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: vcr
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.15.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.15.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: guard-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Search photos on Flickr using your Flickr developer API credentials.
|
112
|
+
email:
|
113
|
+
- velobuff@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- Gemfile
|
121
|
+
- Guardfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- flickr_search.gemspec
|
126
|
+
- lib/flickr_search.rb
|
127
|
+
- lib/flickr_search/flickr_search.rb
|
128
|
+
- lib/flickr_search/version.rb
|
129
|
+
- spec/flickr_search/flickr_search_spec.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
homepage: http://github.com/velobuff/flickr_search/
|
132
|
+
licenses:
|
133
|
+
- MIT
|
134
|
+
metadata: {}
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project:
|
151
|
+
rubygems_version: 2.0.3
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: Simple photo search for Flickr
|
155
|
+
test_files:
|
156
|
+
- spec/flickr_search/flickr_search_spec.rb
|
157
|
+
- spec/spec_helper.rb
|