Skip to content

Commit 8500003

Browse files
authored
RM-1085: SparkAPI Mods to create a generic "media" structure (#171)
* RM-1127: Added media_privacy module * RM-1127: Changing how this module is brought in * RM-1127 Revert last change * RM-1127: Fixed missing require * RM-1127: Testing non-module version * RM-1127: Revert last change * RM-1127: Allowing video and virtual_tour to be savable * RM-1127: Update path support * RM-1127: Undoing last change * RM-1127: Tweaking this to provide better shared functionality. * RM-1127: Missed a spot * RM-1127: Video and virtual tour thumbnail code * RM-1127: Nixing OGP stuff for virtual tour display image * RM-1127: Added media_privacy module * RM-1127: Changing how this module is brought in * RM-1127 Revert last change * RM-1127: Fixed missing require * RM-1127: Testing non-module version * RM-1127: Revert last change * RM-1127: Allowing video and virtual_tour to be savable * RM-1127: Update path support * RM-1127: Undoing last change * RM-1127: Tweaking this to provide better shared functionality. * RM-1127: Missed a spot * RM-1127: Video and virtual tour thumbnail code * RM-1127: Nixing OGP stuff for virtual tour display image * RM-1085: Version bump Co-authored-by: Hadley Markoski <hmarkoski@fbsdata.com>
1 parent a43d161 commit 8500003

File tree

6 files changed

+159
-1
lines changed

6 files changed

+159
-1
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v1.5.1
2+
- Add support for video thumbnails and 'media' interface to unify shared parts of videos/virtual tours
3+
14
v1.5.0
25
Why upgrade?
36
- Adding support for Ruby 3

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.5.0
1+
1.5.1

lib/spark_api/models.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
require 'spark_api/models/listing_cart'
2626
require 'spark_api/models/listing_meta_translations'
2727
require 'spark_api/models/market_statistics'
28+
require 'spark_api/models/media'
2829
require 'spark_api/models/message'
2930
require 'spark_api/models/news_feed_meta'
3031
require 'spark_api/models/newsfeed'

lib/spark_api/models/media.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module SparkApi
2+
module Models
3+
module Media
4+
# This module is effectively an interface and helper to combine common media
5+
# actions and information. Media types (videos, virtual tours, etc)
6+
# should include this module and implement the methods contained
7+
8+
def url
9+
raise "Not Implemented"
10+
end
11+
12+
def description
13+
raise "Not Implemented"
14+
end
15+
16+
def private?
17+
attributes['Privacy'] == 'Private'
18+
end
19+
20+
def public?
21+
attributes['Privacy'] == 'Public'
22+
end
23+
24+
def automatic?
25+
attributes['Privacy'] == 'Automatic'
26+
end
27+
28+
end
29+
end
30+
end

lib/spark_api/models/video.rb

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
require 'net/http'
12
module SparkApi
23
module Models
34
class Video < Base
45
extend Subresource
6+
include Media
7+
include Concerns::Savable,
8+
Concerns::Destroyable
9+
510
self.element_name = 'videos'
611

712
def branded?
@@ -11,6 +16,109 @@ def branded?
1116
def unbranded?
1217
attributes['Type'] == 'unbranded'
1318
end
19+
20+
def url
21+
attributes['ObjectHtml']
22+
end
23+
24+
def description
25+
attributes['Name']
26+
end
27+
28+
# Some youtube URLS are youtu.be instead of youtube
29+
SUPPORTED_VIDEO_TYPES = %w[vimeo youtu].freeze
30+
31+
def is_supported_type?
32+
# Unfortunately there are so many formats of vimeo videos that we canot support all vimeo videos
33+
# Therefore, we need to do a little more checking here and validate that we can get video codes out of the urls
34+
(self.ObjectHtml.include?('youtu') && youtube_video_code.present?) || (self.ObjectHtml.include?('vimeo') && vimeo_video_code.present?)
35+
end
36+
37+
def is_valid_iframe?
38+
self.ObjectHtml.include?('<iframe') && self.ObjectHtml.include?('</iframe>')
39+
end
40+
41+
# gets the thumbnail to be shown on supported (Vimeo and Youtube) videos
42+
# YouTube provides a predictable url for each video's images
43+
# for Vimeo, a get request is necessary
44+
def display_image
45+
url = self.video_link
46+
if url
47+
if(url.include?('youtube'))
48+
youtube_thumbnail_url
49+
else
50+
vimeo_thumbnail_url
51+
end
52+
end
53+
end
54+
55+
def video_link
56+
return nil unless is_supported_type?
57+
58+
if self.ObjectHtml.include?('youtu')
59+
youtube_link
60+
elsif self.ObjectHtml.include?('vimeo')
61+
vimeo_link
62+
end
63+
end
64+
65+
private
66+
67+
def vimeo_video_code
68+
html = self.ObjectHtml
69+
if html.match(/(src=)('|")((https:)?\/\/player\.vimeo\.com\/video\/)/)
70+
new_url = html.split(/(src=')|(src=")/)
71+
if new_url[2]
72+
html = new_url[2].split(/("|')/)[0]
73+
end
74+
end
75+
if html.match(/(?:.+?)?(player\.vimeo\.com|vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?))/)
76+
code = html.split('/').last.split('?').first
77+
# Vimeo Ids are always numerical
78+
code.to_i.to_s === code ? code : nil
79+
else
80+
nil
81+
end
82+
end
83+
84+
# This if correctly embedded by the user is an embed
85+
# If not, it could be pretty much anything
86+
def youtube_video_code
87+
html = self.ObjectHtml
88+
if html.match(/(?:.+?)?(?:\/v\/|watch\/|\?v=|\&v=|youtu\.be\/|\/v=|^youtu\.be\/|embed\/|watch\%3Fv\%3D)([a-zA-Z0-9_-]{11})/) || html.match(/(iframe)(.*)(src=)('|")(https:\/\/www\.youtube\.com\/embed)/)
89+
html.split(/([a-zA-Z0-9_-]{11})/)[1]
90+
else
91+
nil
92+
end
93+
end
94+
95+
def youtube_link
96+
normalize_youtube_url
97+
code = youtube_video_code
98+
code ? "https://www.youtube.com/watch?v=#{code}" : nil
99+
end
100+
101+
def vimeo_link
102+
code = vimeo_video_code
103+
code ? "https://vimeo.com/#{code}" : nil
104+
end
105+
106+
def youtube_thumbnail_url
107+
code = youtube_video_code
108+
code ? "https://i1.ytimg.com/vi/#{code}/hqdefault.jpg" : nil
109+
end
110+
111+
def vimeo_thumbnail_url
112+
# due to the rate limiting issue that surfaced shortly before launch,
113+
# we will temporarily not return vimeo thumbnails until
114+
# there is bandwidth to implement the solution in FLEX-9959
115+
return nil
116+
end
117+
118+
def normalize_youtube_url
119+
self.ObjectHtml.sub!('-nocookie', '')
120+
end
121+
14122
end
15123
end
16124
end

lib/spark_api/models/virtual_tour.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ module SparkApi
22
module Models
33
class VirtualTour < Base
44
extend Subresource
5+
include Media
6+
include Concerns::Savable,
7+
Concerns::Destroyable
8+
59
self.element_name="virtualtours"
610

711

@@ -13,6 +17,18 @@ def unbranded?
1317
attributes["Type"] == "unbranded"
1418
end
1519

20+
def url
21+
attributes['Uri']
22+
end
23+
24+
def description
25+
attributes['Name']
26+
end
27+
28+
def display_image
29+
# Currently we have no universally good mechanism to get images for virtual tours
30+
return nil
31+
end
1632
end
1733
end
1834
end

0 commit comments

Comments
 (0)