shrine-flickr 0.2.0 → 1.0.0

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: 4761bed9ab500b93b20b470079e6f250306e5022
4
- data.tar.gz: 444f254394e988529d323b1f725b5f2e26338c77
3
+ metadata.gz: 93196d2074118c12e2a5c3e4ff0f4b08c6948b84
4
+ data.tar.gz: 9e1f351116746fc9868d10f6fffbbd15aefd7f29
5
5
  SHA512:
6
- metadata.gz: 6d1b39cb97e7917b7b12edd804e0d826fd927090b7a0b17aa8987fc96171198f8cff1aa814e34a34058e9b0f6667454a89ffd7c727ef6e099596af5b86a1c6e8
7
- data.tar.gz: cbaacdf09623d2f40cd2326ecf599ed0ccc579902e726ef100a117580b33d51cfe980de16a2cbda60ad47036c81928b7d574a467253a8950d38b9ed307766cbe
6
+ metadata.gz: f66a38504f2b7687dc543cc0d02fa65c76454d88ceb7c2b6e4f60e74e2878ed7b064d08cb4edabb51f668daa7fb5b08d92f38d9a01bedc92aa42430732a41f30
7
+ data.tar.gz: 3b4cec25c7aa76b78cb883a5b0071d1ded09c3d6519e1e8bc8349519779cabad4a81984a0b341c51ea22096246da31ca6c4f775d5892fc9911321c977e173384
data/README.md CHANGED
@@ -28,62 +28,23 @@ access token, you can assign it to the storage:
28
28
  ```rb
29
29
  require "shrine/storage/flickr"
30
30
 
31
- Shrine::Storage::Flickr.new(access_token: ["key", "secret"])
31
+ Shrine::Storage::Flickr.new(access_token: ["key", "secret"], user: "12345678@N01")
32
32
  ```
33
33
 
34
- ### URL, width and height
34
+ ### URL
35
35
 
36
- After the image is uploaded, URLs, widths and heighs of all sizes will be saved
37
- to "flickr_sizes" metadata key:
38
-
39
- ```rb
40
- user.avatar.metadata["flickr_sizes"] #=>
41
- # [
42
- # {
43
- # "name" => "Square 75",
44
- # "url" => "https://farm6.staticflickr.com/5706/23367159280_36d62093cf_s.jpg",
45
- # "width" => 75,
46
- # "height" => 75
47
- # },
48
- # {
49
- # "name" => "Thumbnail",
50
- # "url" => "https://farm6.staticflickr.com/5706/23367159280_36d62093cf_t.jpg",
51
- # "width" => 100,
52
- # "height" => 67
53
- # },
54
- # {
55
- # "name" => "Original",
56
- # "url" => "https://farm6.staticflickr.com/5706/23367159280_499ddf155e_o.jpg",
57
- # "width" => 100,
58
- # "height" => 67
59
- # }
60
- # ]
61
- ```
62
-
63
- You can pass the size name to `#url`, `#width` and `#height`:
36
+ To generate source URLs, simply pass the name of the size to `#url`:
64
37
 
65
38
  ```rb
66
39
  user.avatar.url(size: "Medium 500")
67
- user.avatar.width(size: "Thumbnail")
68
- user.avatar.height(size: "Small 320")
69
-
70
- # alternative notation
71
- user.avatar.url(size: :medium_500)
72
- user.avatar.width(size: :thumbnail)
73
- user.avatar.height(size: :small_320)
74
-
75
- # defaults to "Original"
76
- user.avatar.url
77
- user.avatar.width
78
- user.avatar.height
79
- ```
40
+ user.avatar.url(size: :medium_500) # alternative notation
80
41
 
81
- All possible sizes are: "Square 75", "Thumbnail", "Square 150", "Small 240",
82
- "Small 320", "Medium 500", "Medium 640", "Medium 800", "Large 1024", "Large
83
- 1600", "Large 2048" and "Original".
42
+ user.avatar.url(size: "Original")
43
+ user.avatar.url(size: :original) # alternative notation
84
44
 
