Module: Shared::Api

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/shared/api.rb

Class Method Summary collapse

Class Method Details

.api_base_path(model_or_instance) ⇒ Object



18
19
20
21
22
23
# File 'app/models/concerns/shared/api.rb', line 18

def self.api_base_path(model_or_instance)
  return "#{host}/api/v1" if model_or_instance.nil?

  klass = model_or_instance.is_a?(Class) ? model_or_instance : model_or_instance.class
  "#{host}/api/v1/#{klass.base_class.name.tableize}"
end


25
26
27
28
29
# File 'app/models/concerns/shared/api.rb', line 25

def self.api_link(ar, id = nil)
  return "#{host}/api/v1" if ar.nil?

  "#{api_base_path(ar)}/#{id || ar.id}"
end


31
32
33
34
35
# File 'app/models/concerns/shared/api.rb', line 31

def self.api_link_for_model_id(klass, id)
  return "#{host}/api/v1" if klass.nil?

  "#{api_base_path(klass)}/#{id}"
end

.bulk_lookup_shortened_urls(long_urls) ⇒ Hash<String, String>

Bulk lookup existing shortened URLs Returns a hash mapping long URLs to their short keys

Parameters:

  • long_urls (Array<String>)

    array of long URLs to lookup

Returns:

  • (Hash<String, String>)

    hash mapping long_url => unique_key



145
146
147
148
149
150
151
152
# File 'app/models/concerns/shared/api.rb', line 145

def self.bulk_lookup_shortened_urls(long_urls)
  return {} if long_urls.empty?

  ::Shortener::ShortenedUrl
    .where(url: long_urls)
    .pluck(:url, :unique_key)
    .to_h
end

.hostObject

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_file_long_url(fingerprint, token) ⇒ String

Build long URL for image file access without shortening.

Parameters:

  • fingerprint (String)

    image_file_fingerprint

  • token (String)

    project API access token

Returns:

  • (String)

    full long URL



109
110
111
# File 'app/models/concerns/shared/api.rb', line 109

def self.image_file_long_url(fingerprint, token)
  "#{image_file_url_prefix}#{fingerprint}?project_token=#{token}"
end

.image_file_url_prefixString

URL prefix for image file access.

Returns:

  • (String)

    URL prefix



95
96
97
# File 'app/models/concerns/shared/api.rb', line 95

def self.image_file_url_prefix
  "#{host}/api/v1/images/file/sha/"
end


37
38
39
40
41
42
43
44
45
46
# File 'app/models/concerns/shared/api.rb', line 37

def self.image_link(image, raise_on_no_token: true, token: nil)
  return host 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

  shorten_url(image_file_long_url(image.image_file_fingerprint, token))
end


48
49
50
51
52
53
54
55
56
57
# File 'app/models/concerns/shared/api.rb', line 48

def self.(image, raise_on_no_token: true, token: nil)
  return host 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

  shorten_url((image.id, token))
end

.image_metadata_long_url(image_id, token) ⇒ String

Build long URL for image metadata without shortening.

Parameters:

  • image_id (Integer)

    image ID

  • token (String)

    project API access token

Returns:

  • (String)

    full long URL



117
118
119
# File 'app/models/concerns/shared/api.rb', line 117

def self.(image_id, token)
  "#{}#{image_id}?project_token=#{token}"
end

.image_metadata_url_prefixString

URL prefix for image metadata (without image_id or token).

Returns:

  • (String)

    URL prefix



101
102
103
# File 'app/models/concerns/shared/api.rb', line 101

def self.
  "#{host}/api/v1/images/"
end

.short_url_from_key(unique_key) ⇒ String

Build short URL from unique key

Parameters:

  • unique_key (String)

    shortened URL unique key

Returns:

  • (String)

    full short URL



137
138
139
# File 'app/models/concerns/shared/api.rb', line 137

def self.short_url_from_key(unique_key)
  "#{host}/s/#{unique_key}"
end

.shorten_url(long_url) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'app/models/concerns/shared/api.rb', line 83

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

.sled_image_file_long_url(fingerprint, svg_view_box, token) ⇒ String

Build long URL for sled image extraction.

Parameters:

  • fingerprint (String)

    image_file_fingerprint

  • svg_view_box (String)

    SVG viewBox value “x y width height”

  • token (String)

    project API access token

Returns:

  • (String)

    full long URL for cropped sled image



126
127
128
129
130
131
132
# File 'app/models/concerns/shared/api.rb', line 126

def self.sled_image_file_long_url(fingerprint, svg_view_box, token)
  x, y, w, h = svg_view_box.split(' ')
  box_width = w.to_i
  box_height = h.to_i

  "#{image_file_url_prefix}#{fingerprint}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}?project_token=#{token}"
end


59
60
61
62
63
64
65
66
# File 'app/models/concerns/shared/api.rb', line 59

def self.sound_link(sound)
  s = host
  return s if sound.nil?

  long = "#{s}/#{Rails.application.routes.url_helpers.rails_blob_path(sound.sound_file, only_path: true)}"

  shorten_url(long)
end


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/concerns/shared/api.rb', line 68

def self.(sound, raise_on_no_token: true, token: nil)
  s = host
  return s if sound.nil?

  token ||= Project.find(sound.project_id).api_access_token
  if token.nil? && raise_on_no_token
    raise TaxonWorks::Error, 'No project token available for sound link!'
  end

  long = "#{s}/api/v1/sounds/#{ sound.id }?project_token=#{token}"

  shorten_url(long)
end