<commit_before>require 'spec_helper'

describe Apitizer::Base do
  extend ResourceHelper

  let(:subject_class) { Apitizer::Base }
  let(:address) { 'https://service.com/api' }

  def stub_request(method, address)
    stub_http_request(method, "https://service.com/api/#{ address }").
      to_return(code: '200', body: '{}')
  end

  describe '#new' do
    it 'does not require any arguments' do
      expect { subject_class.new }.not_to raise_error
    end

    it 'draws a routing map when a block is given' do
      scope_name = address
      subject = subject_class.new { scope(scope_name) { resources(:articles) } }
      stub_request(:get, 'articles')
      expect { subject.process(:index, :articles) }.not_to raise_error
    end

    it 'customizes the mapping between the REST actions and HTTP verbs' do
      scope_name = address
      subject = subject_class.new(dictionary: { update: :delete }) do
        scope(scope_name) { resources(:articles) }
      end
      stub = stub_request(:delete, 'articles/xxx')
      subject.process(:update, :articles, 'xxx')
      expect(stub).to have_been_requested
    end
  end

  describe '#process' do
    subject do
      scope_name = address
      subject_class.new { scope(scope_name) { resources(:articles) } }
    end

    restful_collection_actions.each do |action|
      method = rest_http_dictionary[action]

      it "is capable of #{ action } actions" do
        stub_request(method, 'articles')
        expect { subject.process(action, :articles) }.not_to raise_error
      end

      it "is capable of #{ action } actions via alias" do
        stub_request(method, 'articles')
        expect { subject.send(action, :articles) }.not_to raise_error
      end
    end

    restful_member_actions.each do |action|
      method = rest_http_dictionary[action]

      it "is capable of #{ action } actions" do
        stub_request(method, 'articles/xxx')
        expect { subject.process(action, :articles, 'xxx') }.not_to raise_error
      end

      it "is capable of #{ action } actions via alias" do
        stub_request(method, 'articles/xxx')
        expect { subject.send(action, :articles, 'xxx') }.not_to raise_error
      end
    end
  end
end<commit_msg>A bit of refactoring of the Base spec<commit_after>require 'spec_helper'

describe Apitizer::Base do
  extend ResourceHelper

  let(:subject_class) { Apitizer::Base }
  let(:address) { 'https://service.com/api' }

  def stub_request(method, address)
    stub_http_request(method, "https://service.com/api/#{ address }").
      to_return(code: '200', body: '{}')
  end

  describe '#new' do
    it 'does not require any arguments' do
      expect { subject_class.new }.not_to raise_error
    end

    it 'draws a routing map when a block is given' do
      scope_name = address
      subject = subject_class.new { scope(scope_name) { resources(:articles) } }
      stub_request(:get, 'articles')
      expect { subject.process(:index, :articles) }.not_to raise_error
    end

    it 'customizes the mapping between the REST actions and HTTP verbs' do
      scope_name = address
      subject = subject_class.new(dictionary: { update: :delete }) do
        scope(scope_name) { resources(:articles) }
      end
      stub = stub_request(:delete, 'articles/xxx')
      subject.process(:update, :articles, 'xxx')
      expect(stub).to have_been_requested
    end
  end

  describe '#process' do
    subject do
      scope_name = address
      subject_class.new { scope(scope_name) { resources(:articles) } }
    end

    restful_actions.each do |action|
      method = rest_http_dictionary[action]
      steps = [ :articles ]
      steps << 'xxx' if restful_member_actions.include?(action)

      it "is capable of #{ action } actions" do
        stub_request(method, steps.join('/'))
        expect { subject.process(action, *steps) }.not_to raise_error
      end

      it "is capable of #{ action } actions via alias" do
        stub_request(method, steps.join('/'))
        expect { subject.send(action, *steps) }.not_to raise_error
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10928693947561872, 'fraction_numerical': 0.001470227885322225, 'mean_word_length': 3.017716535433071, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '5494682', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1218, 'n_words': 340}