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.

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/data_kitten/distribution_format.rb', line 16

def initialize(distribution)
  @distribution = distribution
  # Store extension as a lowercase symbol
  @extension = distribution.extension.to_s.downcase.to_sym
  # Set up format lists
  @@formats ||= {
    csv:     { structured:  true, open:  true },
    xls:     { structured:  true, open: false },
    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 },
    shp:     { structured:  true, open:  true },
    html:    { structured: false, open:  true },
    doc:     { structured: false, open:  false },
    pdf:     { structured: false, open:  true },
  }
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.



11
12
13
# File 'lib/data_kitten/distribution_format.rb', line 11

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



60
61
62
63
64
65
66
67
68
# File 'lib/data_kitten/distribution_format.rb', line 60

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



53
54
55
# File 'lib/data_kitten/distribution_format.rb', line 53

def open?
  @@formats[@extension][:open] rescue nil
end

#structured?Boolean

Is this a structured format?

Returns:

  • (Boolean)

    whether the format is machine-readable or not.



46
47
48
# File 'lib/data_kitten/distribution_format.rb', line 46

def structured?
  @@formats[@extension][:structured] rescue nil
end