Class: DataverseOperations

Inherits:
Object
  • Object
show all
Defined in:
lib/dvla/dataverse/operations/dataverse_operations.rb

Instance Method Summary collapse

Constructor Details

#initializeDataverseOperations

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.

Parameters:

  • options (Object)

    the record name and max returned records



79
80
81
82
83
84
85
86
# File 'lib/dvla/dataverse/operations/dataverse_operations.rb', line 79

def apply_filter(options)
  max_number_of_records = options[:top] ? "?$top=#{options[:top]}" : ''
  record_id = options[:id] ? "(#{options[:id]})" : ''
  record_nam = options[: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_fileString

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

Returns:

  • (String)

    base url

Raises:



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_artifactsStruct::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

Returns:

  • (Struct::HttpRequestArtifacts)


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_requestRestClient

Send http request. Make sure that http_request_artifacts is setup with url, headers, http method and payload if needed

Returns:

  • (RestClient)

    RestClient response



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_artifactsObject

Raises:

  • (NotImplementedError)


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_headersHash{String->Float or String

set up the request headers. this will also call @@authenticator to get the authorization token

Returns:

  • (Hash{String->Float or String)

    ]



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