middleman-flickr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/.rubocop.yml +11 -0
- data/Gemfile +19 -0
- data/README.md +148 -0
- data/Rakefile +14 -0
- data/features/support/env.rb +4 -0
- data/lib/middleman-flickr/extension.rb +43 -0
- data/lib/middleman-flickr/scraper.rb +67 -0
- data/lib/middleman-flickr.rb +6 -0
- data/middleman-flickr.gemspec +22 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: adfa3c7f93f4f65e6a8c59d87c5c3c206002c074
|
4
|
+
data.tar.gz: 7c9e4c6ae1da21d6d7db106162183ae60cf5dea3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2f3e30cd443ec311f55fb94763944ff3bb3d3c87e68dd6d6d729f5d0558f5e0dfcccdcbafa1c527302e4a6b03b8b82fb678f2c80c09b645a797fe1a8372afdd4
|
7
|
+
data.tar.gz: 25494e70d11cef25c8623d7a50355b21e2fdfc5018d96312d0c97c55f6ac2b5eabe32b303de7206e3ce62c4ac74c404a528238ca9abb2a225697bdbd7b8c7466
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# If you do not have OpenSSL installed, update
|
2
|
+
# the following line to use "http://" instead
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in middleman-flickr.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
group :development do
|
9
|
+
gem 'rake'
|
10
|
+
gem 'rdoc'
|
11
|
+
gem 'yard'
|
12
|
+
end
|
13
|
+
|
14
|
+
group :test do
|
15
|
+
gem 'aruba'
|
16
|
+
gem 'capybara'
|
17
|
+
gem 'cucumber'
|
18
|
+
gem 'rspec'
|
19
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
## Middleman Flickr
|
2
|
+
|
3
|
+
`middleman-flickr` is an extension for the [Middleman] static site
|
4
|
+
generator that adds some helper functions to allow embedding images from
|
5
|
+
Flickr into your pages.
|
6
|
+
|
7
|
+
**NOTE: Fetches a maximum of 500 photos in a set.**
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
If you're just getting started, install the `middleman` gem and generate
|
12
|
+
a new project:
|
13
|
+
|
14
|
+
```
|
15
|
+
gem install middleman
|
16
|
+
middleman init MY_PROJECT
|
17
|
+
```
|
18
|
+
|
19
|
+
If you already have a Middleman project: Add `gem "middleman-flickr"`
|
20
|
+
to your `Gemfile` and run `bundle install`.
|
21
|
+
|
22
|
+
#### Environment Variables
|
23
|
+
|
24
|
+
For this extension to work, it expects 4 environment variables setup:
|
25
|
+
|
26
|
+
- `FLICKR_KEY`: Flickr API key
|
27
|
+
- `FLICKR_SECRET`: Flickr API secret
|
28
|
+
- `FLICKR_TOKEN`: Flickr OAuth token for the user
|
29
|
+
- `FLICKR_TOKEN_SECREET`: Flickr OAuth token secret for the user
|
30
|
+
|
31
|
+
An error **WILL** be raised if authentication with Flickr fails.
|
32
|
+
|
33
|
+
## Helper Methods
|
34
|
+
|
35
|
+
All helper methods added by this extension accepts configuration options
|
36
|
+
for this extension, as well as any additional options for the Flickr
|
37
|
+
API, if required, e.g.:
|
38
|
+
|
39
|
+
```
|
40
|
+
display_flickr_photoset: 'ID1', show_on_empty: false, per_page: 20
|
41
|
+
```
|
42
|
+
|
43
|
+
Here, `show_on_empty` is a config option for this extension, while
|
44
|
+
`per_page` is an option for the Flickr API call being made. In this
|
45
|
+
example, a maximum of 20 images will be returned, and HTML will not be
|
46
|
+
added if no photos are returned.
|
47
|
+
|
48
|
+
This extension adds the following helper methods for you:
|
49
|
+
|
50
|
+
#### `get_flickr_images`
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
get_flickr_images data: { photo: ['PID1', 'PID2'], photoset: ['PSID3'] }, options
|
54
|
+
```
|
55
|
+
|
56
|
+
Get a list of image URLs from Flickr for the given Photos and Photosets.
|
57
|
+
This helper method can be used in your layouts etc. and can be styled
|
58
|
+
anyway you like.
|
59
|
+
|
60
|
+
#### `display_flickr`
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
display_flickr data: { photo: ['PID1', 'PID2'], photoset: ['PSID3'] }, options
|
64
|
+
```
|
65
|
+
|
66
|
+
Add HTML markup for image URLs from Flickr for the given Photos and
|
67
|
+
Photosets. Thumbnails will be used for the image, while a `lightbox`
|
68
|
+
link is added for the large size image. Each image is wrapped in
|
69
|
+
a `div.grid-item` CSS for ease of styling, which are further wrapped in
|
70
|
+
a `div.grid` container. (Think: `masonry`)
|
71
|
+
|
72
|
+
#### `display_flickr_photo`
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
display_flickr_photo 'PID1', 'PID2', options
|
76
|
+
```
|
77
|
+
|
78
|
+
Add HTML for the given photos.
|
79
|
+
|
80
|
+
#### `display_flickr_photoset`
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
display_flickr_photoset 'PsID1', 'PsID2', options
|
84
|
+
```
|
85
|
+
|
86
|
+
Add HTML for the given photosets. A maximum of 500 photos per photoset
|
87
|
+
can be returned, at the moment.
|
88
|
+
|
89
|
+
## Configuration
|
90
|
+
|
91
|
+
```
|
92
|
+
activate :flickr do |conf|
|
93
|
+
conf.html_class = 'flickr-images'
|
94
|
+
conf.show_on_empty = true
|
95
|
+
conf.suppress_errors = -> (err) { puts err.message }
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
### Options
|
100
|
+
|
101
|
+
`html_class` option defines HTML class for `div` element that wraps the
|
102
|
+
HTML generated by this extension. This may be useful for styling your
|
103
|
+
gallery.
|
104
|
+
|
105
|
+
`show_on_empty` option can be set to `false` to not output any HTML when
|
106
|
+
no images could be found. You can, also, set this option to a `Proc`
|
107
|
+
that generates the HTML in this case.
|
108
|
+
|
109
|
+
`suppress_errors` option (default: `false`) can be used to suppress
|
110
|
+
errors generated when querying Flickr API, e.g. when a Photo/set ID
|
111
|
+
could not be found on Flickr. You can set this option to a `Proc` that
|
112
|
+
consumes the `error` generated to do something more meaningful. By
|
113
|
+
default, errors are raised/bubbled up.
|
114
|
+
|
115
|
+
|
116
|
+
## Community
|
117
|
+
|
118
|
+
The official community forum is available at: http://forum.middlemanapp.com
|
119
|
+
|
120
|
+
## Bug Reports
|
121
|
+
|
122
|
+
Github Issues are used for managing bug reports and feature requests. If
|
123
|
+
you run into issues, please search the issues and submit new problems:
|
124
|
+
https://github.com/middleman/middleman-blog/issues
|
125
|
+
|
126
|
+
The best way to get quick responses to your issues and swift fixes to
|
127
|
+
your bugs is to submit detailed bug reports, include test cases and
|
128
|
+
respond to developer questions in a timely manner. Even better, if you
|
129
|
+
know Ruby, you can submit [Pull
|
130
|
+
Requests](https://help.github.com/articles/using-pull-requests)
|
131
|
+
containing Cucumber Features which describe how your feature should work
|
132
|
+
or exploit the bug you are submitting.
|
133
|
+
|
134
|
+
## Tests / Build Status?
|
135
|
+
|
136
|
+
**Coming up soon.**
|
137
|
+
|
138
|
+
## Donate
|
139
|
+
|
140
|
+
[Click here to lend your support to Middleman](https://spacebox.io/s/4dXbHBorC3)
|
141
|
+
|
142
|
+
## License
|
143
|
+
|
144
|
+
Copyright (c) 2018 Nikhil Gupta. MIT Licensed, see [LICENSE] for details.
|
145
|
+
|
146
|
+
[middleman]: http://middlemanapp.com
|
147
|
+
[travis]: http://travis-ci.org/statonjr/middleman-sitemap
|
148
|
+
[LICENSE]: https://github.com/nikhgupta/middleman-flickr/blob/master/LICENSE
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'cucumber/rake/task'
|
5
|
+
|
6
|
+
Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t|
|
7
|
+
t.cucumber_opts = '--color --tags ~@wip --strict'
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rake/clean'
|
11
|
+
|
12
|
+
task test: ['cucumber']
|
13
|
+
|
14
|
+
task default: :test
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'middleman-core'
|
2
|
+
|
3
|
+
# Middleman Flickr Extension namespace
|
4
|
+
module Middleman
|
5
|
+
class Flickr < Extension
|
6
|
+
option :html_class, 'flickr-images', 'Class for div container for response.'
|
7
|
+
option :show_on_empty, true, 'Whether to embed markup when no photos are
|
8
|
+
returned? You can set this option to a Proc that renders HTML, instead.'
|
9
|
+
option :suppress_errors, false, 'Do not raise errors on missing photos! Set
|
10
|
+
this to a Proc if you like.'
|
11
|
+
|
12
|
+
expose_to_template :display_flickr, :get_flickr_images,
|
13
|
+
:display_flickr_photo, :display_flickr_photoset
|
14
|
+
|
15
|
+
def initialize(app, options_hash = {}, &block)
|
16
|
+
super
|
17
|
+
require 'flickraw'
|
18
|
+
# require 'middleman-flickr/helpers'
|
19
|
+
# self.class.defined_helpers = [Middleman::Flickr::Helpers]
|
20
|
+
require 'middleman-flickr/scraper'
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_flickr_images(opts = {})
|
24
|
+
data = options.to_h.merge(opts)
|
25
|
+
Middleman::Flickr::Scraper.new(data).scrape
|
26
|
+
end
|
27
|
+
|
28
|
+
def display_flickr(opts = {})
|
29
|
+
data = options.to_h.merge(opts)
|
30
|
+
Middleman::Flickr::Scraper.new(data).to_html
|
31
|
+
end
|
32
|
+
|
33
|
+
def display_flickr_photo(*args)
|
34
|
+
opts = args.last.is_a?(Hash) ? args.pop : {}
|
35
|
+
display_flickr opts.merge(data: { photo: args })
|
36
|
+
end
|
37
|
+
|
38
|
+
def display_flickr_photoset(*args)
|
39
|
+
opts = args.last.is_a?(Hash) ? args.pop : {}
|
40
|
+
display_flickr opts.merge(data: { photoset: args })
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Middleman
|
2
|
+
class Flickr
|
3
|
+
class Scraper
|
4
|
+
attr_reader :options
|
5
|
+
|
6
|
+
def initialize(data = {})
|
7
|
+
@options = data
|
8
|
+
|
9
|
+
FlickRaw.api_key = ENV['FLICKR_KEY']
|
10
|
+
FlickRaw.shared_secret = ENV['FLICKR_SECRET']
|
11
|
+
@flickr = FlickRaw::Flickr.new
|
12
|
+
@flickr.access_token = ENV['FLICKR_TOKEN']
|
13
|
+
@flickr.access_secret = ENV['FLICKR_TOKEN_SECRET']
|
14
|
+
|
15
|
+
@flickr.test.login # will raise error on auth failure!
|
16
|
+
end
|
17
|
+
|
18
|
+
def scrape
|
19
|
+
data = options.delete(:data)
|
20
|
+
data.map do |kind, ids|
|
21
|
+
ids.map { |id| request kind, id, options }.compact
|
22
|
+
end.compact.flatten(1).uniq
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_html
|
26
|
+
html = scrape.map do |img|
|
27
|
+
<<-HTML
|
28
|
+
<div class='grid-item'>
|
29
|
+
<a href='#{img[:url]}' rel='lightbox'>
|
30
|
+
<img src='#{img[:thumb]}' class='lazyload flickr'/>
|
31
|
+
</a>
|
32
|
+
</div>"
|
33
|
+
HTML
|
34
|
+
end
|
35
|
+
html = html.any? ? "<div class='grid'>#{html.join}</div>" : nil
|
36
|
+
|
37
|
+
return "<div class='#{options[:html_class]}'>#{html}</div>" if html
|
38
|
+
return if !html && !(blk = options[:show_on_empty])
|
39
|
+
|
40
|
+
return blk.call(err) if blk.respond_to?(:call)
|
41
|
+
'<div class="alert alert-warning">Images not available.</div>'
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_photoset(id, opts = {})
|
45
|
+
options = { photoset_id: id, extras: 'url_c,url_n', per_page: 500 }
|
46
|
+
options = options.merge(opts)
|
47
|
+
response = @flickr.photosets.getPhotos options
|
48
|
+
response.photo.map { |p| { thumb: p['url_n'], url: p['url_c'] } }
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_photo(id, opts = {})
|
52
|
+
options = { photo_id: id, extras: 'url_c,url_n' }
|
53
|
+
options = options.merge(opts)
|
54
|
+
photo = @flickr.photos.getInfo options
|
55
|
+
{ thumb: photo['url_n'], url: photo['url_c'] }
|
56
|
+
end
|
57
|
+
|
58
|
+
def request(kind, id, opts = {})
|
59
|
+
send("get_#{kind}", id, opts) if respond_to?("get_#{kind}")
|
60
|
+
rescue FlickRaw::FailedResponse => err
|
61
|
+
on_error = options[:suppress_errors]
|
62
|
+
raise unless on_error
|
63
|
+
on_error.call(err, kind, id) if on_error.respond_to?(:call)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'middleman-flickr'
|
5
|
+
s.version = '0.0.1'
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ['Nikhil Gupta']
|
8
|
+
s.email = ['me@nikhgupta.com']
|
9
|
+
s.homepage = 'https://github.com/nikhgupta/middleman-flickr'
|
10
|
+
s.summary = 'Middleman extension to display images from Flickr'
|
11
|
+
s.description = 'Middleman extension to display images from Flickr'
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.require_paths = ['lib']
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map do |f|
|
17
|
+
File.basename(f)
|
18
|
+
end
|
19
|
+
|
20
|
+
s.add_runtime_dependency('middleman-core', ['>= 4.2.1'])
|
21
|
+
s.add_runtime_dependency('flickraw')
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: middleman-flickr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nikhil Gupta
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: middleman-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: flickraw
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Middleman extension to display images from Flickr
|
42
|
+
email:
|
43
|
+
- me@nikhgupta.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- Gemfile
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- features/support/env.rb
|
54
|
+
- lib/middleman-flickr.rb
|
55
|
+
- lib/middleman-flickr/extension.rb
|
56
|
+
- lib/middleman-flickr/scraper.rb
|
57
|
+
- middleman-flickr.gemspec
|
58
|
+
homepage: https://github.com/nikhgupta/middleman-flickr
|
59
|
+
licenses: []
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.6.10
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Middleman extension to display images from Flickr
|
81
|
+
test_files:
|
82
|
+
- features/support/env.rb
|