Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
Improve handling of extra SearchResults data
Browse files Browse the repository at this point in the history
The Brightpearl API returns extra data on searches that might be useful, so we should capture it and keep it in every SearchResults instance.
  • Loading branch information
allolex committed Apr 24, 2014
1 parent 60318fe commit 56f18ea
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 47 deletions.
61 changes: 37 additions & 24 deletions lib/nacre/search_results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ class SearchResults
extend Inflectible
include Enumerable

attr_accessor :results
attr_accessor :results, :last_result, :first_result,
:total_results, :returned_results, :columns

def initialize(params_list = [])
@results = []
params_list.each do |params|
@results << params
def initialize(params = {})
unless params.empty?
extract_metadata(params[:response][:meta_data])
extract_results(params[:response][:results])
end
end

def self.from_json(results_json)
raise SearchResultsError.new('Empty JSON') unless results_json.length > 2
list = result_params_list(results_json)
new(list)
def self.from_json(json)
raise SearchResultsError.new('Empty JSON') unless json.length > 2
params = format_hash_keys(JSON.parse(json, symbolize_names: true))
new(params)
end

def each(&block)
Expand All @@ -30,31 +31,43 @@ def each(&block)
end
end

def params
Hash[results]
end

private

def self.result_params_list(results)
keys = keys_from_metadata(results)
list = resources_from_results(results)
list.map { |resource| convert_to_params(keys, resource) }
def extract_results(params)
self.results = []
formatted_params = result_params_list(params)
formatted_params.each do |result|
results << result
end
end

def extract_metadata(metadata)
self.columns = extract_column_properties(metadata[:columns])
self.last_result = metadata[:last_result]
self.first_result = metadata[:first_result]
self.total_results = metadata[:results_available]
self.returned_results = metadata[:results_returned]
end

def self.resources_from_results(results)
hash = JSON.parse(results)
hash['response']['results']
def extract_column_properties(params)
params.map { |col_params| ColumnProperty.new(col_params) }
end

def self.convert_to_params(keys, values)
def result_params_list(results)
keys = columns.map(&:formatted_name)
results.map { |resource| convert_to_params(keys, resource) }
end

def convert_to_params(keys, values)
params = {}
keys.each_with_index do |key, index|
params[snake_case(key).to_sym] = values[index].to_s
params[key] = values[index].to_s
end
params
end

def self.keys_from_metadata(results)
hash = JSON.parse(results)
columns = hash['response']['metaData']['columns']
columns.map { |c| c['name'] }
end
end
end
194 changes: 171 additions & 23 deletions spec/lib/nacre/search_results_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,160 @@

describe Nacre::SearchResults do

describe 'initialization' do
let(:params) do
{
reference: {
order_status_names: {
'4' => 'Invoiced'
},
order_stock_status_names: {
'3' => 'All fulfilled'
},
order_type_names: {
'1' => 'SALES_ORDER'
}
},
response: {
meta_data: {
sorting: [
{
direction: 'ASC',
filterable: {
required: false,
report_data_type: 'INTEGER',
name: 'orderId',
sortable: true,
filterable: true
}
}
],
columns: [
{
required: false,
report_data_type: 'INTEGER',
name: 'orderId',
sortable: true,
filterable: true
},
{
reference_data: [
'orderTypeNames'
],
required: false,
report_data_type: 'INTEGER',
name: 'orderTypeId',
sortable: true,
filterable: true
},
{
required: false,
report_data_type: 'INTEGER',
name: 'contactId',
sortable: true,
filterable: true
},
{
reference_data: [
'orderStatusNames'
],
required: false,
report_data_type: 'INTEGER',
name: 'orderStatusId',
sortable: true,
filterable: true
},
{
reference_data: [
'orderStockStatusNames'
],
required: false,
report_data_type: 'INTEGER',
name: 'orderStockStatusId',
sortable: true,
filterable: true
},
{
required: false,
report_data_type: 'PERIOD',
name: 'createdOn',
sortable: true,
filterable: true
},
{
required: false,
report_data_type: 'INTEGER',
name: 'createdById',
sortable: true,
filterable: true
}
],
last_result: 3,
first_result: 1,
results_returned: 3,
results_available: 3
},
results: [
[
123456,
1,
253,
4,
3,
'2012-12-13T13:00:42.000Z',
280
],
[
123457,
1,
253,
4,
3,
'2012-12-14T13:00:42.000Z',
280
],
[
123458,
1,
253,
4,
3,
'2012-12-14T14:00:42.000Z',
280
]
]
}
}
end

let(:results_hash) { fixture_file_content('order_search_result.json') }
subject { Nacre::SearchResults.new(params) }

context 'with valid JSON data' do
let(:results) do
Nacre::SearchResults.new(JSON.parse(results_hash)['response'])
end
describe 'initialization' do

it_should_behave_like 'Enumerable' do
let(:subject) { results }
end
context 'with parameters' do
it_should_behave_like 'Enumerable'

it 'should return an SearchResults instance' do
expect(results).to be_a(Nacre::SearchResults)
expect(subject).to be_a(Nacre::SearchResults)
end
end

context 'with invalid data' do
it 'should raise an error' do
expect { Nacre::SearchResults.from_json('') }
.to raise_error(Nacre::SearchResultsError)
context 'with empty parameters' do
subject { Nacre::SearchResults.new }

it 'should return an SearchResults instance' do
expect(subject).to be_a(Nacre::SearchResults)
end
end
end

describe '.from_json' do

let(:results_hash) { fixture_file_content('order_search_result.json') }

context 'with valid JSON data' do
let(:results) { Nacre::SearchResults.from_json(results_hash) }
let(:json) { fixture_file_content('order_search_result.json') }

subject { Nacre::SearchResults.from_json(json) }

it 'should return an SearchResults instance' do
expect(results).to be_a(Nacre::SearchResults)
expect(subject).to be_a(Nacre::SearchResults)
end
end

Expand All @@ -51,12 +170,41 @@
describe '#results' do
let(:json) { fixture_file_content('order_search_result.json') }

let(:search) { Nacre::SearchResults.from_json(json) }
subject { Nacre::SearchResults.from_json(json) }

it 'should return a list of order params' do
expect(search.results).to respond_to(:each)
expect(search.results.first).to be_a(Hash)
expect(search.results.first[:order_id]).to eql('123456')
expect(subject.results.first).to be_a(Hash)
expect(subject.results.first[:order_id]).to eql('123456')
end
end

describe '#columns' do
it 'should return a list of column properties' do
expect(subject.columns.first).to be_a(Nacre::ColumnProperty)
end
end

describe '#total_results' do
it 'should be the correct value' do
expect(subject.total_results).to eq(3)
end
end

describe '#returned_results' do
it 'should be the correct value' do
expect(subject.returned_results).to eq(3)
end
end

describe '#first_result' do
it 'should be the correct value' do
expect(subject.first_result).to eq(1)
end
end

describe '#last_result' do
it 'should be the correct value' do
expect(subject.last_result).to eq(3)
end
end
end

0 comments on commit 56f18ea

Please sign in to comment.