85
- Note that when using storage-flickr you shouldn't load the `store_dimensions`
86
- plugin, because they're not compatible.
45
+ All possible sizes are: "Square 75", "Thumbnail", "Square 150", "Small 240",
46
+ "Small 320", "Medium 500", "Medium 640", "Medium 800", "Large 1024", and
47
+ "Original".
87
48
 
88
49
  ### Album
89
50
 
@@ -109,12 +70,10 @@ key:
109
70
  ```rb
110
71
  class MyUploader < Shrine
111
72
  def extract_metadata(io, context)
112
- metadata = super
113
- metadata["flickr"] = {
73
+ super.update("flickr" => {
114
74
  title: io.original_filename,
115
75
  description: context[:record].description,
116
- }
117
- metadata
76
+ })
118
77
  end
119
78
  end
120
79
  ```
@@ -132,11 +91,11 @@ flickr.clear!(:confirm)
132
91
  ### Linking back
133
92
 
134
93
  In Flickr's guidelines it states that if you're displaying photos from Flickr
135
- on another webiste, you should always link back to Flickr. For that you can
136
- use `#flickr_url`:
94
+ on another webiste, you should always link back to Flickr. This link will be
95
+ generate when you don't pass any arguments to `#url`:
137
96
 
138
97
  ```erb
139
- <a href="<%= @user.avatar.flickr_url %>">
98
+ <a href="<%= @user.avatar.url %>">
140
99
  <img src="<%= @user.avatar.url(size: "Small 320") %>">
141
100
  </a>
142
101
  ```
@@ -1,13 +1,15 @@
1
1
  require "shrine"
2
2
  require "flickr-objects"
3
3
  require "down"
4
+ require "net/http"
5
+ require "uri"
4
6
 
5
7
  class Shrine
6
8
  module Storage
7
9
  class Flickr
8
10
  attr_reader :person, :flickr, :upload_options, :album
9
11
 
10
- def initialize(upload_options: {}, user:, access_token:, album: nil)
12
+ def initialize(user:, access_token:, album: nil, upload_options: {})
11
13
  @flickr = ::Flickr.new(*access_token)
12
14
  @person = @flickr.people.find(user)
13
15
  @upload_options = upload_options
@@ -15,47 +17,49 @@ class Shrine
15
17
  end
16
18
 
17
19
  def upload(io, id, metadata = {})
18
- options = metadata.delete("flickr") || {}
20
+ options = {title: metadata["filename"]}
19
21
  options.update(upload_options)
22
+ options.update(metadata.delete("flickr") || {})
20
23
 
21
24
  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
- photo.size!(size)
29
- metadata["flickr_sizes"] << {
30
- "name" => size,
31
- "url" => photo.source_url,
32
- "width" => photo.width,
33
- "height" => photo.height,
34
- }
35
- end
25
+ album.add_photo(photo_id) if album
26
+
27
+ photo = photo(photo_id).get_info!
28
+ id.replace(generate_id(photo))
29
+
30
+ photo.attributes
36
31
  end
37
32
 
38
33
  def download(id)
39
- raise NotImplementedError, "#download cannot be implemented"
34
+ Down.download(url(id, size: "Original"))
40
35
  end
41
36
 
42
37
  def open(id)
43
- raise NotImplementedError, "#open cannot be implemented"
38
+ download(id)
39
+ end
40
+
41
+ def read(id)
42
+ Net::HTTP.get(URI.parse(url(id, size: "Original")))
44
43
  end
45
44
 
46
45
  def exists?(id)
47
- !!photo(id).get_info!
46
+ !!photo(photo_id(id)).get_info!
48
47
  rescue ::Flickr::ApiError => error
49
48
  raise error if error.code != 1
50
49
  false
51
50
  end
52
51
 
53
52
  def delete(id)
54
- photo(id).delete
53
+ photo(photo_id(id)).delete
55
54
  end
56
55
 
