Class: DataPackage::Resource

Inherits:
Hash
  • Object
show all
Defined in:
lib/datapackage/resource.rb

Direct Known Subclasses

InlineResource, LocalResource, RemoteResource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, base_path = '') ⇒ Resource

Returns a new instance of Resource.



4
5
6
# File 'lib/datapackage/resource.rb', line 4

def initialize(resource, base_path = '')
  self.merge! resource
end

Class Method Details

.file_exists?(resource, base_path) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/datapackage/resource.rb', line 30

def self.file_exists?(resource, base_path)
  path = resource['path']
  path = File.join(base_path, path) if base_path != ''
  File.exists?(path)
end

.is_url?(resource, base_path) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/datapackage/resource.rb', line 36

def self.is_url?(resource, base_path)
  return true if resource['url'] != nil && resource['path'] == nil && resource['data'] == nil
  return true if base_path.start_with?('http')
end

.load(resource, base_path = '') ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/datapackage/resource.rb', line 8

def self.load(resource, base_path = '')
  # This returns if there are no alternative ways to access the data OR there
  # is a base_path which is a URL
  if is_url?(resource, base_path)
    RemoteResource.new(resource, base_path)
  else
    # If there's a data attribute, we definitely want an inline resource
    if resource['data']
      InlineResource.new(resource)
    else
      # If the file exists - we want a local resource
      if file_exists?(resource, base_path)
        LocalResource.new(resource, base_path)
      # If it doesn't exist and there's a URL to grab the data from, we want
      # a remote resource
      elsif resource['url']
        RemoteResource.new(resource, base_path)
      end
    end
  end
end

Instance Method Details

#tableObject



41
42
43
# File 'lib/datapackage/resource.rb', line 41

def table
  @table ||= JsonTableSchema::Table.new(CSV.parse(data), self['schema']) if self['schema']
end