Skip to content
This repository was archived by the owner on Mar 17, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2da59cd
Extracting the dataset name before converting the response
bogdanadrianmarc Oct 27, 2021
35c5298
Figure out the json mode from the query
bogdanadrianmarc Oct 27, 2021
2515e71
Added a flag for json compact mode to the response converter
bogdanadrianmarc Oct 27, 2021
b9230de
Returning different responses based on the json mode
bogdanadrianmarc Oct 27, 2021
83c4428
Added response converter for json compact mode
bogdanadrianmarc Oct 27, 2021
9f34a88
JSON complete mode response converter added
bogdanadrianmarc Oct 27, 2021
96d9dc0
Added dataset name as config param
bogdanadrianmarc Oct 28, 2021
06c1ee3
Using query terms Hash instead of parsing JSON string
bogdanadrianmarc Oct 28, 2021
f7d4a6a
Changed default port for testing the API connection
bogdanadrianmarc Oct 28, 2021
ba5d7aa
Mimic the QueryGenerator class for the query object
bogdanadrianmarc Oct 28, 2021
62adf1e
Updated VCR cassette
bogdanadrianmarc Oct 28, 2021
06ff0de
Separated PPD response converter tests
bogdanadrianmarc Oct 28, 2021
7600200
UKHPI response converter tests added
bogdanadrianmarc Oct 28, 2021
de8866e
App version bump
bogdanadrianmarc Oct 28, 2021
075a8c8
Added changelog entry
bogdanadrianmarc Oct 28, 2021
920c40c
Fix readme markdown errors
bogdanadrianmarc Nov 1, 2021
a704340
Added instructions to run the tests to the readme
bogdanadrianmarc Nov 1, 2021
65ced53
Keyword argument to positional argument
bogdanadrianmarc Nov 1, 2021
2386718
Get API url for tests from env var
bogdanadrianmarc Nov 1, 2021
795dc19
Updated instructions for running the tests
bogdanadrianmarc Nov 1, 2021
28d7824
Small refactor
bogdanadrianmarc Nov 1, 2021
63a72e0
Fixed wrong namespace for rubocop rule
bogdanadrianmarc Nov 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Metrics/LineLength:
Layout/LineLength:
Max: 100
Exclude:
- spec/**/*
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for DS API rubygem

## 1.1.0 - 2021-10-28 (Bogdan)

- Added support for `@json_mode: "complete"` query parameter

## 1.0.0 - 2021-06-14 (Bogdan)

