bearcat 1.3.55 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2168e1b83a6cea7ae22cd280352d3c5865aae3811800c789cd122b6240e05ebe
4
- data.tar.gz: bbd67cb26a64ba4d80f5b69a1d51514a7497b28b5cce86fe58a9a49ad4595857
3
+ metadata.gz: f18014e61dafe829eb0820bff31d40b886d178ebd4d67241bef32ffe1a550581
4
+ data.tar.gz: 8908fb0abf898a853546fdeac9211695378487f28b2508deefd5f19923cb297f
5
5
  SHA512:
6
- metadata.gz: e2f98fb58293a2151fc7920ec482f2527080c0f36e36ddb18e12b828cfbc09e002899d4cc341f55b4fdf323485b89a07988229c52bcde7f0257694a2bbaf7a30
7
- data.tar.gz: ee7d9d0fd7cc50ae7d8ca073e2f201cb867c72b077db922ef3607d6e1b90b541c4dda1894f3acae31ac8a1bedbdd36cf0c8cdfeb457a22bdf21a280cb74b9368
6
+ metadata.gz: c618cfc8ce6dc51b976f4f08bee4e0186dd0d0148dcf39245ca276602ca7260165f5eace1e2be041899a33235847564645b004b5d0a2ef742fa06603ea4b09e8
7
+ data.tar.gz: eee162c665db7d9edf5f071ecdd3821a26e080cc58adf0b00a4efc7022cc2b885a78803579ea5f219b5bbd416575aef774deab387d9f62e9f7bd77f69d7d3d91
@@ -2,7 +2,7 @@ module Bearcat
2
2
  class ApiArray
3
3
  include Enumerable
4
4
 
5
- attr_reader :status, :headers, :members
5
+ attr_reader :raw_response, :members
6
6
 
7
7
  def self.process_response(response, api_client)
8
8
  if response.body.is_a?(Array)
@@ -26,18 +26,23 @@ module Bearcat
26
26
 
27
27
  def initialize(response, api_client, array_key = nil)
28
28
  @api_client = api_client
29
+ @raw_response = response
29
30
  @array_key = array_key
30
- @page_count = 100
31
+ @page_count = nil
32
+
31
33
  case response.status
32
34
  when 200..206
33
35
  @members = process_body(response)
34
- @status = response.status
35
- @headers = response.headers
36
- @method = response.env[:method]
37
- init_pages(@headers['Link'])
36
+ init_pages(headers[:link])
38
37
  end
39
38
  end
40
39
 
40
+ delegate :status, :headers, to: :raw_response
41
+
42
+ def method
43
+ raw_response.env[:method]
44
+ end
45
+
41
46
  def [](i)
42
47
  @members[i]
43
48
  end
@@ -54,71 +59,37 @@ module Bearcat
54
59
  @link_hash['next'] || @link_hash['prev']
55
60
  end
56
61
 
57
- def next_page
58
- load_page('next')
59
- end
60
-
61
- def prev_page
62
- load_page('prev')
63
- end
64
-
65
- def first_page
66
- load_page('first')
67
- end
68
-
69
- def last_page
70
- load_page('last')
62
+ %w[next prev first last].each do |rel|
63
+ define_method :"#{rel}_page" do
64
+ load_page(rel)
65
+ end
71
66
  end
72
67
 
73
- def each_page(page_count = 50, &block)
74
- @page_count = page_count
68
+ def each_page(page_count = nil, &block)
75
69
  if pages?
76
- response = get_page(@link_hash['first'])
77
- @headers = response.headers
78
- @status = response.status
79
- @method = response.env[:method]
80
- init_pages(@headers[:link])
81
- @members = process_body(response)
82
- block.call(@members)
83
- while @link_hash['next'] && @members.present?
84
- response = get_page(@link_hash['next'])
85
- @headers = response.headers
86
- @status = response.status
87
- @method = response.env[:method]
70
+ iterate_pages(page_count) do |page_response|
88
71
  @members = process_body(response)
89
- init_pages(@headers[:link])
72
+ break unless @members.present?
90
73
  block.call(@members)
91
74
  end
92
- @link_hash = {}
93
75
  else
94
76
  block.call(@members)
95
77
  end
96
78
  end
97
79
 
98
- def all_pages_each(page_count = 50, &block)
80
+ def all_pages_each(page_count = nil, &block)
99
81
  each_page(page_count) do |page|
100
82
  page.each &block
101
83
  end
102
84
  end
103
85
 
104
- def all_pages!(page_count = 50)
86
+ def all_pages!(page_count = nil)
105
87
  if pages?
106
- @page_count = page_count
107
- response = get_page(@link_hash['first'])
108
- @headers = response.headers
109
- @status = response.status
110
- @method = response.env[:method]
111
- init_pages(@headers[:link])
112
- @members = process_body(response)
113
- while @link_hash['next']
114
- response = get_page(@link_hash['next'])
115
- @headers = response.headers
116
- @status = response.status
117
- @method = response.env[:method]
118
- page_members = process_body(response)
88
+ @members = []
89
+ iterate_pages(page_count) do |page_response|
90
+ page_members = process_body(page_response)
119
91
  break unless page_members.present?
120
92
  @members.concat(page_members)
121
- init_pages(@headers[:link])
122
93
  end
123
94
  @link_hash = {}
124
95
  end
@@ -127,9 +98,10 @@ module Bearcat
127
98
 
