<commit_before>require 'json'
require File.dirname(__FILE__) + '/spec_helper'
require 'vcr'

VCR.config do |c|
  c.default_cassette_options = { :record => :new_episodes }
  c.cassette_library_dir = 'spec/cassettes'
  c.stub_with :fakeweb
end

describe OEmbed::ProviderDiscovery do
  before(:all) do
    VCR.insert_cassette('OEmbed_ProviderDiscovery')
  end
  after(:all) do
    VCR.eject_cassette
  end

  include OEmbedSpecHelper

  {
    # 'name' => [
    #   'given_page_url',
    #   'expected_endpoint' || {:json=>'expected_json_endpoint', :xml=>'expected_xml_endpoint},
    #   :expected_format,
    # ]
    'youtube' => [
      'http://www.youtube.com/watch?v=u6XAPnuFjJc',
      'http://www.youtube.com/oembed',
      :json,
    ],
    'vimeo' => [
      'http://vimeo.com/27953845',
      {:json=>'http://vimeo.com/api/oembed.json',:xml=>'http://vimeo.com/api/oembed.xml'},
      :json,
    ],
    'facebook-photo' => [
      'https://www.facebook.com/Federer/photos/pb.64760994940.-2207520000.1456668968./10153235368269941/?type=3&theater',
      'https://www.facebook.com/plugins/post/oembed.json/',
      :json,
    ],
    'tumblr' => [
      'http://kittehkats.tumblr.com/post/140525169406/katydid-and-the-egg-happy-forest-family',
      'https://www.tumblr.com/oembed/1.0',
      :json
    ],
    #'noteflight' => [
    #  'http://www.noteflight.com/scores/view/09665392c94475f65dfaf5f30aadb6ed0921939d',
    #  'http://www.noteflight.com/services/oembed',
    #  :json,
    #],
    #'wordpress' => [
    #  'http://sweetandweak.wordpress.com/2011/09/23/nothing-starts-the-morning-like-a-good-dose-of-panic/',
    #  'http://public-api.wordpress.com/oembed/1.0/',
    #  :json,
    #],
  }.each do |context, urls|

    given_url, expected_endpoints, expected_format = urls
    expected_endpoints = {expected_format=>expected_endpoints} unless expected_endpoints.is_a?(Hash)

    context "given a #{context} url" do

      shared_examples "a discover_provider call" do |endpoint, format|
        describe ".discover_provider" do
          it "should return the correct Class" do
            expect(provider).to be_instance_of(OEmbed::Provider)
          end

          it "should detect the correct URL" do
            expect(provider.endpoint).to eq(endpoint)
          end

          it "should return the correct format" do
            expect(provider.format).to eq(format)
          end
        end

        describe ".get" do
          it "should return the correct Class" do
            expect(response).to be_kind_of(OEmbed::Response)
          end

          it "should return the correct format" do
            expect(response.format).to eq(format.to_s)
          end

          it "should return the correct data" do
            expect(response.type).to_not be_nil

            # Technically, the following values _could_ be blank, but for the
            # examples urls we're using we expect them not to be.
            expect(response.title).to_not be_nil
          end
        end # get
      end

      context "with no format specified" do
        let(:provider) { OEmbed::ProviderDiscovery.discover_provider(given_url) }
        let(:response) { OEmbed::ProviderDiscovery.get(given_url) }
        include_examples "a discover_provider call", expected_endpoints[expected_format], expected_format
      end

      if expected_endpoints.include?(:json)
        context "with json format specified" do
          let(:provider) { OEmbed::ProviderDiscovery.discover_provider(given_url, :format=>:json) }
          let(:response) { OEmbed::ProviderDiscovery.get(given_url, :format=>:json) }
          include_examples "a discover_provider call", expected_endpoints[:json], :json<commit_msg>ProviderDiscovery#get spec only expects required data<commit_after>require File.dirname(__FILE__) + '/spec_helper'
require 'json'
require 'vcr'

VCR.config do |c|
  c.default_cassette_options = { :record => :new_episodes }
  c.cassette_library_dir = 'spec/cassettes'
  c.stub_with :fakeweb
