Module: Shared::Api

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

Class Method Summary collapse

Class Method Details



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

.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


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

def self.image_link(image)
  s = host
  return s if image.nil?

  token = Project.find(image.project_id).api_access_token
  long = "#{s}/api/v1/images/file/sha/#{ image.image_file_fingerprint }?project_token=#{token}"

  shorten_url(long)
end


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

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

  token = Project.find(image.project_id).api_access_token
  long = "#{s}/api/v1/images/#{ image.id }?project_token=#{token}"

  shorten_url(long)
end

.shorten_url(long_url) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'app/models/concerns/shared/api.rb', line 54

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


45
46
47
48
49
50
51
52
# File 'app/models/concerns/shared/api.rb', line 45

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