Module: Shared::Api
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/shared/api.rb
Class Method Summary collapse
- .api_link(ar, id = nil) ⇒ Object
-
.host ⇒ Object
TODO: can we do away with these? Standard helpers would be preferred.
- .image_link(image, raise_on_no_token: true) ⇒ Object
- .image_metadata_link(image, raise_on_no_token: true) ⇒ Object
- .shorten_url(long_url) ⇒ Object
- .sound_link(sound) ⇒ Object
Class Method Details
.api_link(ar, id = nil) ⇒ Object
18 19 20 21 22 23 |
# File 'app/models/concerns/shared/api.rb', line 18 def self.api_link(ar, id = nil) s = host return s + '/api/v1/' if ar.nil? "#{s}/api/v1/#{ar.class.base_class.name.tableize}/#{id || ar.id}" end |
.host ⇒ Object
TODO: can we do away with these? Standard helpers would be preferred. !! These can be useful in a background or other context where you don’t have the normal request context and all that comes with it, but standard helpers should be preffered in general. !!
9 10 11 12 13 14 15 16 |
# File 'app/models/concerns/shared/api.rb', line 9 def self.host s = ENV['SERVER_NAME'] if s.nil? 'http://127.0.0.1:3000' else 'https://' + s end end |
.image_link(image, raise_on_no_token: true) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/concerns/shared/api.rb', line 25 def self.image_link(image, raise_on_no_token: true) s = host return s if image.nil? token = Project.find(image.project_id).api_access_token if token.nil? && raise_on_no_token raise TaxonWorks::Error, 'No project token available for image link!' end long = "#{s}/api/v1/images/file/sha/#{ image.image_file_fingerprint }?project_token=#{token}" shorten_url(long) end |
.image_metadata_link(image, raise_on_no_token: true) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/models/concerns/shared/api.rb', line 38 def self.(image, raise_on_no_token: true) s = host return s if image.nil? token = Project.find(image.project_id).api_access_token if token.nil? && raise_on_no_token raise TaxonWorks::Error, 'No project token available for image metadata link!' end long = "#{s}/api/v1/images/#{ image.id }?project_token=#{token}" shorten_url(long) end |
.shorten_url(long_url) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'app/models/concerns/shared/api.rb', line 60 def self.shorten_url(long_url) s = host return s if long_url.nil? # Stores long <-> short in db. short_key = Shortener::ShortenedUrl.generate(long_url).unique_key "#{s}/s/#{short_key}" end |
.sound_link(sound) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'app/models/concerns/shared/api.rb', line 51 def self.sound_link(sound) s = host return s if sound.nil? long = "#{s}/#{Rails.application.routes.url_helpers.rails_blob_path(Sound.last.sound_file, only_path: true)}" shorten_url(long) end |