tanoshimu-utils 1.0.1 → 1.0.2
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
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 531a670d4c79e8729cf9510d3d3c264aa642f09feb5b54c69a9e8ecb53028040
|
4
|
+
data.tar.gz: baa54ab093b305062b1f824379e3b5d53113164887b41f758c04dae5dd55e040
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 487215b02647a3b805d13a8cf7c2e663a37e051c17428a9bde2f456eb779c8d5b7a1648a1d77c306d55b422ca52d6bd6df0811b2b03dfd54e5594ad47afb4fa9
|
7
|
+
data.tar.gz: 8c7bd03cd34e77c5c17ad0a7f3eff9aa925eb67d5ee48e4d1ae8daae77eec292d17caa418a740ab69de6494a0c55782c287e18942eddc748924e3fd49f5d7807
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'concerns/resource_fetch'
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module ResourceFetch
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
class ResourceNotAttachable < StandardError
|
5
|
+
def initialize(resource_name)
|
6
|
+
super("Resource #{resource_name} not attachable.")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class_methods do
|
11
|
+
def has_resource(resource_name, default_url: '/', expiry: 1.day)
|
12
|
+
resource_url = :"#{resource_name}_url"
|
13
|
+
db_resource_url = :"db_#{resource_url}?"
|
14
|
+
send(:define_method, db_resource_url) do
|
15
|
+
@db_resource_url ||= self.class.column_names.include?(resource_url.to_s)
|
16
|
+
end
|
17
|
+
send(:define_method, resource_url) do
|
18
|
+
ensure_fetchable_resource!(resource_name)
|
19
|
+
resource_url_for(resource_name, default_url: default_url, expiry: expiry)
|
20
|
+
end
|
21
|
+
send(:define_method, "#{resource_url}!") do
|
22
|
+
attachment = resource_for(resource_name).attachment
|
23
|
+
return default_url if attachment.nil?
|
24
|
+
|
25
|
+
if Config.uses_disk_storage?
|
26
|
+
Rails.application.routes.url_helpers.rails_blob_url(attachment, only_path: true)
|
27
|
+
else
|
28
|
+
attachment.service_url(expires_in: expiry)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
send(:define_method, "#{resource_name}?") do
|
32
|
+
(fetch!(resource_name, default_url)&.attached?).present?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def resource_for(resource_name)
|
40
|
+
method(resource_name).call
|
41
|
+
end
|
42
|
+
|
43
|
+
def resource_url_for(resource_name, default_url: '/', expiry: 1.day)
|
44
|
+
ensure_attachable_resource!(resource_name)
|
45
|
+
resource_url = :"#{resource_name}_url"
|
46
|
+
if try(:"db_#{resource_url}?")
|
47
|
+
self[resource_url] || default_url
|
48
|
+
else
|
49
|
+
resource = resource_for(resource_name)
|
50
|
+
if resource.attached?
|
51
|
+
resource.service_url(expires_in: expiry)
|
52
|
+
else
|
53
|
+
default_url
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def fetch!(resource_name, default_url)
|
59
|
+
ensure_attachable_resource!(resource_name)
|
60
|
+
resource = resource_for(resource_name)
|
61
|
+
#return resource if resource.attached?
|
62
|
+
|
63
|
+
#resource.attach(io: File.open("./public/#{default_url}"), filename: "episode-#{id}")
|
64
|
+
|
65
|
+
rescue ResourceFetch::ResourceNotAttachable, Errno::ENOENT
|
66
|
+
nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def ensure_fetchable_resource!(resource_name)
|
70
|
+
raise NoMethodError, "Don't know how to fetch #{resource_name}" unless respond_to?(resource_name)
|
71
|
+
end
|
72
|
+
|
73
|
+
def ensure_attachable_resource!(resource_name)
|
74
|
+
resource_for(resource_name)
|
75
|
+
rescue NameError
|
76
|
+
raise ResourceFetch::ResourceNotAttachable.new(resource_name)#, resource unless resource.respond_to?(:attached?)
|
77
|
+
end
|
78
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tanoshimu-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akinyele Cafe-Febrissy
|
@@ -31,7 +31,10 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
-
- lib/
|
34
|
+
- lib/tanoshimu_utils.rb
|
35
|
+
- lib/tanoshimu_utils/concerns.rb
|
36
|
+
- lib/tanoshimu_utils/concerns/resource_fetch.rb
|
37
|
+
- lib/tanoshimu_utils/version.rb
|
35
38
|
homepage: https://github.com/thedrummeraki
|
36
39
|
licenses:
|
37
40
|
- MIT
|
data/lib/tanoshimu-utils.rb
DELETED