- Added a DSAPI to SapiNT converter, which converts all DSAPI queries
Expand Down
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@ This gem provides a Ruby API for the [data services API](https://github.com/epim

Add this line to your application's Gemfile:

gem 'data-api'
```ruby
gem 'data-api'
```

And then execute:

$ bundle
```sh
bundle
```

Also, an API URL needs to be provided for the Service class in order for the gem to work
Also, an API URL needs to be provided for the Service class in order for the gem
Comment thread
bogdanadrianmarc marked this conversation as resolved.
to work.

## Tests

To run the tests you must set the environment variable `API_URL` to point to a
running instance of a valid SAPINT API and run:

```sh
rake test
```

If `API_URL` is not set it will default to `http://localhost:8888`

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion lib/data_services_api/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def data_api
@json['data-api']
end

def dataset
@json['dataset']
end

def structure_api
@json['structure-api']
end
Expand All @@ -51,7 +55,8 @@ def structure
def query(query)
sapi_query_params = SapiNTConverter.new(query.to_json).to_sapint_query
sapint_response = service.api_get_json(data_api, sapi_query_params)
DSAPIResponseConverter.new(sapint_response, data_api).to_dsapi_response
json_mode_compact = query.terms['@json_mode'] == 'compact'
DSAPIResponseConverter.new(sapint_response, dataset, json_mode_compact).to_dsapi_response
end

def describe(uri)
Expand Down
30 changes: 25 additions & 5 deletions lib/data_services_api/dsapi_response_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

module DataServicesApi
class DSAPIResponseConverter
def initialize(sapint_response, data_api)
def initialize(sapint_response, dataset_name, json_mode_compact = false)
@sapint_response = sapint_response
@data_api = data_api
@dataset_name = dataset_name
@json_mode_compact = json_mode_compact
end

# Converts SAPINT returned JSON format to DSAPI returned JSON format
Expand All @@ -22,15 +23,34 @@ def to_dsapi_item(sapint_item)
end
end

# Return different response formats based on the set JSON mode
def to_dsapi_json(sapint_key, sapint_value)
dataset_name = @data_api[/[a-z]*$/]
return json_mode_compact(sapint_key, sapint_value) if @json_mode_compact
Comment thread
bogdanadrianmarc marked this conversation as resolved.

json_mode_complete(sapint_key, sapint_value)
end

def json_mode_compact(sapint_key, sapint_value)
return { sapint_key => sapint_value } if sapint_key == '@id'
return { "#{dataset_name}:#{sapint_key}" => sapint_value } unless sapint_value.is_a?(Hash)
return { "#{@dataset_name}:#{sapint_key}" => sapint_value } unless sapint_value.is_a?(Hash)

sapint_value.map do |key, value|
["#{dataset_name}:#{sapint_key}#{key == '@id' ? '' : key.capitalize}", value]
["#{@dataset_name}:#{sapint_key}#{key == '@id' ? '' : key.capitalize}", value]
end.to_h
end

def json_mode_complete(sapint_key, sapint_value)
case sapint_key
when '@id'
return { sapint_key => sapint_value }
when 'refMonth'
Comment thread
bogdanadrianmarc marked this conversation as resolved.
return { "#{@dataset_name}:#{sapint_key}" => { '@value' => sapint_value } }
when 'refPeriodStart'
return { "#{@dataset_name}:#{sapint_key}" => [{ '@value' => sapint_value }] }
end
return { "#{@dataset_name}:#{sapint_key}" => [sapint_value] } unless sapint_value.is_a?(Hash)

{ "#{@dataset_name}:#{sapint_key}" => sapint_value }
end
end
end
3 changes: 2 additions & 1 deletion lib/data_services_api/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def dataset(name)
raise 'Dataset name is required' unless name

endpoint = {
'data-api' => "#{@url}/landregistry/id/#{name}"
'data-api' => "#{@url}/landregistry/id/#{name}",
'dataset' => name
}
Dataset.new(endpoint, self)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/data_services_api/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

# :nodoc:
module DataServicesApi
VERSION = '1.0.0'
VERSION = '1.1.0'
MAJOR, MINOR, PATCH = VERSION.split('.')
end
14 changes: 12 additions & 2 deletions spec/data_services_api/dataset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
describe 'DataServiceApi::Dataset' do
before do
VCR.insert_cassette name, record: :new_episodes
@dataset = DataServicesApi::Service.new(url: 'http://localhost:8080').dataset('ukhpi')
api_url = ENV['API_URL'] || 'http://localhost:8888'
@dataset = DataServicesApi::Service.new(url: api_url).dataset('ukhpi')
end

after do
Expand All @@ -18,7 +19,16 @@
end

it 'should accept a query and return the result' do
query = { 'ukhpi:refRegion' => { '@eq' => { '@id' => 'http://landregistry.data.gov.uk/id/region/somerset' } } }
query = Class.new do
def terms
{ '@and' => [{ 'ukhpi:refMonth' => { '@ge' => { :@value => '2019-01', :@type => 'http://www.w3.org/2001/XMLSchema#gYearMonth' } } }, { 'ukhpi:refRegion' => { '@eq' => { :@id => 'http://landregistry.data.gov.uk/id/region/united-kingdom' } } }], '@sort' => [{ '@down' => 'ukhpi:refMonth' }], '@limit' => 1 }
end

def to_json(*_args)
terms.to_json
end
end.new

json = @dataset.query(query)
_(json).wont_be_nil
_(json.size).must_be :>, 0
Expand Down
Loading