57
- def url(id, **options)
58
- raise NotImplementedError, "#url cannot be implemented"
56
+ def url(id, size: nil)
57
+ if size
58
+ size = size.to_s.tr("_", " ").capitalize if size.is_a?(Symbol)
59
+ "https://farm%s.staticflickr.com/%s/%s_%s%s.%s" % size_data(size, id)
60
+ else
61
+ "https://www.flickr.com/photos/#{person.id}/#{photo_id(id)}"
62
+ end
59
63
  end
60
64
 
61
65
  def clear!(confirm = nil)
@@ -72,64 +76,38 @@ class Shrine
72
76
  def photo(id)
73
77
  flickr.photos.find(id)
74
78
  end
75
- end
76
- end
77
- end
78
-
79
- class Shrine
80
- module Plugins
81
- module Flickr
82
- module FileMethods
83
- def download
84
- if storage.is_a?(Storage::Flickr)
85
- Down.download(url)
86
- else
87
- super
88
- end
89
- end
90
-
91
- def url(**options)
92
- if storage.is_a?(Storage::Flickr)
93
- size_attribute("url", **options)
94
- else
95
- super
96
- end
97
- end
98
-
99
- def width(**options)
100
- size_attribute("width", **options)
101
- end
102
-
103
- def height(**options)
104
- size_attribute("height", **options)
105
- end
106
-
107
- def flickr_url
108
- "https://www.flickr.com/photos/#{storage.person.id}/#{id}"
109
- end
110
-
111
- private
112
79
 
113
- def size_attribute(key, size: "Original")
114
- size = size.to_s.tr("_", " ").capitalize if size.is_a?(Symbol)
115
- hash = flickr_sizes.find { |hash| hash["name"] == size }
116
- hash.fetch(key) if hash
80
+ private
81
+
82
+ def size_data(size, id)
83
+ farm, server, photo_id, secret, original_secret, original_format = id.split(/\W/)
84
+
85
+ case size
86
+ when "Square 75" then [farm, server, photo_id, secret, "_s", "jpg"]
87
+ when "Square 150" then [farm, server, photo_id, secret, "_q", "jpg"]
88
+ when "Thumbnail" then [farm, server, photo_id, secret, "_t", "jpg"]
89
+ when "Small 240" then [farm, server, photo_id, secret, "_m", "jpg"]
90
+ when "Small 320" then [farm, server, photo_id, secret, "_n", "jpg"]
91
+ when "Medium 500" then [farm, server, photo_id, secret, "", "jpg"]
92
+ when "Medium 640" then [farm, server, photo_id, secret, "_z", "jpg"]
93
+ when "Medium 800" then [farm, server, photo_id, secret, "_c", "jpg"]
94
+ when "Large 1024" then [farm, server, photo_id, secret, "_b", "jpg"]
95
+ when "Original" then [farm, server, photo_id, original_secret, "_o", original_format]
96
+ when "Large 1600", "Large 2048"
97
+ raise Shrine::Error, "#{size.inspect} size isn't available"
98
+ else
99
+ raise Shrine::Error, "unknown size: #{size.inspect}"
117
100
  end
101
+ end
118
102
 
119
- def io
120
- if storage.is_a?(Storage::Flickr)
121
- @io ||= download
122
- else
123
- super
124
- end
125
- end
103
+ def generate_id(photo)
104
+ "#{photo.farm}-#{photo.server}-#{photo.id}-#{photo.secret}" \
105
+ "-#{photo["originalsecret"]}.#{photo["originalformat"]}"
106
+ end
126
107
 
127
- def flickr_sizes
128
- metadata["flickr_sizes"]
129
- end
108
+ def photo_id(id)
109
+ id.split("-").fetch(2)
130
110
  end
131
111
  end
132
112
  end
133
-
134
- plugin Plugins::Flickr
135
113
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-flickr"
3
- gem.version = "0.2.0"
3
+ gem.version = "1.0.0"
4
4
 
5
5
  gem.required_ruby_version = ">= 2.1"
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-flickr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-11 00:00:00.000000000 Z
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: flickr-objects