shrine-flickr 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 12ab499b1ff7996632ad117ae88b3942f34a5b18
4
+ data.tar.gz: 7e288b9df96f09379f15f25a2c16579e5c26aca5
5
+ SHA512:
6
+ metadata.gz: 21bf0cd8d224802249b45135429a5a1e8c59e6e6043527200e0f13b08ecc07c03a99f2a14439592842cf713379a51c70d92e0f56053c1ec8f7ec1e797d9a6e75
7
+ data.tar.gz: db89dd5f8298879254fdf2d1a0f3df6b7222cc22d1fd3f79c4a937157e0c6105c0ab37ef19f08835900ddd5134782e478848229d318cd0dd96e41c06c9f15c0f
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Janko Marohnić
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,131 @@
1
+ # Shrine::Flickr
2
+
3
+ Provides [Flickr] storage for [Shrine].
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem "shrine-flickr"
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ First you need to assign API key and shared secret to the Flickr client:
14
+
15
+ ```rb
16
+ require "flickr-objects"
17
+
18
+ Flickr.configure do |config|
19
+ config.api_key = "..."
20
+ config.shared_secret = "..."
21
+ end
22
+ ```
23
+
24
+ Afterwards you need to authenticate the account on which you want to upload
25
+ photos, see [`Flickr::OAuth`] on how you can do that. After you've obtained the
26
+ access token, you can assign it to the storage:
27
+
28
+ ```rb
29
+ require "shrine/storage/flickr"
30
+
31
+ Shrine::Storage::Flickr.new(access_token: ["key", "secret"])
32
+ ```
33
+
34
+ ### URL options
35
+
36
+ After the image is uploaded, available sizes and their URLs will be saved to
37
+ file's `#metadata`:
38
+
39
+ ```rb
40
+ user.avatar.metadata #=>
41
+ # {
42
+ # "flickr_sizes" => {
43
+ # "Square 75" => "https://farm6.staticflickr.com/5687/23578693711_ab9745cfd0_s.jpg",
44
+ # "Thumbnail" => "https://farm6.staticflickr.com/5687/23578693711_ab9745cfd0_t.jpg",
45
+ # "Original" => "https://farm6.staticflickr.com/5687/23578693711_0bf01bb96a_o.jpg",
46
+ # }
47
+ # }
48
+ ```
49
+
50
+ You can pass in the name of the size as a URL option:
51
+
52
+ ```rb
53
+ user.avatar.url(size: "Medium 500")
54
+ user.avatar.url(size: :medium_500)
55
+ ```
56
+
57
+ All possible sizes are: "Square 75", "Thumbnail", "Square 150", "Small 240",
58
+ "Small 320", "Medium 500", "Medium 640", "Medium 800", "Large 1024", "Large
59
+ 1600", "Large 2048" and "Original".
60
+
61
+ ### Album
62
+
63
+ You can choose to upload all your photos in an album by giving the album ID:
64
+
65
+ ```rb
66
+ Shrine::Storage::Flickr.new(album: "934891234")
67
+ ```
68
+
69
+ ### Upload options
70
+
71
+ If you want to pass in additional attributes to the [Flickr's upload API], you
72
+ can pass in the `:upload_options` option:
73
+
74
+ ```rb
75
+ Shrine::Storage::Flickr.new(upload_options: {hidden: 1})
76
+ ```
77
+
78
+ Alternatively, if you would like to do it per-file, you can override
79
+ `#extract_metadata` in your uploader, and add upload options under the "flickr"
80
+ key:
81
+
82
+ ```rb
83
+ class MyUploader < Shrine
84
+ def extract_metadata(io, context)
85
+ metadata = super
86
+ metadata["flickr"] = {
87
+ title: io.original_filename,
88
+ description: context[:record].description,
89
+ }
90
+ metadata
91
+ end
92
+ end
93
+ ```
94
+
95
+ ### Clearing storage
96
+
97
+ If you want to delete all photos from this storage, you can call `#clear!`:
98
+
99
+ ```rb
100
+ flickr = Shrine::Storage::Flickr.new(access_token: ["...", "..."])
101
+ # ...
102
+ flickr.clear!(:confirm)
103
+ ```
104
+
105
+ ## Contributing
106
+
107
+ First you need to create an `.env` file where you will store your credentials:
108
+
109
+ ```sh
110
+ # .env
111
+ FLICKR_API_KEY="..."
112
+ FLICKR_SHARED_SECRET="..."
113
+ FLICKR_ACCESS_TOKEN_KEY="..."
114
+ FLICKR_ACCESS_TOKEN_SECRET="..."
115
+ FLICKR_USER="..." # The user ID, e.g. "94384331@N07"
116
+ ```
117
+
118
+ Afterwards you can run the tests:
119
+
120
+ ```sh
121
+ $ bundle exec rake test
122
+ ```
123
+
124
+ ## License
125
+
126
+ [MIT](http://opensource.org/licenses/MIT)
127
+
128
+ [Flickr]: https://www.flickr.com/
129
+ [Shrine]: https://github.com/janko-m/shrine
130
+ [`Flickr::OAuth`]: http://www.rubydoc.info/github/janko-m/flickr-objects/master/Flickr/OAuth
131
+ [Flickr's upload API]: https://www.flickr.com/services/api/upload.api.html
@@ -0,0 +1,120 @@
1
+ require "shrine"
2
+ require "flickr-objects"
3
+ require "down"
4
+
5
+ class Shrine
6
+ module Storage
7
+ class Flickr
8
+ attr_reader :person, :flickr, :upload_options, :album
9
+
10
+ def initialize(upload_options: {}, user:, access_token:, album: nil)
11
+ @flickr = ::Flickr.new(*access_token)
12
+ @person = @flickr.people.find(user)
13
+ @upload_options = upload_options
14
+ @album = @flickr.sets.find(album) if album
15
+ end
16
+
17
+ def upload(io, id, metadata = {})
18
+ options = metadata.delete("flickr") || {}
19
+ options.update(upload_options)
20
+
21
+ photo_id = flickr.upload(io, options)
22
+ id.replace(photo_id)
23
+ album.add_photo(id) if album
24
+
25
+ metadata["flickr_sizes"] = {}
26
+ photo = photo(id).get_sizes!
27
+ photo.available_sizes.each do |size|
28
+ metadata["flickr_sizes"][size] = photo.size!(size).source_url
29
+ end
30
+ end
31
+
32
+ def download(id)
33
+ raise NotImplementedError, "#download cannot be implemented"
34
+ end
35
+
36
+ def open(id)
37
+ raise NotImplementedError, "#open cannot be implemented"
38
+ end
39
+
40
+ def exists?(id)
41
+ !!photo(id).get_info!
42
+ rescue ::Flickr::ApiError => error
43
+ raise error if error.code != 1
44
+ false
45
+ end
46
+
47
+ def delete(id)
48
+ photo(id).delete
49
+ end
50
+
51
+ def url(id, **options)
52
+ raise NotImplementedError, "#url cannot be implemented"
53
+ end
54
+
55
+ def clear!(confirm = nil)
56
+ raise Shrine::Confirm unless confirm == :confirm
57
+ if album
58
+ album.photos.each(&:delete)
59
+ else
60
+ person.photos.each(&:delete)
61
+ end
62
+ end
63
+
64
+ protected
65
+
66
+ def photo(id)
67
+ flickr.photos.find(id)
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ class Shrine
74
+ module Plugins
75
+ module Flickr
76
+ module FileMethods
77
+ def download
78
+ if storage.is_a?(Storage::Flickr)
79
+ Down.download(original_url)
80
+ else
81
+ super
82
+ end
83
+ end
84
+
85
+ def url(**options)
86
+ if storage.is_a?(Storage::Flickr)
87
+ if size = options[:size]
88
+ size = size.to_s.tr("_", " ").capitalize if size.is_a?(Symbol)
89
+ flickr_sizes.fetch(size)
90
+ else
91
+ original_url
92
+ end
93
+ else
94
+ super
95
+ end
96
+ end
97
+
98
+ private
99
+
100
+ def io
101
+ if storage.is_a?(Storage::Flickr)
102
+ @io ||= download
103
+ else
104
+ super
105
+ end
106
+ end
107
+
108
+ def original_url
109
+ flickr_sizes.fetch("Original")
110
+ end
111
+
112
+ def flickr_sizes
113
+ metadata["flickr_sizes"]
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ plugin Plugins::Flickr
120
+ end
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "shrine-flickr"
3
+ gem.version = "0.1.0"
4
+
5
+ gem.required_ruby_version = ">= 2.1"
6
+
7
+ gem.summary = "Provides Flickr storage for Shrine."
8
+ gem.homepage = "https://github.com/janko-m/shrine-flickr"
9
+ gem.authors = ["Janko Marohnić"]
10
+ gem.email = ["janko.marohnic@gmail.com"]
11
+ gem.license = "MIT"
12
+
13
+ gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-flickr.gemspec"]
14
+ gem.require_path = "lib"
15
+
16
+ gem.add_dependency "flickr-objects", ">= 0.6.1"
17
+ gem.add_dependency "down", ">= 1.0.3"
18
+
19
+ gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "shrine"
21
+ gem.add_development_dependency "minitest"
22
+ gem.add_development_dependency "dotenv"
23
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-flickr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Janko Marohnić
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: flickr-objects
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: down
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.3
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: shrine
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
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: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - janko.marohnic@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - LICENSE.txt
105
+ - README.md
106
+ - lib/shrine/storage/flickr.rb
107
+ - shrine-flickr.gemspec
108
+ homepage: https://github.com/janko-m/shrine-flickr
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '2.1'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.5
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Provides Flickr storage for Shrine.
132
+ test_files: []
133
+ has_rdoc: