Class: Fireimg::Client
- Inherits:
-
Object
- Object
- Fireimg::Client
- Defined in:
- lib/fireimg/client.rb
Instance Method Summary collapse
- #api_base ⇒ Object
- #cdn_base ⇒ Object
- #configured? ⇒ Boolean
- #delete(image_key) ⇒ Object
-
#initialize(config = Fireimg.configuration) ⇒ Client
constructor
A new instance of Client.
- #moderate(image_key) ⇒ Object
- #presign(filename:, content_type:, sizes: nil) ⇒ Object
- #presign_many(files:, sizes: nil) ⇒ Object
- #project ⇒ Object
- #public_url(image_key) ⇒ Object
-
#upload(filename:, content_type:, sizes: nil, **source) ⇒ Object
Presign + PUT, matching
fireimg uploadin the CLI. - #upload_many(files:, sizes: nil) ⇒ Object
-
#url(image_key, **options) ⇒ Object
CDN URL with query-parameter transforms, matching @fireimg/js getUrl.
Constructor Details
#initialize(config = Fireimg.configuration) ⇒ Client
Returns a new instance of Client.
9 10 11 |
# File 'lib/fireimg/client.rb', line 9 def initialize(config = Fireimg.configuration) @config = config end |
Instance Method Details
#api_base ⇒ Object
21 22 23 |
# File 'lib/fireimg/client.rb', line 21 def api_base @config.api_base end |
#cdn_base ⇒ Object
25 26 27 |
# File 'lib/fireimg/client.rb', line 25 def cdn_base @config.cdn_base end |
#configured? ⇒ Boolean
13 14 15 |
# File 'lib/fireimg/client.rb', line 13 def configured? @config.configured? end |
#delete(image_key) ⇒ Object
113 114 115 116 |
# File 'lib/fireimg/client.rb', line 113 def delete(image_key) request_json(Net::HTTP::Delete, "/v1/api/projects/#{project}/images/#{image_key}", nil) true end |
#moderate(image_key) ⇒ Object
109 110 111 |
# File 'lib/fireimg/client.rb', line 109 def moderate(image_key) post_json("/v1/api/projects/#{project}/images/moderation/#{image_key}", {}) end |
#presign(filename:, content_type:, sizes: nil) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/fireimg/client.rb', line 42 def presign(filename:, content_type:, sizes: nil) rows = presign_many(files: [{ filename: filename, content_type: content_type }], sizes: sizes) raise Error, 'FireImg presign returned no URLs' if rows.empty? rows.first end |
#presign_many(files:, sizes: nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/fireimg/client.rb', line 98 def presign_many(files:, sizes: nil) payload = { 'slug' => project, 'files' => Array(files).map { |file| file_payload(file) } } payload['sizes'] = Array(sizes).map { |size| Size.from(size).to_h } unless sizes.nil? body = post_json('/v1/api/presigned-upload-urls', payload) Array(body['urls']).map { |row| parse_presign_row(row) } end |
#project ⇒ Object
17 18 19 |
# File 'lib/fireimg/client.rb', line 17 def project @config.project end |
#public_url(image_key) ⇒ Object
29 30 31 32 |
# File 'lib/fireimg/client.rb', line 29 def public_url(image_key) key = image_key.to_s.sub(%r{\A/+}, '') "#{cdn_base}/#{project}/images/#{key}" end |
#upload(filename:, content_type:, sizes: nil, **source) ⇒ Object
Presign + PUT, matching fireimg upload in the CLI.
Provide the bytes with path:, io:, or body:.
51 52 53 54 55 56 57 58 59 |
# File 'lib/fireimg/client.rb', line 51 def upload(filename:, content_type:, sizes: nil, **source) rows = upload_many( files: [source.merge(filename: filename, content_type: content_type)], sizes: sizes ) raise Error, 'FireImg upload returned no results' if rows.empty? rows.first end |
#upload_many(files:, sizes: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/fireimg/client.rb', line 61 def upload_many(files:, sizes: nil) list = Array(files) raise Error, 'files cannot be empty' if list.empty? prepared = [] seen = {} list.each do |file| payload = file_payload(file) name = payload['filename'] raise Error, "duplicate filename #{name.inspect}" if seen.key?(name) seen[name] = true prepared << { payload: payload, content_type: payload['contentType'], bytes: read_bytes(file) } end rows = presign_many(files: prepared.map { |item| item[:payload] }, sizes: sizes) by_name = prepared.to_h { |item| [item[:payload]['filename'], item] } rows.map do |row| item = by_name[row.filename] raise Error, "presign returned unexpected filename #{row.filename.inspect}" if item.nil? put(row.upload_url, item[:content_type], item[:bytes]) image_key = row.filename.to_s.sub(%r{\A/+}, '') UploadResult.new( filename: image_key, image_key: image_key, cdn_url: public_url(image_key), s3_key: row.s3_key ) end end |
#url(image_key, **options) ⇒ Object
CDN URL with query-parameter transforms, matching @fireimg/js getUrl.
35 36 37 38 39 40 |
# File 'lib/fireimg/client.rb', line 35 def url(image_key, **) key = image_key.to_s.sub(%r{\A/+}, '') qs = query_string() base = "#{cdn_base}/#{project}/images/#{key}" qs.empty? ? base : "#{base}?#{qs}" end |