<commit_before>require 'spec_helper'

describe Attune::Client do
  let(:client){ described_class.new(options) }
  subject { client }

  context "with defaults" do
    let(:options){ {} }
    specify { expect(subject.endpoint).to eq(Attune::Default::ENDPOINT) }
    specify { expect(subject.middleware).to eq(Attune::Default::MIDDLEWARE) }
  end
  context "with custom endpoint" do
    let(:endpoint){ 'http://example.com/' }
    let(:options){ {endpoint: endpoint} }
    specify { expect(subject.endpoint).to eq(endpoint) }
  end

  let(:options){ {endpoint: 'http://example.com:8080/', middleware: middleware} }
  let(:stubs) { Faraday::Adapter::Test::Stubs.new }
  let(:middleware) do
    Faraday::Builder.new do |builder|
      builder.use ParamFlattener
      builder.adapter :test, stubs
    end
  end

  it "can create_anonymous generating an id" do
    stubs.post("anonymous", %[{"user_agent":"Mozilla/5.0"}]){ [200, {location: 'urn:id:abcd123'}, nil] }
    id = client.create_anonymous(user_agent: 'Mozilla/5.0')
    stubs.verify_stubbed_calls

    expect(id).to eq('abcd123')
  end

  it "can bind" do
    stubs.put("bindings/anonymous=abcd123&customer=foobar"){ [200, {}, nil] }
    client.bind('abcd123', 'foobar')
    stubs.verify_stubbed_calls
  end

  it "can create_anonymous using existing id" do
    stubs.put("anonymous/abcd123", %[{"user_agent":"Mozilla/5.0"}]){ [200, {}, nil] }
    id = client.create_anonymous(id: 'abcd123', user_agent: 'Mozilla/5.0')
    stubs.verify_stubbed_calls

    expect(id).to eq('abcd123')
  end

  it "can get_rankings" do
    stubs.get("rankings/anonymous=abcd123&view=b%2Fmens-pants&entity_collection=products&entities=1001%2C%2C1002%2C%2C1003%2C%2C1004&ip=none"){ [200, {}, %[{"ranking":["1004","1003","1002","1001"]}]] }
    rankings = client.get_rankings(
      id: 'abcd<PASSWORD>',
      view: 'b/mens-pants',
      collection: 'products',
      entities: %w[1001, 1002, 1003, 1004]
    )
    stubs.verify_stubbed_calls

    expect(rankings).to eq(["1004", "1003", "1002", "1001"])
  end
end<commit_msg>style: string array shortcut syntax<commit_after>require 'spec_helper'

describe Attune::Client do
  let(:client){ described_class.new(options) }
  subject { client }

  context "with defaults" do
    let(:options){ {} }
    specify { expect(subject.endpoint).to eq(Attune::Default::ENDPOINT) }
    specify { expect(subject.middleware).to eq(Attune::Default::MIDDLEWARE) }
  end
  context "with custom endpoint" do
    let(:endpoint){ 'http://example.com/' }
    let(:options){ {endpoint: endpoint} }
    specify { expect(subject.endpoint).to eq(endpoint) }
  end

  let(:options){ {endpoint: 'http://example.com:8080/', middleware: middleware} }
  let(:stubs) { Faraday::Adapter::Test::Stubs.new }
  let(:middleware) do
    Faraday::Builder.new do |builder|
      builder.use ParamFlattener
      builder.adapter :test, stubs
    end
  end

  it "can create_anonymous generating an id" do
    stubs.post("anonymous", %[{"user_agent":"Mozilla/5.0"}]){ [200, {location: 'urn:id:abcd123'}, nil] }
    id = client.create_anonymous(user_agent: 'Mozilla/5.0')
    stubs.verify_stubbed_calls

    expect(id).to eq('abcd123')
  end

  it "can bind" do
    stubs.put("bindings/anonymous=abcd123&customer=foobar"){ [200, {}, nil] }
    client.bind('abcd123', 'foobar')
    stubs.verify_stubbed_calls
  end

  it "can create_anonymous using existing id" do
    stubs.put("anonymous/abcd123", %[{"user_agent":"Mozilla/5.0"}]){ [200, {}, nil] }
    id = client.create_anonymous(id: 'abcd123', user_agent: 'Mozilla/5.0')
    stubs.verify_stubbed_calls

    expect(id).to eq('abcd123')
  end

  it "can get_rankings" do
    stubs.get("rankings/anonymous=abcd123&view=b%2Fmens-pants&entity_collection=products&entities=1001%2C%2C1002%2C%2C1003%2C%2C1004&ip=none"){ [200, {}, %[{"ranking":["1004","1003","1002","1001"]}]] }
    rankings = client.get_rankings(
      id: 'abcd123',
      view: 'b/mens-pants',
      collection: 'products',
      entities: %w[1001, 1002, 1003, 1004]
    )
    stubs.verify_stubbed_calls

    expect(rankings).to eq(%W[1004 1003 1002 1001])
  end
end
{'fraction_non_alphanumeric': 0.16707080504364694, 'fraction_numerical': 0.05843840931134821, 'mean_word_length': 4.32258064516129, 'pattern_counts': {'":': 6, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2854091', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1509, 'n_words': 269}