Sha256: 5e0f8d1df108b54df52a7cfbf97b6fb7176eb2f204b1d4537d1f6ad605c8b852

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 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' and (bus[:format].blank? or bus[:format].to_s == 'csv')
        @parsed_url.query = 'output=csv&' + @parsed_url.query.sub(/\&*output=.*(\&|\z)/, '')
      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
        cmd = %{
          curl \
          --silent \
          --header "Expect: " \
          --location \
          #{"--data \"#{form_data}\"" if form_data.present?} \
          "#{parsed_url}" \
          --output "#{path}"
        }
        `#{cmd}`
        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

5 entries across 5 versions & 1 rubygems

Version Path
remote_table-0.2.14 lib/remote_table/request.rb
remote_table-0.2.13 lib/remote_table/request.rb
remote_table-0.2.12 lib/remote_table/request.rb
remote_table-0.2.11 lib/remote_table/request.rb
remote_table-0.2.10 lib/remote_table/request.rb