Sha256: 9caa488487abd5c1329429071a33882a3acb88cc1c4e7cf45092ef348ee016dd

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

class RemoteTable
  class Request
    attr_accessor :url, :post_data, :username, :password
    attr_accessor :form_data
    
    # TODO: support post_data
    # TODO: support HTTP basic auth
    def initialize(bus)
      @url = bus[:url] or raise "need url"
      @form_data = bus[:form_data]
    end
    
    def download
      path = ::File.join(staging_dir_path, 'REMOTE_TABLE_PACKAGE')
      cmd = %{
        curl \
        --silent \
        --header "Expect: " \
        --location \
        #{"--data \"#{form_data}\"" if form_data.present?} \
        "#{url_with_google_docs_handling}" \
        --output "#{path}"
      }
      `#{cmd}`
      path
    end
    
    private
    
    def staging_dir_path
      path = tempfile_path_from_url
      FileUtils.rm_f(path)
      FileUtils.mkdir(path)
      at_exit { FileUtils.rm_rf(path) }
      path
    end
    
    def tempfile_path_from_url
      Tempfile.open(url.gsub(/[^a-z0-9]+/i, '_')[0,100]).path
    end
    
    def url_with_google_docs_handling
      url = self.url
      if url.include?('spreadsheets.google.com')
        url = url.gsub(/\&output=.*(\&|\z)/, '')
        url << "&output=csv"
      end
      url
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
remote_table-0.2.8 lib/remote_table/request.rb
remote_table-0.2.7 lib/remote_table/request.rb
remote_table-0.2.6 lib/remote_table/request.rb