Class: DataKitten::DistributionFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/data_kitten/distribution_format.rb

Overview

A file format for a distribution

For instance CSV, XML, etc.

Constant Summary collapse

FORMATS =
{
  csv: { structured: true, open: true },
  xls: { structured: true, open: false },
  xlsx: { structured: true, open: true },
  rdf: { structured: true, open: true },
  xml: { structured: true, open: true },
  wms: { structured: true, open: true },
  ods: { structured: true, open: true },
  rdfa: { structured: true, open: true },
  kml: { structured: true, open: true },
  rss: { structured: true, open: true },
  json: { structured: true, open: true },
  ical: { structured: true, open: true },
  sparql: { structured: true, open: true },
  kml: { structured: true, open: true },
  georss: { structured: true, open: true },
  geojson: { structured: true, open: true },
  shp: { structured: true, open: true },
  html: { structured: false, open: true },
  doc: { structured: false, open: false },
  pdf: { structured: false, open: true }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(distribution) ⇒ DistributionFormat

Create a new DistributionFormat object with the relevant extension

Parameters:

  • distribution (Distribution)

    the distribution for the format



40
41
42
43
44
# File 'lib/data_kitten/distribution_format.rb', line 40

def initialize(distribution)
  @distribution = distribution
  # Store extension as a lowercase symbol
  @extension = distribution.extension.to_s.downcase.to_sym
end

Instance Attribute Details

#extensionSymbol

Returns a symbol for the file extension. For instance, :csv.

Returns:

  • (Symbol)

    a symbol for the file extension. For instance, :csv.



35
36
37
# File 'lib/data_kitten/distribution_format.rb', line 35

def extension
  @extension
end

Instance Method Details

#matches?Boolean

Whether the format of the file matches the extension given by the data

Returns:

  • (Boolean)

    whether the MIME type given in the HTTP response matches the data or not



63
64
65
66
67
68
69
70
71
# File 'lib/data_kitten/distribution_format.rb', line 63

def matches?
  begin
    mimes = []
    MIME::Types.type_for(@extension.to_s).each { |i| mimes << i.content_type }
    !!(@distribution.http_head.content_type =~ /#{mimes.join('|')}/) || false
  rescue
    nil
  end
end

#open?Boolean

Is this an open format?

Returns:

  • (Boolean)

    whether the format is open or not



56
57
58
# File 'lib/data_kitten/distribution_format.rb', line 56

def open?
  FORMATS[extension][:open]
end

#structured?Boolean

Is this a structured format?

Returns:

  • (Boolean)

    whether the format is machine-readable or not.



49
50
51
# File 'lib/data_kitten/distribution_format.rb', line 49

def structured?
  FORMATS[extension][:structured]
end