summaryrefslogtreecommitdiff
path: root/lib/bulk_imports/common/extractors/rest_extractor.rb
blob: 883153052222d4f14d1082486e28346b54bb63d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

module BulkImports
  module Common
    module Extractors
      class RestExtractor
        def initialize(options = {})
          @query = options[:query]
        end

        def extract(context)
          client = http_client(context.configuration)
          params = query.to_h(context)
          response = client.get(params[:resource], params[:query])

          BulkImports::Pipeline::ExtractedData.new(
            data: response.parsed_response,
            page_info: page_info(response.headers)
          )
        end

        private

        attr_reader :query

        def http_client(configuration)
          @http_client ||= BulkImports::Clients::HTTP.new(
            url: configuration.url,
            token: configuration.access_token,
            per_page: 100
          )
        end

        def page_info(headers)
          next_page = headers['x-next-page']

          {
            'has_next_page' => next_page.present?,
            'next_page' => next_page
          }
        end
      end
    end
  end
end