Class: RemoteTable

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/remote_table.rb,
lib/remote_table/format.rb,
lib/remote_table/hasher.rb,
lib/remote_table/version.rb,
lib/remote_table/executor.rb,
lib/remote_table/format/xml.rb,
lib/remote_table/local_file.rb,
lib/remote_table/properties.rb,
lib/remote_table/format/html.rb,
lib/remote_table/transformer.rb,
lib/remote_table/format/excel.rb,
lib/remote_table/format/excelx.rb,
lib/remote_table/format/delimited.rb,
lib/remote_table/format/fixed_width.rb,
lib/remote_table/format/open_office.rb,
lib/remote_table/format/mixins/textual.rb,
lib/remote_table/format/mixins/processed_by_roo.rb,
lib/remote_table/format/mixins/processed_by_nokogiri.rb

Defined Under Namespace

Classes: Executor, Format, Hasher, LocalFile, Properties, Transform, Transformer

Constant Summary collapse

VERSION =
"1.2.0"
CSV =
::CSV

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RemoteTable

Create a new RemoteTable.

RemoteTable.new(url, options = {})

New syntax:

RemoteTable.new('www.customerreferenceprogram.org/uploads/CRP_RFP_template.xlsx', 'foo' => 'bar')

Old syntax:

RemoteTable.new(:url => 'www.customerreferenceprogram.org/uploads/CRP_RFP_template.xlsx', :foo => 'bar')

See the Properties object for the sorts of options you can pass.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/remote_table.rb', line 52

def initialize(*args)
  @options = args.last.is_a?(::Hash) ? args.last.dup : {}
  @options.stringify_keys!
  @url = if args.first.is_a? ::String
    args.first.dup
  else
    @options['url'].dup
  end
  @url.freeze
  @options.freeze
end

Instance Attribute Details

#download_countObject (readonly)

Returns the value of attribute download_count.



144
145
146
# File 'lib/remote_table.rb', line 144

def download_count
  @download_count
end

#optionsObject (readonly)

Returns the value of attribute options.



40
41
42
# File 'lib/remote_table.rb', line 40

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



39
40
41
# File 'lib/remote_table.rb', line 39

def url
  @url
end

Class Method Details

.executorObject

Used internally to execute stuff in shells.



115
116
117
# File 'lib/remote_table.rb', line 115

def self.executor
  Executor.instance
end

.hasherObject

Used internally to create unique hashes of rows.



120
121
122
# File 'lib/remote_table.rb', line 120

def self.hasher
  Hasher.instance
end

Instance Method Details

#[](row_number) ⇒ Object

Get a row by row number



99
100
101
102
103
104
105
# File 'lib/remote_table.rb', line 99

def [](row_number)
  if fully_cached?
    cache[row_number]
  else
    to_a[row_number]
  end
end

#each(&blk) ⇒ Object Also known as: each_row

not thread safe



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/remote_table.rb', line 65

def each(&blk)
  if fully_cached?
    cache.each(&blk)
  else
    mark_download!
    retval = format.each do |row|
      row.row_hash = ::RemoteTable.hasher.hash row
      transformer.transform(row).each do |virtual_row|
        if properties.errata
          next if properties.errata.rejects? virtual_row
          properties.errata.correct! virtual_row
        end
        next if properties.select and !properties.select.call(virtual_row)
        next if properties.reject and properties.reject.call(virtual_row)
        cache.push virtual_row unless properties.streaming
        yield virtual_row
      end
    end
    fully_cached! unless properties.streaming
    retval
  end
end

#formatObject

Used internally to access to the driver that reads the format



135
136
137
# File 'lib/remote_table.rb', line 135

def format
  @format ||= properties.format.new self
end

#freeObject

clear the row cache to save memory



108
109
110
111
112
# File 'lib/remote_table.rb', line 108

def free
  cache.clear
  ::GC.start
  nil
end

#local_fileObject

Used internally to access to a downloaded copy of the file



125
126
127
# File 'lib/remote_table.rb', line 125

def local_file
  @local_file ||= LocalFile.new self
end

#propertiesObject

Used internally to access to the properties of the table, either set by the user or implied



130
131
132
# File 'lib/remote_table.rb', line 130

def properties
  @properties ||= Properties.new self
end

#to_aObject Also known as: rows



89
90
91
92
93
94
95
# File 'lib/remote_table.rb', line 89

def to_a
  if fully_cached?
    cache.dup
  else
    map { |row| row }
  end
end

#transformerObject

Used internally to acess the transformer (aka parser).



140
141
142
# File 'lib/remote_table.rb', line 140

def transformer
  @transformer ||= Transformer.new self
end