Class: OEmbed::ApplicationSerializer

Inherits:
Object
  • Object
show all
Defined in:
app/serializers/o_embed/application_serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ ApplicationSerializer

Returns a new instance of ApplicationSerializer.



3
4
5
6
7
# File 'app/serializers/o_embed/application_serializer.rb', line 3

def initialize(object, options = {})
  @object = object
  @maxwidth = options[:maxwidth]
  @maxheight = options[:maxheight]
end

Instance Method Details

#author_properties(creator) ⇒ Object



18
19
20
21
22
23
24
# File 'app/serializers/o_embed/application_serializer.rb', line 18

def author_properties(creator)
  return {} if creator.nil?
  {
    author_name: creator.name,
    author_url: Rails.application.routes.url_helpers.creator_url(creator)
  }
end

#generic_properties ⇒ Object



9
10
11
12
13
14
15
16
# File 'app/serializers/o_embed/application_serializer.rb', line 9

def generic_properties
  {
    version: "1.0",
    provider_name: ENV.fetch("SITE_NAME", "Manyfold"),
    provider_url: Rails.application.routes.url_helpers.root_url,
    cache_age: 86400
  }
end


42
43
44
45
46
# File 'app/serializers/o_embed/application_serializer.rb', line 42

def link_properties
  {
    type: "link"
  }
end

#model_file_properties(model_file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/serializers/o_embed/application_serializer.rb', line 26

def model_file_properties(model_file)
  props = if model_file&.is_image?
    photo_properties(model_file)
  elsif Components::Renderers::Three.supports?(model_file)
    renderable_properties(model_file)
  elsif model_file&.is_video?
    video_properties(model_file)
  else
    link_properties
  end
  props.merge({
    name: model_file&.name,
    summary: model_file&.caption
  }).compact
end

#photo_properties(model_file) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'app/serializers/o_embed/application_serializer.rb', line 48

def photo_properties(model_file)
  width = @maxwidth || 512 # TODO proper scale calc based on image size
  height = @maxheight || 512
  {
    type: "photo",
    url: Rails.application.routes.url_helpers.model_model_file_url(model_file.model, model_file, format: model_file.extension),
    width: width,
    height: height,
    mediaType: model_file.mime_type.to_s
  }
end

#renderable_properties(model_file) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/serializers/o_embed/application_serializer.rb', line 81

def renderable_properties(model_file)
  width = @maxwidth || 512
  height = @maxheight || 512
  html = "    <iframe\n      src=\"\#{Rails.application.routes.url_helpers.model_model_file_url(model_file.model, model_file, embed: true)}\"\n      width=\"\#{width}\" height=\"\#{height}\" loading=\"lazy\" referrerpolicy=\"no-referrer\" scrolling=\"no\"\n      style=\"overflow:hidden; border: none\" sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\">\n    </iframe>\n  EOF\n  {\n    type: \"rich\",\n    html: html,\n    width: width,\n    height: height,\n    mediaType: model_file.mime_type.to_s\n  }\nend\n"

#video_properties(model_file) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/serializers/o_embed/application_serializer.rb', line 60

def video_properties(model_file)
  width = @maxwidth || 512
  height = [@maxheight, width * 0.75].compact.min # TODO proper aspect ratio calculation
  html = "    <video controls width=\"\#{width}\" height=\"\#{height}\">\n      <source\n        src=\"\#{Rails.application.routes.url_helpers.model_model_file_url(model_file.model, model_file, format: model_file.extension)}\"\n        type=\"\#{model_file.mime_type}\"\n      />\n    </video>\n  EOF\n  {\n    type: \"video\",\n    url: Rails.application.routes.url_helpers.model_model_file_url(model_file.model, model_file, format: model_file.extension),\n    html: html,\n    width: width,\n    height: height,\n    mediaType: model_file.mime_type.to_s\n  }\nend\n"