94 lines
2.3 KiB
Ruby
94 lines
2.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative '../../log/loggable'
|
|
require_relative '../errors/schedule_fetch_error'
|
|
require 'net/http'
|
|
|
|
module Vendors
|
|
module Vistar
|
|
class VistarFetchAd
|
|
include Log::Loggable
|
|
|
|
PATH = 'api/v1/get_ad/json'
|
|
DEFAULT_SUPPORTED_MEDIA = %w[
|
|
application/x-shockwave-dynamic-flash
|
|
application/x-shockwave-flash
|
|
image/jpeg
|
|
image/png
|
|
video/mp4
|
|
video/mpeg
|
|
video/mpg
|
|
video/quicktime
|
|
video/webm
|
|
video/x-flv
|
|
video/x-ms-wmv
|
|
video/x-msvideo
|
|
]
|
|
|
|
def initialize(tokens)
|
|
@tokens = tokens
|
|
end
|
|
|
|
def call(params)
|
|
logger.info "Vistar fetch Ad request"
|
|
res = Net::HTTP.post(
|
|
Vendors::Vistar::VistarSettings.instance.vistar_url(PATH),
|
|
body(params),
|
|
{
|
|
"Content-Type": 'application/json'
|
|
}
|
|
)
|
|
case res
|
|
when Net::HTTPSuccess
|
|
JSON.parse(res.body).with_indifferent_access
|
|
else
|
|
logger.error "Error fetching Vistar Ad: #{res.code}"
|
|
raise Vendors::Errors::ScheduleFetchError, { response_code: res.code }.to_json
|
|
end
|
|
end
|
|
|
|
private
|
|
def body(params)
|
|
display_area = (params[:display_area] || [{}]).each_with_index.collect do |item, idx|
|
|
{
|
|
id: "display-#{idx}",
|
|
width: 1080,
|
|
height: 1920,
|
|
supported_media: DEFAULT_SUPPORTED_MEDIA,
|
|
static_duration: 8
|
|
}.merge(item)
|
|
end
|
|
params_whitelist(params).merge({
|
|
network_id: Vendors::Vistar::VistarSettings.instance.network_id,
|
|
api_key: "#{@tokens.api_key}",
|
|
direct_connection: false,
|
|
display_area: display_area
|
|
}).to_json
|
|
end
|
|
|
|
def params_whitelist(params)
|
|
params.slice(
|
|
:device_id,
|
|
:venue_id,
|
|
:display_time,
|
|
:device_attribute,
|
|
:name,
|
|
:display_area,
|
|
:id,
|
|
:width,
|
|
:height,
|
|
:allow_audio,
|
|
:supported_media,
|
|
:min_duration,
|
|
:max_duration,
|
|
:order_id,
|
|
:max_file_size_bytes,
|
|
:static_duration,
|
|
:latitude,
|
|
:longitude
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|