Class: Iron::Content
- Inherits:
-
Object
- Object
- Iron::Content
- Defined in:
- app/models/iron/content.rb,
app/models/iron/content/download_budget.rb
Defined Under Namespace
Classes: DownloadBudget, Error, InvalidContent
Constant Summary collapse
- MAX_DOWNLOAD_REDIRECTS =
5- MAX_DOWNLOAD_BYTES =
50.megabytes
- DOWNLOAD_OPEN_TIMEOUT =
5- DOWNLOAD_READ_TIMEOUT =
30
Class Method Summary collapse
- .create(handle, content, route: nil, locale: nil, actor: nil, allow_local_files: true) ⇒ Object
- .delete(handle, id: nil, route: nil, actor: nil) ⇒ Object
- .download(url, filename: nil, content_type: nil, budget: DownloadBudget.new) ⇒ Object
- .get(handle, id: nil, route: nil, locale: nil) ⇒ Object
- .list(handle, locale: nil) ⇒ Object
- .serialize(entry) ⇒ Object
- .update(handle, content, id: nil, route: nil, locale: nil, actor: nil, allow_local_files: true) ⇒ Object
Class Method Details
.create(handle, content, route: nil, locale: nil, actor: nil, allow_local_files: true) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/iron/content.rb', line 34 def create(handle, content, route: nil, locale: nil, actor: nil, allow_local_files: true) type = content_type(handle) raise Error, "#{handle} is a single — use update" if type.single? acting_as(actor, locale) do entry = type.entries.build entry.route = route if route write_entry(entry, content, allow_local_files:) end end |
.delete(handle, id: nil, route: nil, actor: nil) ⇒ Object
53 54 55 56 57 58 59 |
# File 'app/models/iron/content.rb', line 53 def delete(handle, id: nil, route: nil, actor: nil) acting_as(actor, nil) do entry = locate_entry(content_type(handle), id:, route:) entry.destroy! { "deleted" => true, "id" => entry.id } end end |
.download(url, filename: nil, content_type: nil, budget: DownloadBudget.new) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/iron/content.rb', line 65 def download(url, filename: nil, content_type: nil, budget: DownloadBudget.new) io = fetch_guarded(url, budget) ActiveStorage::Blob.create_and_upload!( io:, filename: filename.presence || File.basename(URI.parse(url).path).presence || "upload", content_type: ) rescue URI::InvalidURIError raise Error, "could not download #{url}: invalid URL" rescue SocketError, SystemCallError, OpenSSL::SSL::SSLError, Net::OpenTimeout, Net::ReadTimeout, Timeout::Error, Net::HTTPBadResponse => error raise Error, "could not download #{url}: #{error.}" end |
.get(handle, id: nil, route: nil, locale: nil) ⇒ Object
28 29 30 31 32 |
# File 'app/models/iron/content.rb', line 28 def get(handle, id: nil, route: nil, locale: nil) acting_as(nil, locale) do render_entry(locate_entry(content_type(handle), id:, route:)) end end |
.list(handle, locale: nil) ⇒ Object
22 23 24 25 26 |
# File 'app/models/iron/content.rb', line 22 def list(handle, locale: nil) acting_as(nil, locale) do content_type(handle).entries.order(:id).map { |entry| render_entry(entry) } end end |
.serialize(entry) ⇒ Object
61 62 63 |
# File 'app/models/iron/content.rb', line 61 def serialize(entry) render_entry(entry) end |
.update(handle, content, id: nil, route: nil, locale: nil, actor: nil, allow_local_files: true) ⇒ Object
45 46 47 48 49 50 51 |
# File 'app/models/iron/content.rb', line 45 def update(handle, content, id: nil, route: nil, locale: nil, actor: nil, allow_local_files: true) type = content_type(handle) acting_as(actor, locale) do write_entry(entry_for_update(type, id:, route:), content, allow_local_files:) end end |