Module: Foobara::RemoteImports::ImportBase
Defined Under Namespace
Classes: BadManifestInputsError, NotFoundError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#manifest_data ⇒ Object
Returns the value of attribute manifest_data.
57
58
59
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 57
def manifest_data
@manifest_data
end
|
#manifest_to_import ⇒ Object
Returns the value of attribute manifest_to_import.
57
58
59
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 57
def manifest_to_import
@manifest_to_import
end
|
#manifests_to_import ⇒ Object
Returns the value of attribute manifests_to_import.
57
58
59
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 57
def manifests_to_import
@manifests_to_import
end
|
Class Method Details
.included(klass) ⇒ Object
[View source]
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 28
def included(klass)
klass.possible_input_error :to_import, NotFoundError
klass.possible_error BadManifestInputsError
klass.inputs(
manifest_url: :string,
raw_manifest: :associative_array,
cache: { type: :boolean, default: true },
cache_path: { type: :string, default: "tmp/cache/foobara-remote-imports" },
to_import: :duck,
already_imported: { type: :duck, allow_nil: true },
deanonymize_models: { type: :boolean, default: true }
)
klass.result :duck
end
|
Instance Method Details
#already_exists? ⇒ Boolean
[View source]
74
75
76
77
78
79
80
81
82
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 74
def already_exists?
method = "foobara_lookup_#{manifest_to_import.scoped_category}"
Foobara.foobara_root_namespace.send(
method,
manifest_to_import.reference,
mode: Namespace::LookupMode::ABSOLUTE
)
end
|
#already_imported ⇒ Object
[View source]
84
85
86
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 84
def already_imported
inputs[:already_imported] || AlreadyImported.new
end
|
#cache_file_path ⇒ Object
[View source]
179
180
181
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 179
def cache_file_path
@cache_file_path ||= "#{cache_path}/#{cache_key}.json"
end
|
#cache_key ⇒ Object
[View source]
170
171
172
173
174
175
176
177
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 170
def cache_key
@cache_key ||= begin
hash = Digest::MD5.hexdigest(manifest_url)
escaped_url = manifest_url.gsub(/[^\w]/, "_")
"#{hash}_#{escaped_url}"
end
end
|
#cache_manifest ⇒ Object
[View source]
153
154
155
156
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 153
def cache_manifest
FileUtils.mkdir_p(cache_path)
File.write(cache_file_path, manifest_data.to_json)
end
|
#cached? ⇒ Boolean
[View source]
162
163
164
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 162
def cached?
File.exist?(cache_file_path)
end
|
#determine_manifests_to_import ⇒ Object
[View source]
183
184
185
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 183
def determine_manifests_to_import
self.manifests_to_import = find_manifests_to_import
end
|
#execute ⇒ Object
[View source]
46
47
48
49
50
51
52
53
54
55
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 46
def execute
load_manifest
cache_manifest if should_cache?
determine_manifests_to_import
filter_manifests_to_import
import_objects_from_manifests
imported_objects
end
|
#filter_manifests_to_import ⇒ Object
TODO: feels like a command smell to pass manifests here… reconsider algorithm
[View source]
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 115
def filter_manifests_to_import
return if to_import.nil? || to_import.empty?
references = manifests_to_import.map(&:reference)
filter = Util.array(to_import)
filter.map! do |name|
if references.include?(name)
name
else
suffix = "::#{name}"
partial_matches = references.select { |reference| reference.end_with?(suffix) }
if partial_matches.size == 1
partial_matches.first
else
name
end
end
end
not_found = filter - references
if not_found.any?
add_input_error :to_import, :not_found, "Could not find #{not_found}", not_found:
end
self.manifests_to_import = manifests_to_import.select do |manifest|
filter.include?(manifest.reference)
end
end
|
#find_manifests_to_import ⇒ Object
[View source]
187
188
189
190
191
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 187
def find_manifests_to_import
raise "subclass responsibility"
end
|
#import_object_from_manifest ⇒ Object
[View source]
206
207
208
209
210
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 206
def import_object_from_manifest
raise "subclass responsibility"
end
|
#import_objects_from_manifests ⇒ Object
[View source]
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 193
def import_objects_from_manifests
manifests_to_import.each do |manifest|
unless already_imported.already_imported?(manifest)
self.manifest_to_import = manifest
next if already_exists?
imported_objects << import_object_from_manifest
already_imported << manifest
end
end
end
|
#imported_objects ⇒ Object
[View source]
212
213
214
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 212
def imported_objects
@imported_objects ||= []
end
|
#load_cached_manifest ⇒ Object
[View source]
166
167
168
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 166
def load_cached_manifest
JSON.parse(File.read(cache_file_path))
end
|
#load_manifest ⇒ Object
[View source]
88
89
90
91
92
93
94
95
96
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 88
def load_manifest
self.manifest_data = if raw_manifest
raw_manifest
elsif cache && cached?
load_cached_manifest
else
load_manifest_from_url
end
end
|
#load_manifest_from_url ⇒ Object
[View source]
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 98
def load_manifest_from_url
url = URI.parse(manifest_url)
response = Net::HTTP.get_response(url)
manifest_json = if response.is_a?(Net::HTTPSuccess)
response.body
else
raise "Could not get manifest from #{url}: " \
"#{response.code} #{response.message}"
end
JSON.parse(manifest_json)
end
|
#root_manifest ⇒ Object
[View source]
149
150
151
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 149
def root_manifest
@root_manifest ||= Manifest::RootManifest.new(manifest_data)
end
|
#should_cache? ⇒ Boolean
[View source]
158
159
160
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 158
def should_cache?
cache && manifest_url && !cached?
end
|
#validate ⇒ Object
[View source]
59
60
61
62
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 59
def validate
super
validate_manifest
end
|
#validate_manifest ⇒ Object
[View source]
64
65
66
67
68
69
70
71
72
|
# File 'foobara-remote-imports-0.0.14/src/foobara/remote_imports/import_base.rb', line 64
def validate_manifest
if manifest_url
if raw_manifest
add_runtime_error :bad_manifest_inputs, "Cannot provide both manifest_url and raw_manifest"
end
elsif !raw_manifest
add_runtime_error :bad_manifest_inputs, "Must provide either manifest_url or raw_manifest"
end
end
|