dech 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/dech.rb +1 -0
- data/lib/dech/ponpare.rb +7 -0
- data/lib/dech/ponpare/csv.rb +57 -0
- data/lib/dech/ponpare/ftps.rb +53 -0
- data/lib/dech/version.rb +1 -1
- data/spec/dech/ponpare/csv_spec.rb +60 -0
- data/spec/dech/ponpare/ftps_spec.rb +78 -0
- data/spec/dech/ponpare_spec.rb +5 -0
- metadata +10 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a298dba8dc5995a1a411bb5c7f51a46cf9e97a61
|
4
|
+
data.tar.gz: d1c7288d35568fc22f1b91f58cf435c8daa00288
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe95dfc6dc917b601e92a453ad8d4a414733702d08ce18df8698ab8b93cf81bdc9fbc83e2075af8ad9348560571d7be20c0e29b0bde0f00ea5472ab58645f0c6
|
7
|
+
data.tar.gz: 10aece1245ad3cf0eba3a0e29ddc2bed670a3e70b25ab33445b2ab1bba92b1b2e314430115fe9ce82ed1f733426c5e8fbe10ad665560cb392f44d2bb500723b1
|
data/lib/dech.rb
CHANGED
data/lib/dech/ponpare.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'emmental'
|
3
|
+
require 'dech/csv'
|
4
|
+
require 'dech/hash_key_mapper'
|
5
|
+
|
6
|
+
module Dech
|
7
|
+
class Ponpare
|
8
|
+
class CSV < Dech::CSV
|
9
|
+
ENCODING = Encoding::Windows_31J
|
10
|
+
|
11
|
+
HEADER_MAPPINGS = {
|
12
|
+
id: "商品管理ID(商品URL)",
|
13
|
+
price: "販売価格"
|
14
|
+
}
|
15
|
+
|
16
|
+
REQUIRED_HEADERS = [
|
17
|
+
"コントロールカラム",
|
18
|
+
"商品管理ID(商品URL)"
|
19
|
+
]
|
20
|
+
|
21
|
+
STATIC_COLUMNS = {"コントロールカラム" => "u"}
|
22
|
+
|
23
|
+
def initialize(products)
|
24
|
+
@products = products
|
25
|
+
super(formatted_products)
|
26
|
+
end
|
27
|
+
|
28
|
+
def valid?
|
29
|
+
validate! rescue false
|
30
|
+
end
|
31
|
+
|
32
|
+
def validate!
|
33
|
+
translated_products.each do |product|
|
34
|
+
REQUIRED_HEADERS.each do |header|
|
35
|
+
raise "#{header} is missing in #{product}" unless product.keys.include?(header)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def formatted_products
|
43
|
+
emmental = Emmental.new
|
44
|
+
translated_products.each{|product| emmental << product }
|
45
|
+
emmental.to_a
|
46
|
+
end
|
47
|
+
|
48
|
+
def translated_products
|
49
|
+
merged_products.map{|product| Dech::HashKeyMapper.map(product, HEADER_MAPPINGS) }
|
50
|
+
end
|
51
|
+
|
52
|
+
def merged_products
|
53
|
+
@products.map{|product| STATIC_COLUMNS.merge(product) }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'net/ftp'
|
3
|
+
require 'dech/ponpare/csv'
|
4
|
+
|
5
|
+
module Dech
|
6
|
+
class Ponpare
|
7
|
+
class FTPS
|
8
|
+
DEFAULT_HOST = "ftps.ponparemall.com"
|
9
|
+
DEFAULT_PATH = "/item.csv"
|
10
|
+
|
11
|
+
attr_accessor :products, :username, :host, :path
|
12
|
+
|
13
|
+
def initialize(args={})
|
14
|
+
@products = args[:products] || []
|
15
|
+
@username = args[:username]
|
16
|
+
@password = args[:password]
|
17
|
+
@host = args[:host] || DEFAULT_HOST
|
18
|
+
@path = args[:path] || DEFAULT_PATH
|
19
|
+
end
|
20
|
+
|
21
|
+
def csv
|
22
|
+
Dech::Ponpare::CSV.new(@products)
|
23
|
+
end
|
24
|
+
|
25
|
+
def ready?
|
26
|
+
ftps_connection{|ftps| !ftps.nlst(File.dirname(@path)).include?(@path) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def upload!
|
30
|
+
ftps_connection{|ftps| ftps.storlines("STOR #{@path}", csv) }
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def upload
|
35
|
+
ready? && upload!
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def ftps_connection(&block)
|
41
|
+
ftps = DoubleBagFTPS.new
|
42
|
+
ftps.passive = true
|
43
|
+
ftps.ssl_context = DoubleBagFTPS.create_ssl_context(verify_mode: OpenSSL::SSL::VERIFY_NONE)
|
44
|
+
ftps.connect(@host)
|
45
|
+
ftps.login(@username, @password)
|
46
|
+
|
47
|
+
yield(ftps)
|
48
|
+
ensure
|
49
|
+
ftps.close
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/dech/version.rb
CHANGED
@@ -0,0 +1,60 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe Dech::Ponpare::CSV do
|
5
|
+
describe "initialize" do
|
6
|
+
products = [
|
7
|
+
{id: "ABC-001", price: 12800},
|
8
|
+
{id: "xyz-123", price: 9800}
|
9
|
+
]
|
10
|
+
|
11
|
+
subject{ Dech::Ponpare::CSV.new(products) }
|
12
|
+
it { is_expected.to be_an_instance_of(Dech::Ponpare::CSV) }
|
13
|
+
it { is_expected.to be_a(Dech::CSV) }
|
14
|
+
it { is_expected.to be_a(StringIO) }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#valid?" do
|
18
|
+
context "with valid columns" do
|
19
|
+
products = [
|
20
|
+
{id: "ABC-001", price: 12800},
|
21
|
+
{id: "xyz-123", price: 9800}
|
22
|
+
]
|
23
|
+
|
24
|
+
subject{ Dech::Ponpare::CSV.new(products) }
|
25
|
+
it { is_expected.to be_valid }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with invalid columns" do
|
29
|
+
products = [
|
30
|
+
{price: 12800},
|
31
|
+
{price: 9800}
|
32
|
+
]
|
33
|
+
|
34
|
+
subject{ Dech::Ponpare::CSV.new(products) }
|
35
|
+
it { is_expected.not_to be_valid }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#validate!" do
|
40
|
+
context "valid columns" do
|
41
|
+
products = [
|
42
|
+
{id: "ABC-001", price: 12800},
|
43
|
+
{id: "xyz-123", price: 9800}
|
44
|
+
]
|
45
|
+
|
46
|
+
subject{ lambda{ Dech::Ponpare::CSV.new(products).validate! } }
|
47
|
+
it { is_expected.not_to raise_error }
|
48
|
+
end
|
49
|
+
|
50
|
+
context "invalid columns" do
|
51
|
+
products = [
|
52
|
+
{price: 12800},
|
53
|
+
{price: 9800}
|
54
|
+
]
|
55
|
+
|
56
|
+
subject{ lambda{ Dech::Ponpare::CSV.new(products).validate! } }
|
57
|
+
it { is_expected.to raise_error }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Dech::Ponpare::FTPS do
|
6
|
+
let(:ftp) {
|
7
|
+
described_class.new(
|
8
|
+
products: [{id: "PRODUCT-CODE", price: 9800}],
|
9
|
+
username: "username",
|
10
|
+
password: "password",
|
11
|
+
host: "example.com"
|
12
|
+
)
|
13
|
+
}
|
14
|
+
|
15
|
+
let(:ftps) {
|
16
|
+
ftps = double("ftps")
|
17
|
+
allow(ftps).to receive(:passive=)
|
18
|
+
allow(ftps).to receive(:ssl_context=)
|
19
|
+
allow(ftps).to receive(:connect)
|
20
|
+
allow(ftps).to receive(:login)
|
21
|
+
allow(ftps).to receive(:close)
|
22
|
+
expect(DoubleBagFTPS).to receive(:new).and_return(ftps)
|
23
|
+
ftps
|
24
|
+
}
|
25
|
+
|
26
|
+
describe "initialize" do
|
27
|
+
subject { ftp }
|
28
|
+
it { is_expected.to be_an_instance_of(described_class) }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#csv" do
|
32
|
+
subject { ftp.csv }
|
33
|
+
it { is_expected.to be_an_instance_of(Dech::Ponpare::CSV) }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#ready?" do
|
37
|
+
subject { ftp.ready? }
|
38
|
+
|
39
|
+
context "CSV file exists in FTPS server" do
|
40
|
+
before { expect(ftps).to receive(:nlst).and_return([ftp.path]) }
|
41
|
+
it { is_expected.to be false }
|
42
|
+
end
|
43
|
+
|
44
|
+
context "CSV file does not exist in FTPS server" do
|
45
|
+
before { expect(ftps).to receive(:nlst).and_return([]) }
|
46
|
+
it { is_expected.to be true }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#upload!" do
|
51
|
+
subject{ lambda{ ftp.upload! } }
|
52
|
+
|
53
|
+
it "should upload CSV file to the path on FTPS server" do
|
54
|
+
expect(ftps).to receive(:storlines)
|
55
|
+
subject.call
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#upload" do
|
60
|
+
subject{ lambda{ ftp.upload } }
|
61
|
+
|
62
|
+
context "FTPS server is ready" do
|
63
|
+
it "should call #upload!" do
|
64
|
+
expect(ftp).to receive(:ready?).and_return(true)
|
65
|
+
expect(ftp).to receive(:upload!).and_return(true)
|
66
|
+
subject.call
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "FTPS server is not ready" do
|
71
|
+
it "should not call #upload!" do
|
72
|
+
expect(ftp).to receive(:ready?).and_return(false)
|
73
|
+
expect(ftp).not_to receive(:upload!)
|
74
|
+
subject.call
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
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.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OSA Shunsuke
|
@@ -117,6 +117,9 @@ files:
|
|
117
117
|
- lib/dech/dena/csv.rb
|
118
118
|
- lib/dech/dena/ftp.rb
|
119
119
|
- lib/dech/hash_key_mapper.rb
|
120
|
+
- lib/dech/ponpare.rb
|
121
|
+
- lib/dech/ponpare/csv.rb
|
122
|
+
- lib/dech/ponpare/ftps.rb
|
120
123
|
- lib/dech/price_uploader.rb
|
121
124
|
- lib/dech/price_uploader/dena.rb
|
122
125
|
- lib/dech/price_uploader/dena/ftp.rb
|
@@ -135,6 +138,9 @@ files:
|
|
135
138
|
- spec/dech/dena/ftp_spec.rb
|
136
139
|
- spec/dech/dena_spec.rb
|
137
140
|
- spec/dech/hash_key_mapper_spec.rb
|
141
|
+
- spec/dech/ponpare/csv_spec.rb
|
142
|
+
- spec/dech/ponpare/ftps_spec.rb
|
143
|
+
- spec/dech/ponpare_spec.rb
|
138
144
|
- spec/dech/price_uploader/dena/ftp_spec.rb
|
139
145
|
- spec/dech/price_uploader/ponpare/ftps_spec.rb
|
140
146
|
- spec/dech/rakuten/csv_spec.rb
|
@@ -176,6 +182,9 @@ test_files:
|
|
176
182
|
- spec/dech/dena/ftp_spec.rb
|
177
183
|
- spec/dech/dena_spec.rb
|
178
184
|
- spec/dech/hash_key_mapper_spec.rb
|
185
|
+
- spec/dech/ponpare/csv_spec.rb
|
186
|
+
- spec/dech/ponpare/ftps_spec.rb
|
187
|
+
- spec/dech/ponpare_spec.rb
|
179
188
|
- spec/dech/price_uploader/dena/ftp_spec.rb
|
180
189
|
- spec/dech/price_uploader/ponpare/ftps_spec.rb
|
181
190
|
- spec/dech/rakuten/csv_spec.rb
|