dech 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30f639c446e2de69ab0c1d076d46737877cc41e6
4
- data.tar.gz: 6b016294b7c598b0862980f6d34c7f803aabb4df
3
+ metadata.gz: e75dc0ac05d00fe019e35c471e4d8bf99d987499
4
+ data.tar.gz: c373618bf25961474e9812a1c204ffa74dc34419
5
5
  SHA512:
6
- metadata.gz: 2da66aeff6363ee32ec6458ebb5093661849531da215f5bb27ea07378b1323a45d0fc85a083a0915c33aaf58053216040c4c9edc7dfcd78850fd6ba7f58b684e
7
- data.tar.gz: cecbbbb78092e15d48c53a2615905332d291099a51e5630e35d03de3c04e89dbf4c1730dc88e5e1ff5c542fd33e93fd3f5acd0fbcde18188f3a15edad739b1f8
6
+ metadata.gz: a19d46d53d061d34999c4f2506a797ade35cc7f148fbf0dc36d7ec3830460017db6013128a788eac0c89456cf6272a4c371d65ade4f5fbab0bd3cf43bd9c24a5
7
+ data.tar.gz: bd39eb4c5f5775afc549ee875a696b2761e335e8be352997a4b6be2cd6645561d792fad4ae56a7cd1147a0122e94378c64ebf1e64af12ca7fb4e4c6cee367d0e
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require "dech/version"
4
+ require "dech/csvio"
4
5
  require "dech/price_uploader"
5
6
 
6
7
  module Dech
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+
3
+ require 'csv'
4
+ require 'stringio'
5
+
6
+ module Dech
7
+ class CSVIO < StringIO
8
+ class << self
9
+ def generate(args={})
10
+ csv_string = CSV.generate{|csv| yield(csv) if block_given? }
11
+ self.new(csv_string.encode(args[:encoding] || Encoding::Windows_31J))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require "dech/price_uploader/ponpare"
4
+ require "dech/price_uploader/dena"
4
5
 
5
6
  module Dech
6
7
  module PriceUploader
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+
3
+ require "dech/price_uploader/dena/ftp"
4
+
5
+ module Dech
6
+ module PriceUploader
7
+ module Dena
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,65 @@
1
+ # coding: utf-8
2
+
3
+ require 'dech/csvio'
4
+ require 'net/ftp'
5
+
6
+ module Dech
7
+ module PriceUploader
8
+ module Dena
9
+ class FTP
10
+ HEADERS = %w(Code exhibittype Price KtaiPrice)
11
+
12
+ attr_accessor :username, :host, :path
13
+
14
+ def initialize(args={})
15
+ @products = args[:products] || []
16
+ @username = args[:username]
17
+ @password = args[:password]
18
+ @host = args[:host] || "bcmaster1.dena.ne.jp"
19
+ @path = args[:path] || "/data.csv"
20
+ end
21
+
22
+ def ready?
23
+ true
24
+ end
25
+
26
+ def csv
27
+ Dech::CSVIO.generate do |csv|
28
+ csv << HEADERS
29
+ @products.each do |product|
30
+ csv << [product[:id], "MX", product[:price], product[:price]]
31
+ end
32
+ end
33
+ end
34
+
35
+ def save_csv_as(filename)
36
+ FileUtils.mkdir_p(File.dirname(filename))
37
+ File.open(filename, "w:windows-31j") do |file|
38
+ file << csv.string
39
+ end
40
+ end
41
+
42
+ def upload!
43
+ ftp_connection{|ftp| ftp.storlines("STOR #{@path}", csv) }
44
+ true
45
+ end
46
+
47
+ def upload
48
+ ready? && upload!
49
+ end
50
+
51
+ private
52
+
53
+ def ftp_connection(&block)
54
+ ftp = Net::FTP.new
55
+ ftp.connect(@host)
56
+ ftp.login(@username, @password)
57
+
58
+ yield(ftp)
59
+ ensure
60
+ ftp.close
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,8 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
- require "net/ftp"
3
+ require "dech/csvio"
4
4
  require "double_bag_ftps"
5
- require "csv"
6
5
 
7
6
  module Dech
8
7
  module PriceUploader
@@ -25,14 +24,12 @@ module Dech
25
24
  end
26
25
 
27
26
  def csv
28
- csv_string = CSV.generate do |csv|
27
+ Dech::CSVIO.generate do |csv|
29
28
  csv << HEADERS
30
29
  @products.each do |product|
31
30
  csv << ["u", product[:id].to_s.downcase, product[:price]]
32
31
  end
33
32
  end
34
-
35
- StringIO.new(csv_string.encode(Encoding::Windows_31J))
36
33
  end
37
34
 
38
35
  def save_csv_as(filename)
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
2
 
