Sha256: 7eb6929733f34af36f8fc6f0ae23b7cb0c8466e714b8ab480f240532bfb3a749

Contents?: true

Size: 1.9 KB

Versions: 5

Compression:

Stored size: 1.9 KB

Contents

require 'rails_helper'

describe LHS::Record do
  before(:each) do
    class Business < LHS::Record
      configuration(
        items_key: [:response, :businesses],
        total_key: [:response, :count],
        limit_key: { body: [:response, :max] },
        pagination_key: { body: [:response, :offset] },
        pagination_strategy: :offset
      )
      endpoint 'http://uberall/businesses'
    end
  end

  let(:stub_single_business_request) do
    stub_request(:get, "http://uberall/businesses?identifier=ABC123&limit=1")
      .to_return(body: {
        status: "SUCCESS",
        response: {
          offset: 0,
          max: 50,
          count: 1,
          businesses: [
            {
              identifier: 'ABC123',
              name: 'localsearch'
            }
          ]
        }
      }.to_json)
  end

  let(:stub_multiple_businesses_request) do
    stub_request(:get, "http://uberall/businesses?name=localsearch")
      .to_return(body: {
        status: "SUCCESS",
        response: {
          offset: 0,
          max: 50,
          count: 2,
          businesses: [
            {
              identifier: 'ABC123',
              name: 'localsearch'
            },
            {
              identifier: 'ABC121',
              name: 'Swisscom'
            }
          ]
        }
      }.to_json)
  end

  context 'access nested keys for configuration' do
    it 'uses paths from configuration to access nested values' do
      stub_single_business_request
      business = Business.find_by(identifier: 'ABC123')
      expect(business.identifier).to eq 'ABC123'
      expect(business.name).to eq 'localsearch'
    end

    it 'digs for meta data when meta information is nested' do
      stub_multiple_businesses_request
      businesses = Business.where(name: 'localsearch')
      expect(businesses.length).to eq 2
      expect(businesses.count).to eq 2
      expect(businesses.offset).to eq 0
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lhs-15.3.1 spec/record/dig_configuration_spec.rb
lhs-15.3.1.pre.fixlhc.1 spec/record/dig_configuration_spec.rb
lhs-15.3.0 spec/record/dig_configuration_spec.rb
lhs-15.2.5 spec/record/dig_configuration_spec.rb
lhs-15.2.4 spec/record/dig_configuration_spec.rb