end

describe OEmbed::ProviderDiscovery do
  before(:all) do
    OEmbed::Formatter::JSON.backend = 'JSONGem'
    VCR.insert_cassette('OEmbed_ProviderDiscovery')
  end
  after(:all) do
    VCR.eject_cassette
  end

  include OEmbedSpecHelper

  {
    # 'name' => [
    #   'given_page_url',
    #   'expected_endpoint' || {:json=>'expected_json_endpoint', :xml=>'expected_xml_endpoint},
    #   :expected_format,
    # ]
    'youtube' => [
      'http://www.youtube.com/watch?v=u6XAPnuFjJc',
      'http://www.youtube.com/oembed',
      :json,
    ],
    'vimeo' => [
      'http://vimeo.com/27953845',
      {:json=>'http://vimeo.com/api/oembed.json',:xml=>'http://vimeo.com/api/oembed.xml'},
      :json,
    ],
    'facebook-photo' => [
      'https://www.facebook.com/Federer/photos/pb.64760994940.-2207520000.1456668968./10153235368269941/?type=3&theater',
      'https://www.facebook.com/plugins/post/oembed.json/',
      :json,
    ],
    'tumblr' => [
      'http://kittehkats.tumblr.com/post/140525169406/katydid-and-the-egg-happy-forest-family',
      'https://www.tumblr.com/oembed/1.0',
      :json
    ],
    #'noteflight' => [
    #  'http://www.noteflight.com/scores/view/09665392c94475f65dfaf5f30aadb6ed0921939d',
    #  'http://www.noteflight.com/services/oembed',
    #  :json,
    #],
    #'wordpress' => [
    #  'http://sweetandweak.wordpress.com/2011/09/23/nothing-starts-the-morning-like-a-good-dose-of-panic/',
    #  'http://public-api.wordpress.com/oembed/1.0/',
    #  :json,
    #],
  }.each do |context, urls|

    given_url, expected_endpoints, expected_format = urls
    expected_endpoints = {expected_format=>expected_endpoints} unless expected_endpoints.is_a?(Hash)

    context "given a #{context} url" do

      shared_examples "a discover_provider call" do |endpoint, format|
        describe ".discover_provider" do
          it "should return the correct Class" do
            expect(provider).to be_instance_of(OEmbed::Provider)
          end

          it "should detect the correct URL" do
            expect(provider.endpoint).to eq(endpoint)
          end

          it "should return the correct format" do
            expect(provider.format).to eq(format)
          end
        end

        describe ".get" do
          it "should return the correct Class" do
            expect(response).to be_kind_of(OEmbed::Response)
          end

          it "should return the correct format" do
            expect(response.format).to eq(format.to_s)
          end

          it "should return the correct data" do
            expect(response.type).to_not be_empty

            case response.type
            when 'video', 'rich'
              expect(response.html).to_not be_empty
              expect(response.width).to_not be_nil
              expect(response.height).to_not be_nil
            when 'photo'
              expect(response.url).to_not be_empty
              expect(response.width).to_not be_nil
              expect(response.height).to_not be_nil
            end
          end
        end # get
      end

      context "with no format specified" do
        let(:provider) { OEmbed::ProviderDiscovery.discover_provider(given_url) }
        let(:response) { OEmbed::ProviderDiscovery.get(given_url) }
        include_examples "a discover_provider call", expected_endpoints[expected_format], expected_format
      end

      if expected_endpoints.include?(:json)
        context "with json format specified" do
          let(:provider) { OEmbed::ProviderDiscovery.discover_provider(given_url, :format=>:json) }
          let(:response) { OEmbed::ProviderDiscovery.get(given_url, :format=>:json) }
          include_examples "a discover_provider call", expected_endpoints[:json], :json
{'fraction_non_alphanumeric': 0.12911981388134935, 'fraction_numerical': 0.02791779759596743, 'mean_word_length': 2.684761904761905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 6, 'lorem ipsum': 0, 'www.': 14, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6178349', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2499, 'n_words': 434}