dech 0.0.4 → 0.0.5
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/Gemfile +2 -0
- data/README.md +2 -0
- data/lib/dech.rb +1 -0
- data/lib/dech/csv.rb +4 -0
- data/lib/dech/version.rb +1 -1
- data/lib/dech/yahoo.rb +7 -0
- data/lib/dech/yahoo/csv.rb +58 -0
- data/lib/dech/yahoo/ftp.rb +45 -0
- data/spec/dech/rakuten/csv_spec.rb +0 -2
- data/spec/dech/rakuten/ftp_spec.rb +0 -16
- data/spec/dech/yahoo/csv_spec.rb +44 -0
- data/spec/dech/yahoo/ftp_spec.rb +76 -0
- data/spec/dech/yahoo_spec.rb +5 -0
- data/spec/spec_helper.rb +3 -0
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e37c4e3ffda0a6566eb7bfd5315130ad4badcc5
|
4
|
+
data.tar.gz: 21995ddea993c65866b478cc49721f2a25c3cffe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29e3d5644ada4cfc0dd21cc6fba9588442fac3d32c818e82d8011f74007a1e7179d74869e7d865066c1111462310116dc4c1ca825bb12f9098a56b72beb4c138
|
7
|
+
data.tar.gz: bf6f9bcf903be27f07f28710ae5a55a0212d626086a4e46a11d3e8b9486356ec9b1c1f6508d6266da4eea4ae9fbcdbcd345f5021dbd9f84d09f62dca3b04e8f1
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Dech
|
2
2
|
|
3
|
+
[](http://badge.fury.io/rb/dech)
|
3
4
|

|
5
|
+
[](https://coveralls.io/r/e-maido/dech?branch=master)
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
data/lib/dech.rb
CHANGED
data/lib/dech/csv.rb
CHANGED
data/lib/dech/version.rb
CHANGED
data/lib/dech/yahoo.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'emmental'
|
3
|
+
require 'dech/csv'
|
4
|
+
require 'dech/hash_key_mapper'
|
5
|
+
|
6
|
+
module Dech
|
7
|
+
class Yahoo
|
8
|
+
class CSV < Dech::CSV
|
9
|
+
ENCODING = Encoding::Windows_31J
|
10
|
+
|
11
|
+
HEADER_MAPPINGS = {
|
12
|
+
id: "code",
|
13
|
+
price: ["price", "sale-price"]
|
14
|
+
}
|
15
|
+
|
16
|
+
REQUIRED_HEADERS = [
|
17
|
+
"path",
|
18
|
+
"name",
|
19
|
+
"code",
|
20
|
+
"price",
|
21
|
+
"sale-price"
|
22
|
+
]
|
23
|
+
|
24
|
+
def initialize(products)
|
25
|
+
@products = products
|
26
|
+
super(formatted_products)
|
27
|
+
end
|
28
|
+
|
29
|
+
def valid?
|
30
|
+
validate! rescue false
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate!
|
34
|
+
translated_products.each do |product|
|
35
|
+
REQUIRED_HEADERS.each do |header|
|
36
|
+
raise "#{header} is missing in #{product}" unless product.keys.include?(header)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def formatted_products
|
44
|
+
emmental = Emmental.new
|
45
|
+
translated_products.each{|product| emmental << product }
|
46
|
+
emmental.to_a
|
47
|
+
end
|
48
|
+
|
49
|
+
def translated_products
|
50
|
+
merged_products.map{|product| Dech::HashKeyMapper.map(product, HEADER_MAPPINGS) }
|
51
|
+
end
|
52
|
+
|
53
|
+
def merged_products
|
54
|
+
@products.map{|product| STATIC_COLUMNS.merge(product) }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'net/ftp'
|
3
|
+
require 'dech/yahoo/csv'
|
4
|
+
|
5
|
+
module Dech
|
6
|
+
class Yahoo
|
7
|
+
class FTP
|
8
|
+
DEFAULT_HOST = "yjftp.yahoofs.jp"
|
9
|
+
DEFAULT_PATH = "/data_spy.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::Yahoo::CSV.new(@products)
|
23
|
+
end
|
24
|
+
|
25
|
+
def ready?
|
26
|
+
ftp_connection{|ftp| !ftp.nlst(File.dirname(@path)).include?(@path) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def upload!
|
30
|
+
ftp_connection{|ftp| ftp.storlines("STOR #{@path}", csv) }
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def upload
|
35
|
+
ready? && upload!
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def ftp_connection
|
41
|
+
Net::FTP.open(@host, @username, @password){|ftp| yield(ftp) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -55,22 +55,6 @@ describe Dech::Rakuten::FTP do
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
=begin
|
59
|
-
describe "#upload" do
|
60
|
-
context "server is ready" do
|
61
|
-
before do
|
62
|
-
allow(ftp).to receive(:nlst).and_return([])
|
63
|
-
expect(ftp).to receive(:storlines)
|
64
|
-
expect(Net::FTP).to receive(:new).and_return(ftp).at_least(:once)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should upload CSV file to the path on FTP server" do
|
68
|
-
expect(dech.upload).to be true
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
=end
|
73
|
-
|
74
58
|
describe "#upload" do
|
75
59
|
subject{ lambda{ ftp.upload } }
|
76
60
|
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe Dech::Yahoo::CSV do
|
5
|
+
let(:valid_products){
|
6
|
+
[
|
7
|
+
{id: "ABC-001", price: 12800, "name" => "ProductName1", "path" => "a:b"},
|
8
|
+
{id: "xyz-123", price: 9800, "name" => "ProductName2", "path" => "a:b:c"}
|
9
|
+
]
|
10
|
+
}
|
11
|
+
|
12
|
+
let(:invalid_products){ [ {price: 12800}, {price: 9800} ] }
|
13
|
+
|
14
|
+
describe "initialize" do
|
15
|
+
subject{ Dech::Yahoo::CSV.new(valid_products) }
|
16
|
+
it { is_expected.to be_an_instance_of(Dech::Yahoo::CSV) }
|
17
|
+
it { is_expected.to be_a(Dech::CSV) }
|
18
|
+
it { is_expected.to be_a(StringIO) }
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#valid?" do
|
22
|
+
context "with valid columns" do
|
23
|
+
subject{ Dech::Yahoo::CSV.new(valid_products) }
|
24
|
+
it { is_expected.to be_valid }
|
25
|
+
end
|
26
|
+
|
27
|
+
context "with invalid columns" do
|
28
|
+
subject{ Dech::Yahoo::CSV.new(invalid_products) }
|
29
|
+
it { is_expected.not_to be_valid }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#validate!" do
|
34
|
+
context "valid columns" do
|
35
|
+
subject{ lambda{ Dech::Yahoo::CSV.new(valid_products).validate! } }
|
36
|
+
it { is_expected.not_to raise_error }
|
37
|
+
end
|
38
|
+
|
39
|
+
context "invalid columns" do
|
40
|
+
subject{ lambda{ Dech::Yahoo::CSV.new(invalid_products).validate! } }
|
41
|
+
it { is_expected.to raise_error }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe Dech::Yahoo::FTP do
|
5
|
+
let(:ftp) {
|
6
|
+
described_class.new(
|
7
|
+
products: [{id: "PRODUCT-CODE", price: 9800, "path" => "a:b", "name" => "productA"}],
|
8
|
+
username: "username",
|
9
|
+
password: "password",
|
10
|
+
host: "example.com"
|
11
|
+
)
|
12
|
+
}
|
13
|
+
|
14
|
+
let(:net_ftp) {
|
15
|
+
net_ftp = double("net_ftp")
|
16
|
+
allow(net_ftp).to receive(:passive=)
|
17
|
+
allow(net_ftp).to receive(:connect)
|
18
|
+
allow(net_ftp).to receive(:login)
|
19
|
+
allow(net_ftp).to receive(:close)
|
20
|
+
expect(Net::FTP).to receive(:new).and_return(net_ftp)
|
21
|
+
net_ftp
|
22
|
+
}
|
23
|
+
|
24
|
+
describe "initialize" do
|
25
|
+
subject { ftp }
|
26
|
+
it { is_expected.to be_an_instance_of(described_class) }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#csv" do
|
30
|
+
subject { ftp.csv }
|
31
|
+
it { is_expected.to be_an_instance_of(Dech::Yahoo::CSV) }
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#ready?" do
|
35
|
+
subject { ftp.ready? }
|
36
|
+
|
37
|
+
context "CSV file exists in FTP server" do
|
38
|
+
before { expect(net_ftp).to receive(:nlst).and_return([ftp.path]) }
|
39
|
+
it { is_expected.to be false }
|
40
|
+
end
|
41
|
+
|
42
|
+
context "CSV file does not exist in FTP server" do
|
43
|
+
before { expect(net_ftp).to receive(:nlst).and_return([]) }
|
44
|
+
it { is_expected.to be true }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#upload!" do
|
49
|
+
subject{ lambda{ ftp.upload! } }
|
50
|
+
|
51
|
+
it "should upload CSV file to the path on FTP server" do
|
52
|
+
expect(net_ftp).to receive(:storlines)
|
53
|
+
subject.call
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#upload" do
|
58
|
+
subject{ lambda{ ftp.upload } }
|
59
|
+
|
60
|
+
context "FTP server is ready" do
|
61
|
+
it "should call #upload!" do
|
62
|
+
expect(ftp).to receive(:ready?).and_return(true)
|
63
|
+
expect(ftp).to receive(:upload!).and_return(true)
|
64
|
+
subject.call
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "FTP server is not ready" do
|
69
|
+
it "should not call #upload!" do
|
70
|
+
expect(ftp).to receive(:ready?).and_return(false)
|
71
|
+
expect(ftp).not_to receive(:upload!)
|
72
|
+
subject.call
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OSA Shunsuke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: double-bag-ftps
|
@@ -123,6 +123,9 @@ files:
|
|
123
123
|
- lib/dech/rakuten/csv.rb
|
124
124
|
- lib/dech/rakuten/ftp.rb
|
125
125
|
- lib/dech/version.rb
|
126
|
+
- lib/dech/yahoo.rb
|
127
|
+
- lib/dech/yahoo/csv.rb
|
128
|
+
- lib/dech/yahoo/ftp.rb
|
126
129
|
- spec/dech/csv_spec.rb
|
127
130
|
- spec/dech/csvio_spec.rb
|
128
131
|
- spec/dech/hash_key_mapper_spec.rb
|
@@ -131,6 +134,9 @@ files:
|
|
131
134
|
- spec/dech/rakuten/csv_spec.rb
|
132
135
|
- spec/dech/rakuten/ftp_spec.rb
|
133
136
|
- spec/dech/rakuten_spec.rb
|
137
|
+
- spec/dech/yahoo/csv_spec.rb
|
138
|
+
- spec/dech/yahoo/ftp_spec.rb
|
139
|
+
- spec/dech/yahoo_spec.rb
|
134
140
|
- spec/dech_spec.rb
|
135
141
|
- spec/spec_helper.rb
|
136
142
|
homepage: https://github.com/e-maido/dech
|
@@ -166,5 +172,8 @@ test_files:
|
|
166
172
|
- spec/dech/rakuten/csv_spec.rb
|
167
173
|
- spec/dech/rakuten/ftp_spec.rb
|
168
174
|
- spec/dech/rakuten_spec.rb
|
175
|
+
- spec/dech/yahoo/csv_spec.rb
|
176
|
+
- spec/dech/yahoo/ftp_spec.rb
|
177
|
+
- spec/dech/yahoo_spec.rb
|
169
178
|
- spec/dech_spec.rb
|
170
179
|
- spec/spec_helper.rb
|