Sha256: e340a2f61c94bf573d9a73c7ab74aff6cb52df97824bb8f162f6b003c7eb3290

Contents?: true

Size: 1010 Bytes

Versions: 3

Compression:

Stored size: 1010 Bytes

Contents

module PhoneGap
  module Build
    class PackageDownloader

      attr_reader :id, :platform, :target_dir, :http_response

      def download(id, platform, target_dir = '/tmp')
        @id, @platform, @target_dir = id, platform, target_dir
        @http_response = PhoneGap::Build::ApiRequest.new.get("/apps/#{id}/#{platform}")
        save_file if http_response.success?
      end

      private

      def save_file
        FileUtils.mkdir_p(platform_output_dir)
        puts "Saving to #{file_path}"
        File.open(file_path, 'w+') { |f| f.write(http_response.body) }
        puts 'Download complete'
      end

      def platform_output_dir
        File.join(target_dir, platform.to_s)
      end

      def file_path
        File.join(platform_output_dir, file_name)
      end

      def file_name
        file_name_from_uri(http_response.request.instance_variable_get(:@last_uri).request_uri)
      end

      def file_name_from_uri(uri)
        uri.match(/\/([^\/]*)$/)[0]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
phone_gap-build-0.7.0 lib/phone_gap/build/package_downloader.rb
phone_gap-build-0.6.2 lib/phone_gap/build/package_downloader.rb
phone_gap-build-0.6.1 lib/phone_gap/build/package_downloader.rb