Sha256: 6d883c51592172cfb22e0f5859d1d4522c7565db504dc09e8a0adcd0a311912b

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

module Onebox
  module Engine
    module StandardEmbed

      def raw
        return @raw if @raw
        response = fetch_response(url)

        html_doc = Nokogiri::HTML(response.body)

        # Determine if we should use OEmbed or OpenGraph
        oembed_alternate = html_doc.at("//link[@type='application/json+oembed']") || html_doc.at("//link[@type='text/json+oembed']")
        if oembed_alternate
          # If the oembed request fails, we can still try the opengraph below.
          begin
            @raw = Onebox::Helpers.symbolize_keys(::MultiJson.load(fetch_response(oembed_alternate['href']).body))
          rescue Errno::ECONNREFUSED, Net::HTTPError, MultiJson::LoadError
            @raw = nil
          end
        end

        open_graph = OpenGraph.new(response.body, false)
        if @raw
          @raw[:image] = open_graph.images.first if @raw[:image].nil? && open_graph && open_graph.images

          return @raw
        end

        @raw = open_graph
      end

      private

      def fetch_response(location, limit = 3)
        raise Net::HTTPError.new('HTTP redirect too deep', location) if limit == 0

        uri = URI(location)
        http = Net::HTTP.new(uri.host, uri.port)
        http.open_timeout = Onebox.options.connect_timeout
        http.read_timeout = Onebox.options.timeout
        response = http.request_get(uri.request_uri)

        case response
        when Net::HTTPSuccess     then response
        when Net::HTTPRedirection then fetch_response(response['location'], limit - 1)
        else
          response.error!
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
onebox-1.2.8 lib/onebox/engine/standard_embed.rb
onebox-1.2.7 lib/onebox/engine/standard_embed.rb