128
99
  private
129
100
 
130
- def init_pages(link_header)
101
+ def init_pages(link_header = headers[:link])
131
102
  @link_hash = {}
132
- if @headers.has_key? 'Link'
103
+ if headers.has_key? 'Link'
104
+
133
105
  links = link_header.split(/,\s?/)
134
106
 
135
107
  links.each do |link|
@@ -143,14 +115,11 @@ module Bearcat
143
115
 
144
116
  def get_page(url, params = {})
145
117
  params['per_page'] = @page_count unless params.key?('per_page') || !@page_count
146
- query = URI.parse(url).query
147
- p = CGI.parse(query)
148
- u = url.gsub("?#{query}", '')
149
118
 
150
- # strip value out of array if value is an array and key doesn't have [] (parameter is not an array parameter)
151
- p.each { |k, v| p[k] = v.first if v.is_a?(Array) && k !~ /\[\]$/ }
152
- # remove [] from key names, this is copied from rails' {}.transform_keys!
153
- p.keys.each { |k| p[k.delete('[]')] = p.delete(k) }
119
+ parsed_url = URI.parse(url)
120
+ p = parse_url_params(parsed_url)
121
+ u = url.gsub("?#{parsed_url.query}", '')
122
+
154
123
  # merge params
155
124
  p.merge!(params)
156
125
 
@@ -166,6 +135,44 @@ module Bearcat
166
135
  end
167
136
  end
168
137
 
138
+ def iterate_pages(per_page = @page_count)
139
+ return to_enum(:iterate_pages, per_page) unless block_given?
140
+
141
+ if per_page.present? && per_page != per_page_count && @link_hash['first']
142
+ @page_count = per_page
143
+ @raw_response = response = get_page(@link_hash['first'])
144
+ yield response
145
+ init_pages(headers[:link])
146
+ else
147
+ yield @raw_response
148
+ end
149
+
150
+ while @link_hash['next']
151
+ @raw_response = response = get_page(@link_hash['next'])
152
+ yield response
153
+ init_pages(headers[:link])
154
+ end
155
+ end
156
+
157
+ def per_page_count
158
+ url = raw_response.env[:url]
159
+ query_params = parse_url_params(url)
160
+ query_params[:per_page]&.to_i
161
+ end
162
+
163
+ def parse_url_params(url)
164
+ url = URI.parse(url) if url.is_a?(String)
165
+ p = CGI.parse(url.query || '')
166
+ p.default = nil
167
+
168
+ # strip value out of array if value is an array and key doesn't have [] (parameter is not an array parameter)
169
+ p.each { |k, v| p[k] = v.first if v.is_a?(Array) && k !~ /\[\]$/ }
170
+ # remove [] from key names, this is copied from rails' {}.transform_keys!
171
+ p.keys.each { |k| p[k.delete('[]')] = p.delete(k) }
172
+
173
+ p.with_indifferent_access
174
+ end
175
+
169
176
  #TODO: This is a quick fix for JSONAPI responses if we need to do this for anything else we need to do this a better way
170
177
  def self.array_key(response)
171
178
  key = nil
@@ -1,3 +1,3 @@
1
1
  module Bearcat
2
- VERSION = '1.3.55' unless defined?(Bearcat::VERSION)
2
+ VERSION = '1.4.0' unless defined?(Bearcat::VERSION)
3
3
  end
@@ -2,8 +2,17 @@ require 'helper'
2
2
 
3
3
  describe Bearcat::ApiArray do
4
4
  let(:api_client) { double }
5
+ let(:response_headers) { { } }
6
+ let(:response_env) { { url: "https://fake.com" } }
5
7
  let(:response_body) { @response_body || [] }
6
- let(:original_response) { double(body: response_body, status: 200, headers: {}, env: {}) }
8
+ let(:original_response) do
9
+ double(
10
+ body: response_body,
11
+ status: 200,
12
+ headers: Faraday::Utils::Headers.new(response_headers),
13
+ env: Faraday::Env.from(response_env)
14
+ )
15
+ end
7
16
 
8
17
  describe 'get_page' do
9
18
  let(:api_array) { described_class.process_response(original_response, api_client) }
@@ -55,6 +64,28 @@ describe Bearcat::ApiArray do
55
64
  end
56
65
  end
57
66
 
67
+ describe '#iterate_pages' do
68
+ let(:api_array) { described_class.process_response(original_response, api_client) }
69
+
70
+ let(:original_response) do
71
+ super().tap do |s|
72
+ s.headers['Link'] = [
73
+ '<https://fake.com?page=1>; rel="first"'
74
+ ].join(', ')
75
+ end
76
+ end
77
+
78
+ it 're-requests the first-page if per_page has changed' do
79
+ expect(api_array).to receive(:get_page).and_return(original_response)
80
+ api_array.send(:iterate_pages, 10).to_a
81
+ end
82
+
83
+ it 'yields existing first-page if per_page is unchanged' do
84
+ expect(api_array).not_to receive(:get_page)
85
+ api_array.send(:iterate_pages, nil).to_a
86
+ end
87
+ end
88
+
58
89
  context 'makes returned values indifferent' do
59
90
  it 'makes a Hash response indifferent' do
60
91
  @response_body = { something: 1 }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bearcat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.55
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Mills, Jake Sorce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-19 00:00:00.000000000 Z
11
+ date: 2020-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake