Class: IframeParser

Inherits:
Parser show all
Defined in:
lib/Parsers/IframeParser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Parser

#initialize, #setNext

Constructor Details

This class inherits a constructor from Parser

Instance Attribute Details

#nextParserObject

Returns the value of attribute nextParser.



14
15
16
# File 'lib/Parsers/IframeParser.rb', line 14

def nextParser
  @nextParser
end

#pathPolicyObject

Returns the value of attribute pathPolicy.



14
15
16
# File 'lib/Parsers/IframeParser.rb', line 14

def pathPolicy
  @pathPolicy
end

Instance Method Details

#parse(paragraph) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/Parsers/IframeParser.rb', line 15

def parse(paragraph)
    if paragraph.type == 'IFRAME'
        if !paragraph.iframe.src.nil? && paragraph.iframe.src != ""
            url = paragraph.iframe.src
        else
            url = "https://medium.com/media/#{paragraph.iframe.id}"
        end

        if !url[/(www\.youtube\.com)/].nil?
            # is youtube
            youtubeURL = URI(URI.decode(url)).query
            params = URI::decode_www_form(youtubeURL).to_h
            if !params["image"].nil? && !params["url"].nil?

                fileName = "#{paragraph.name}_#{URI(params["image"]).path.split("/").last}" #21de_default.jpg

                imageURL = params["image"]
                imagePathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), paragraph.postID)
                absolutePath = imagePathPolicy.getAbsolutePath(fileName)
                
                if  ImageDownloader.download(absolutePath, imageURL)
                    relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
                    result = "\n[![YouTube](#{relativePath} \"YouTube\")](#{params["url"]})"
                else
                    result = "\n[YouTube](#{params["url"]})"
                end
            end
        else
            html = Request.html(Request.URL(url))
            src = html.search('script').first.attribute('src')
            result = nil
            if !src.to_s[/^(https\:\/\/gist\.github\.com)/].nil?
                # is gist
                gist = Request.body(Request.URL(src)).scan(/(document\.write\('){1}(.*)(\)){1}/)[1][1]
                gist.gsub! '\n', ''
                gist.gsub! '\"', '"'
                gist.gsub! '<\/', '</'
                gistHTML = Nokogiri::HTML(gist)
                lang = gistHTML.search('table').first['data-tagsearch-lang']
                gistHTML.search('a').each do |a|
                    if a.text == 'view raw'
                        gistRAW = Request.body(Request.URL(a['href']))
                        result = "```#{lang}\n#{gistRAW}\n```"
                    end
                end
            end
        end

        if result.nil?
            "[#{paragraph.iframe.title}](#{url})"
        else
            result
        end
    else
        if !nextParser.nil?
            nextParser.parse(paragraph)
        end
    end
end