Class: DataverseOperations
- Inherits:
-
Object
- Object
- DataverseOperations
- Defined in:
- lib/dvla/dataverse/operations/dataverse_operations.rb
Direct Known Subclasses
CreateRecords, DeleteRecord, RetrieveRecords, ServiceCheck, UpdateRecord
Instance Method Summary collapse
-
#apply_filter(options) ⇒ Object
Set up the filter to be in the url query string.
-
#get_url_from_config_file ⇒ String
load the url from the configuration yml file.
-
#http_request_artifacts ⇒ Struct::HttpRequestArtifacts
HttpRequestArtifacts is to store the artifacts needed to make the http request.
-
#initialize ⇒ DataverseOperations
constructor
A new instance of DataverseOperations.
-
#send_request ⇒ RestClient
Send http request.
- #setup_artifacts ⇒ Object
-
#setup_http_headers ⇒ Hash{String->Float or String
set up the request headers.
Constructor Details
#initialize ⇒ DataverseOperations
Returns a new instance of DataverseOperations.
13 14 15 16 17 |
# File 'lib/dvla/dataverse/operations/dataverse_operations.rb', line 13 def initialize Authenticator.instance.setup_authentication_configs setup_artifacts end |
Instance Method Details
#apply_filter(options) ⇒ Object
Set up the filter to be in the url query string. The filter only be the name of the record, the maximum number of records to be retrieved and the record id.
79 80 81 82 83 84 85 86 |
# File 'lib/dvla/dataverse/operations/dataverse_operations.rb', line 79 def apply_filter() max_number_of_records = [:top] ? "?$top=#{[:top]}" : '' record_id = [:id] ? "(#{[:id]})" : '' record_nam = [:record_name] url = http_request_artifacts.url http_request_artifacts.url = "#{url}#{record_nam}#{max_number_of_records}#{record_id}" end |
#get_url_from_config_file ⇒ String
load the url from the configuration yml file. You should overwrite this function if you dont dont want to use the base url
this function will throw an error if no url is found under dataverse:operations:base:url
31 32 33 34 |
# File 'lib/dvla/dataverse/operations/dataverse_operations.rb', line 31 def get_url_from_config_file url = Settings&.dataverse&.operations&.base_url url.nil? ? raise('base url is missing from the configuration file') : url end |
#http_request_artifacts ⇒ Struct::HttpRequestArtifacts
HttpRequestArtifacts is to store the artifacts needed to make the http request. headers: for the request headers, should be a hash url: the url to make the request, should be string method: the http method, for example :post payload: the body of the request if any
44 45 46 |
# File 'lib/dvla/dataverse/operations/dataverse_operations.rb', line 44 def http_request_artifacts @http_request_artifacts ||= Struct::HttpRequestArtifacts.new end |
#send_request ⇒ RestClient
Send http request. Make sure that http_request_artifacts
is setup with url, headers, http method and payload if needed
67 68 69 70 71 72 |
# File 'lib/dvla/dataverse/operations/dataverse_operations.rb', line 67 def send_request RestClient::Request.execute(method: http_request_artifacts.method, url: http_request_artifacts.url, headers: http_request_artifacts.headers, payload: http_request_artifacts.payload) end |
#setup_artifacts ⇒ Object
19 20 21 |
# File 'lib/dvla/dataverse/operations/dataverse_operations.rb', line 19 def setup_artifacts raise NotImplementedError, "#{self.class} method '#{__method__} has not been implemented'" end |
#setup_http_headers ⇒ Hash{String->Float or String
set up the request headers. this will also call @@authenticator to get the authorization token
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dvla/dataverse/operations/dataverse_operations.rb', line 52 def setup_http_headers headers = { 'If-None-Match' => nil, 'OData-Version' => 4.0, 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'OData-MaxVersion' => 4.0, 'Authorization' => Authenticator.instance.get_token } http_request_artifacts.headers = headers end |