Skip to content

Commit

Permalink
RM-1085: SparkAPI Mods to create a generic "media" structure (#171)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hadleyn and hadleyn authored Feb 11, 2021
1 parent a43d161 commit 8500003
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v1.5.1
- Add support for video thumbnails and 'media' interface to unify shared parts of videos/virtual tours

v1.5.0
Why upgrade?
- Adding support for Ruby 3
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.0
1.5.1
1 change: 1 addition & 0 deletions lib/spark_api/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require 'spark_api/models/listing_cart'
require 'spark_api/models/listing_meta_translations'
require 'spark_api/models/market_statistics'
require 'spark_api/models/media'
require 'spark_api/models/message'
require 'spark_api/models/news_feed_meta'
require 'spark_api/models/newsfeed'
Expand Down
30 changes: 30 additions & 0 deletions lib/spark_api/models/media.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module SparkApi
module Models
module Media
# This module is effectively an interface and helper to combine common media
# actions and information. Media types (videos, virtual tours, etc)
# should include this module and implement the methods contained

def url
raise "Not Implemented"
end

def description
raise "Not Implemented"
end

def private?
attributes['Privacy'] == 'Private'
end

def public?
attributes['Privacy'] == 'Public'
end

def automatic?
attributes['Privacy'] == 'Automatic'
end

end
end
end
108 changes: 108 additions & 0 deletions lib/spark_api/models/video.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
require 'net/http'
module SparkApi
module Models
class Video < Base
extend Subresource
include Media
include Concerns::Savable,
Concerns::Destroyable

self.element_name = 'videos'

def branded?
Expand All @@ -11,6 +16,109 @@ def branded?
def unbranded?
attributes['Type'] == 'unbranded'
end

def url
attributes['ObjectHtml']
end

def description
attributes['Name']
end

# Some youtube URLS are youtu.be instead of youtube
SUPPORTED_VIDEO_TYPES = %w[vimeo youtu].freeze

def is_supported_type?
# Unfortunately there are so many formats of vimeo videos that we canot support all vimeo videos
# Therefore, we need to do a little more checking here and validate that we can get video codes out of the urls
(self.ObjectHtml.include?('youtu') && youtube_video_code.present?) || (self.ObjectHtml.include?('vimeo') && vimeo_video_code.present?)
end

def is_valid_iframe?
self.ObjectHtml.include?('<iframe') && self.ObjectHtml.include?('</iframe>')
end

# gets the thumbnail to be shown on supported (Vimeo and Youtube) videos
# YouTube provides a predictable url for each video's images
# for Vimeo, a get request is necessary
def display_image
url = self.video_link
if url
if(url.include?('youtube'))
youtube_thumbnail_url
else
vimeo_thumbnail_url
end
end
end

def video_link
return nil unless is_supported_type?

if self.ObjectHtml.include?('youtu')
youtube_link
elsif self.ObjectHtml.include?('vimeo')
vimeo_link
end
end

private

def vimeo_video_code
html = self.ObjectHtml
if html.match(/(src=)('|")((https:)?\/\/player\.vimeo\.com\/video\/)/)
new_url = html.split(/(src=')|(src=")/)
if new_url[2]
html = new_url[2].split(/("|')/)[0]
end
end
if html.match(/(?:.+?)?(player\.vimeo\.com|vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?))/)
code = html.split('/').last.split('?').first
# Vimeo Ids are always numerical
code.to_i.to_s === code ? code : nil
else
nil
end
end

# This if correctly embedded by the user is an embed
# If not, it could be pretty much anything
def youtube_video_code
html = self.ObjectHtml
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)/)
html.split(/([a-zA-Z0-9_-]{11})/)[1]
else
nil
end
end

def youtube_link
normalize_youtube_url
code = youtube_video_code
code ? "https://www.youtube.com/watch?v=#{code}" : nil
end

def vimeo_link
code = vimeo_video_code
code ? "https://vimeo.com/#{code}" : nil
end

def youtube_thumbnail_url
code = youtube_video_code
code ? "https://i1.ytimg.com/vi/#{code}/hqdefault.jpg" : nil
end

def vimeo_thumbnail_url
# due to the rate limiting issue that surfaced shortly before launch,
# we will temporarily not return vimeo thumbnails until
# there is bandwidth to implement the solution in FLEX-9959
return nil
end

def normalize_youtube_url
self.ObjectHtml.sub!('-nocookie', '')
end

end
end
end
16 changes: 16 additions & 0 deletions lib/spark_api/models/virtual_tour.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module SparkApi
module Models
class VirtualTour < Base
extend Subresource
include Media
include Concerns::Savable,
Concerns::Destroyable

self.element_name="virtualtours"


Expand All @@ -13,6 +17,18 @@ def unbranded?
attributes["Type"] == "unbranded"
end

def url
attributes['Uri']
end

def description
attributes['Name']
end

def display_image
# Currently we have no universally good mechanism to get images for virtual tours
return nil
end
end
end
end

0 comments on commit 8500003

Please sign in to comment.