3
3
  module Dech
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Dech::CSVIO do
6
+ describe ".generate" do
7
+ describe "class" do
8
+ it "should return an instance of StringIO" do
9
+ actual = Dech::CSVIO.generate
10
+ expect(actual).to be_is_a(StringIO)
11
+ end
12
+
13
+ it "should return an instance of Dech::CSVIO" do
14
+ actual = Dech::CSVIO.generate
15
+ expect(actual).to be_is_a(Dech::CSVIO)
16
+ end
17
+ end
18
+
19
+ describe "encoding" do
20
+ it "should have windows-31j external_encoding as default" do
21
+ actual = Dech::CSVIO.generate
22
+ expect(actual.external_encoding).to eq(Encoding::Windows_31J)
23
+ end
24
+
25
+ it "should have given external_encoding" do
26
+ actual = Dech::CSVIO.generate(encoding: Encoding::UTF_8)
27
+ expect(actual.external_encoding).to eq(Encoding::UTF_8)
28
+ end
29
+ end
30
+
31
+ describe "headers" do
32
+ before do
33
+ @headers = %w(a b c d e)
34
+ io = Dech::CSVIO.generate{|csv| csv << @headers }
35
+ @csv = CSV.new(io, headers: true)
36
+ @csv.readlines
37
+ end
38
+
39
+ it "should be able to set headers" do
40
+ expect(@csv.headers).to eq(@headers)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,111 @@
1
+ # coding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Dech::PriceUploader::Dena::FTP do
6
+ let(:dech) {
7
+ Dech::PriceUploader::Dena::FTP.new(
8
+ products: [{id: "PRODUCT-CODE", price: 9800}],
9
+ username: "username",
10
+ password: "password",
11
+ host: "example.com"
12
+ )
13
+ }
14
+
15
+ let(:ftp) {
16
+ ftp = double("ftp")
17
+ allow(ftp).to receive(:passive=)
18
+ allow(ftp).to receive(:connect)
19
+ allow(ftp).to receive(:login)
20
+ allow(ftp).to receive(:close)
21
+ ftp
22
+ }
23
+
24
+ describe "initialize" do
25
+ context "given no args" do
26
+ it "should create an instance successfully" do
27
+ expect(dech).to be_an_instance_of(Dech::PriceUploader::Dena::FTP)
28
+ end
29
+ end
30
+
31
+ context "given some args" do
32
+ it "should create an instance successfully" do
33
+ expect(dech).to be_an_instance_of(Dech::PriceUploader::Dena::FTP)
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#ready?" do
39
+ it "should be false" do
40
+ expect(dech.ready?).to be true
41
+ end
42
+ end
43
+
44
+ describe "#csv" do
45
+ headers = {
46
+ "Code" => String,
47
+ "exhibittype" => /\AMX\Z/,
48
+ "KtaiPrice" => /\d+/
49
+ }
50
+
51
+ describe "headers" do
52
+ let(:csv){ CSV.new(dech.csv, headers: true) }
53
+
54
+ headers.each_key.map{|h| h.encode(Encoding::Windows_31J) }.each do |header|
55
+ it "should have '#{header}' header" do
56
+ csv.readlines
57
+ expect(csv.headers).to be_include(header)
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "columns" do
63
+ let(:csv){ CSV.new(dech.csv, headers: true).read }
64
+
65
+ headers.each do |header, type|
66
+ it "should have #{type} in '#{header}'" do
67
+ expect(csv[header.encode(Encoding::Windows_31J)]).to be_all{|c| type === c }
68
+ end
69
+ end
70
+ end
71
+
72
+ describe "encoding" do
73
+ let(:io){ dech.csv }
74
+
75
+ it "should have windows-31j as external_encoding" do
76
+ expect(io.external_encoding).to eq(Encoding::Windows_31J)
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#save_csv_as" do
82
+ let(:filename){ "tmp/#{Time.now.strftime("%Y%m%d_%H%M%S_%N")}.csv" }
83
+
84
+ it "should save CSV file as given name" do
85
+ dech.save_csv_as(filename)
86
+ expect(Dir.glob("tmp/*")).to be_include(filename)
87
+ end
88
+
89
+ it "should save CSV file in Shift_JIS" do
90
+ dech.save_csv_as(filename)
91
+ CSV.open(filename, "r:windows-31j:utf-8", headers: true) do |csv|
92
+ expect{csv.readlines}.not_to raise_error
93
+ expect(csv.headers).to eq(Dech::PriceUploader::Dena::FTP::HEADERS)
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "#upload" do
99
+ context "server is ready" do
100
+ before do
101
+ allow(ftp).to receive(:nlst).and_return([])
102
+ expect(ftp).to receive(:storlines)
103
+ expect(Net::FTP).to receive(:new).and_return(ftp).at_least(:once)
104
+ end
105
+
106
+ it "should upload CSV file to the path on FTP server" do
107
+ expect(dech.upload).to be true
108
+ end
109
+ end
110
+ end
111
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dech
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - OSA Shunsuke
@@ -97,10 +97,15 @@ files:
97
97
  - Rakefile
98
98
  - dech.gemspec
99
99
  - lib/dech.rb
100
+ - lib/dech/csvio.rb
100
101
  - lib/dech/price_uploader.rb
102
+ - lib/dech/price_uploader/dena.rb
103
+ - lib/dech/price_uploader/dena/ftp.rb
101
104
  - lib/dech/price_uploader/ponpare.rb
102
105
  - lib/dech/price_uploader/ponpare/ftps.rb
103
106
  - lib/dech/version.rb
107
+ - spec/dech/csvio_spec.rb
108
+ - spec/dech/price_uploader/dena/ftp_spec.rb
104
109
  - spec/dech/price_uploader/ponpare/ftps_spec.rb
105
110
  - spec/dech_spec.rb
106
111
  - spec/spec_helper.rb
@@ -129,6 +134,8 @@ signing_key:
129
134
  specification_version: 4
130
135
  summary: Utilities gem for e-commerce mall in Japan.
131
136
  test_files:
137
+ - spec/dech/csvio_spec.rb
138
+ - spec/dech/price_uploader/dena/ftp_spec.rb
132
139
  - spec/dech/price_uploader/ponpare/ftps_spec.rb
133
140
  - spec/dech_spec.rb
134
141
  - spec/spec_helper.rb