Class: Nice::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nice/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ckan_url: nil, api_key: nil) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/nice/client.rb', line 7

def initialize(ckan_url: nil, api_key: nil)
  @ckan_url = ckan_url || Nice.configuration&.ckan_url
  @api_key = api_key || Nice.configuration&.api_key

  validate_credentials!

  # add ending slash if missing
  fixed_ckan_url = @ckan_url.end_with?('/') ? @ckan_url : "#{@ckan_url}/"
  CKAN::API.api_url = fixed_ckan_url
  CKAN::API.api_version = "3"
  CKAN::API.api_key = @api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



5
6
7
# File 'lib/nice/client.rb', line 5

def api_key
  @api_key
end

#ckan_urlObject (readonly)

Returns the value of attribute ckan_url.



5
6
7
# File 'lib/nice/client.rb', line 5

def ckan_url
  @ckan_url
end

Instance Method Details

#get_package(package_or_id) ⇒ Object

Get package details



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nice/client.rb', line 28

def get_package(package_or_id)
  package_id = package_or_id.is_a?(CKAN::Package) ? package_or_id.id : package_or_id
  package = CKAN::Package.find(package_id)
  {
    'id' => package.id,
    'name' => package.name,
    'title' => package.title,
    'resources' => package.resources
  }
rescue => e
  raise APIError, "Failed to get package: #{e.message}"
end

#list_package_idsObject

List all package IDs (datasets)



21
22
23
24
25
# File 'lib/nice/client.rb', line 21

def list_package_ids
  CKAN::Package.find
rescue => e
  raise APIError, "Failed to list packages: #{e.message}"
end