class VastAnalyzer::ParserResult

Attributes

vast[R]
vast_version[R]

Public Class Methods

new(url, max_redirects: 5) click to toggle source
# File lib/vast_analyzer.rb, line 17
def initialize(url, max_redirects: 5)
  open_xml(url)
  vast_root = @vast&.xpath('//VAST')
  raise NotVastError.new("Not vast, url: #{url}") unless vast_root&.any?
  unwrap(max_redirects) unless @vast.xpath('//VASTAdTagURI').empty?
  @vast_version = vast_root.attr('version').value
end

Public Instance Methods

mediafiles() click to toggle source
# File lib/vast_analyzer.rb, line 47
def mediafiles
  @mediafiles ||= @vast.xpath('//MediaFile')&.map do |node|
    h = node.to_h
    h['url'] = node.content
    h
  end
end
skippable?() click to toggle source
# File lib/vast_analyzer.rb, line 37
def skippable?
  @skippable ||=
    case @vast_version
    when '2.0', '2.0.1'
      !!@vast.xpath('//Tracking')&.any? { |track| track.attr('event') == 'skip' }
    when '3.0'
      !!@vast.xpath('//Linear').attr('skipoffset')
    end
end
vpaid_status() click to toggle source
# File lib/vast_analyzer.rb, line 25
def vpaid_status
  if include_flash_vpaid? && include_js?
    'flash_js_vpaid'
  elsif include_flash_vpaid?
    'flash_vpaid'
  elsif include_js?
    'js_vpaid'
  else
    'neither'
  end
end

Private Instance Methods

include_flash_vpaid?() click to toggle source
# File lib/vast_analyzer.rb, line 89
def include_flash_vpaid?
  @include_flash ||= mediafiles.any? do |mediafile|
    is_vpaid_api = mediafile['apiFramework'] == 'VPAID'
    uses_flash = ['application/x-shockwave-flash', 'video/x-flv']
                 .include?(mediafile['type'])
    is_vpaid_api && uses_flash
  end
end
include_js?() click to toggle source
# File lib/vast_analyzer.rb, line 98
def include_js?
  @include_js ||= mediafiles.any? do |mediafile|
    ['application/x-javascript', 'application/javascript'].include?(mediafile['type'])
  end
end
open_xml(url, limit: 2) click to toggle source
# File lib/vast_analyzer.rb, line 57
def open_xml(url, limit: 2)
  raise ArgumentError.new('Too many HTTP redirects') if limit == 0
  uri = Addressable::URI.parse(url)
  response = Net::HTTP.get_response(uri)
  case response
  when Net::HTTPSuccess
    @vast = Nokogiri::XML(response.body){ |config| config.noblanks }
  when Net::HTTPRedirection
    open_xml(response['location'], :limit => limit - 1)
  else
    raise ErrorOpeningUrl.new("Net/http error, #{response.code}, #{response.message}"\
      "url: #{url}")
  end
rescue Timeout::Error
  raise UrlTimeoutError.new('Timeout error')
rescue StandardError => e
  raise ErrorOpeningUrl.new("Error opening url, #{e.message}")
end
unwrap(max_redirects) click to toggle source
# File lib/vast_analyzer.rb, line 76
def unwrap(max_redirects)
  max_redirects.times do
    return if @vast.xpath('//VASTAdTagURI').empty?
    begin
      url = @vast.xpath('//VASTAdTagURI')[0].content
      open_xml(url)
    rescue
      raise WrapperRedirectError.new('Error with opening the wrapper url')
    end
  end
  raise WrapperDepthError.new('Error: Wrapper depth exceeds five redirects')
end