Sha256: 3468497caae4af84881424bb5d7b3b07b473471d6fe2d1db71f4b735bcc7fd14

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

class RemoteTable
  class Request
    attr_accessor :parsed_url, :post_data, :username, :password
    attr_accessor :form_data
    
    # TODO: support HTTP basic auth
    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
    
    def download
      path = ::File.join staging_dir_path, 'REMOTE_TABLE_PACKAGE'
      if parsed_url.scheme == 'file'
        parsed_url.path
      else
        RemoteTable.backtick_with_reporting %{
          curl
          --header "Expect: "
          --location
          #{"--data \"#{form_data}\"" if form_data.present?}
          "#{parsed_url}"
          --output "#{path}"
        }
        path
      end
    end
    
    private
    
    def staging_dir_path    
      return @_staging_dir_path if @_staging_dir_path
      @_staging_dir_path = ::File.join Dir.tmpdir, rand.to_s
      FileUtils.mkdir @_staging_dir_path
      at_exit { FileUtils.rm_rf @_staging_dir_path }
      @_staging_dir_path
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remote_table-0.2.17 lib/remote_table/request.rb