Class: RemoteTable::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_table/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bus) ⇒ Request

TODO: support HTTP basic auth

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
# File 'lib/remote_table/request.rb', line 7

def initialize(bus)
  raise(ArgumentError, "RemoteTable needs :url option") unless bus[:url].present?
  @parsed_url = URI.parse bus[:url]
  if @parsed_url.host == 'spreadsheets.google.com'
    if bus[:format].blank? or bus[:format].to_s == 'csv'
      @parsed_url.query = 'output=csv&' + @parsed_url.query.sub(/\&?output=.*?(\&|\z)/, '\1')
    end
  end
  @form_data = bus[:form_data]
end

Instance Attribute Details

#form_dataObject

Returns the value of attribute form_data.



4
5
6
# File 'lib/remote_table/request.rb', line 4

def form_data
  @form_data
end

#parsed_urlObject

Returns the value of attribute parsed_url.



3
4
5
# File 'lib/remote_table/request.rb', line 3

def parsed_url
  @parsed_url
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/remote_table/request.rb', line 3

def password
  @password
end

#post_dataObject

Returns the value of attribute post_data.



3
4
5
# File 'lib/remote_table/request.rb', line 3

def post_data
  @post_data
end

#usernameObject

Returns the value of attribute username.



3
4
5
# File 'lib/remote_table/request.rb', line 3

def username
  @username
end

Instance Method Details

#downloadObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/remote_table/request.rb', line 18

def download
  path = ::File.join staging_dir_path, 'REMOTE_TABLE_PACKAGE'
  if parsed_url.scheme == 'file'
    ::FileUtils.cp parsed_url.path, path
  else
    RemoteTable.backtick_with_reporting %{
      curl
      --header "Expect: "
      --location
      #{"--data #{Escape.shell_single_word form_data}" if form_data.present?}
      #{Escape.shell_single_word parsed_url.to_s}
      --output #{Escape.shell_single_word path}
      2>&1
    }
  end
  path
end

#staging_dir_pathObject



36
37
38
39
40
41
42
# File 'lib/remote_table/request.rb', line 36

def staging_dir_path    
  return @_staging_dir_path if @_staging_dir_path
  @_staging_dir_path = ::File.join Dir.tmpdir, 'remote_table_gem', rand.to_s
  FileUtils.mkdir_p @_staging_dir_path
  RemoteTable.remove_at_exit @_staging_dir_path
  @_staging_dir_path
end