Sha256: aef3fc498129f1e027ae167fe3418b88491698af5b34c19a39816b4fcce9867b

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

# coding: utf-8

require 'dech/csvio'
require 'net/ftp'

module Dech
  module PriceUploader
    module Dena
      class FTP
        HEADERS = %w(Code exhibittype Price KtaiPrice)

        attr_accessor :username, :host, :path

        def initialize(args={})
          @products = args[:products] || []
          @username = args[:username]
          @password = args[:password]
          @host     = args[:host] || "bcmaster1.dena.ne.jp"
          @path     = args[:path] || "/data.csv"
        end

        def ready?
          true
        end

        def csv
          Dech::CSVIO.generate do |csv|
            csv << HEADERS
            @products.each do |product|
              csv << [product[:id], "MX", product[:price], product[:price]]
            end
          end
        end

        def save_csv_as(filename)
          FileUtils.mkdir_p(File.dirname(filename))
          File.open(filename, "w:windows-31j") do |file|
            file << csv.string
          end
        end

        def upload!
          ftp_connection{|ftp| ftp.storlines("STOR #{@path}", csv) }
          true
        end

        def upload
          ready? && upload!
        end

        private

        def ftp_connection(&block)
          ftp = Net::FTP.new
          ftp.connect(@host)
          ftp.login(@username, @password)

          yield(ftp)
        ensure
          ftp.close
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dech-0.0.7 lib/dech/price_uploader/dena/ftp.rb
dech-0.0.6 lib/dech/price_uploader/dena/ftp.rb
dech-0.0.5 lib/dech/price_uploader/dena/ftp.rb
dech-0.0.4 lib/dech/price_uploader/dena/ftp.rb
dech-0.0.3 lib/dech/price_uploader/dena/ftp.rb