dech 0.0.5 → 0.0.6

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: 1e37c4e3ffda0a6566eb7bfd5315130ad4badcc5
4
- data.tar.gz: 21995ddea993c65866b478cc49721f2a25c3cffe
3
+ metadata.gz: db8a51d0bc99784877dd3a64ecd77bbf791dfe90
4
+ data.tar.gz: d49ce44554b96aadbac97896b7823b09c2178b97
5
5
  SHA512:
6
- metadata.gz: 29e3d5644ada4cfc0dd21cc6fba9588442fac3d32c818e82d8011f74007a1e7179d74869e7d865066c1111462310116dc4c1ca825bb12f9098a56b72beb4c138
7
- data.tar.gz: bf6f9bcf903be27f07f28710ae5a55a0212d626086a4e46a11d3e8b9486356ec9b1c1f6508d6266da4eea4ae9fbcdbcd345f5021dbd9f84d09f62dca3b04e8f1
6
+ metadata.gz: c41234c2d6c9396580925ab3dbc18b2190b10af298e84315290fc47fe055549450376633fb2a21cad764b3eba3f6f94cb850f2bad3cf12925bf492f96c372f39
7
+ data.tar.gz: ba3b53b0e18b8beffc71e0bea37ff2c9e96c533fa23c06d528904d423f21f029c1b9317b107107b89eced51c5b8e79d1bb5243c69c7ba6992f8c24487ecf6b9c
data/lib/dech.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "dech/hash_key_mapper"
4
4
  require "dech/version"
5
5
 
6
+ require "dech/dena"
6
7
  require "dech/rakuten"
7
8
  require "dech/yahoo"
8
9
 
data/lib/dech/dena.rb ADDED
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+ require 'dech/dena/ftp'
3
+
4
+ module Dech
5
+ class Dena
6
+ end
7
+ end
@@ -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 Dena
8
+ class CSV < Dech::CSV
9
+ ENCODING = Encoding::Windows_31J
10
+
11
+ HEADER_MAPPINGS = {
12
+ id: "Code",
13
+ price: ["Price", "KtaiPrice"]
14
+ }
15
+
16
+ REQUIRED_HEADERS = [
17
+ "Code",
18
+ "exhibittype"
19
+ ]
20
+
21
+ STATIC_COLUMNS = {"exhibittype" => "MX"}
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,45 @@
1
+ # coding: utf-8
2
+ require 'net/ftp'
3
+ require 'dech/dena/csv'
4
+
5
+ module Dech
6
+ class Dena
7
+ class FTP
8
+ DEFAULT_HOST = "bcmaster1.dena.ne.jp"
9
+ DEFAULT_PATH = "/data.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::Dena::CSV.new(@products)
23
+ end
24
+
25
+ def ready?
26
+ true
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
data/lib/dech/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
2
 
3
3
  module Dech
4
- VERSION = "0.0.5"
4
+ VERSION = "0.0.6"
5
5
  end
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Dech::Dena::CSV do
5
+ let(:valid_products){
6
+ [
7
+ {id: "ABC-001", price: 12800},
8
+ {id: "xyz-123", price: 9800}
9
+ ]
10
+ }
11
+
12
+ let(:invalid_products){ [ {price: 12800}, {price: 9800} ] }
13
+
14
+ describe "initialize" do
15
+ subject{ Dech::Dena::CSV.new(valid_products) }
16
+ it { is_expected.to be_an_instance_of(Dech::Dena::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::Dena::CSV.new(valid_products) }
24
+ it { is_expected.to be_valid }
25
+ end
26
+
27
+ context "with invalid columns" do
28
+ subject{ Dech::Dena::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::Dena::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::Dena::CSV.new(invalid_products).validate! } }
41
+ it { is_expected.to raise_error }
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,70 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Dech::Dena::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::Dena::CSV) }
32
+ end
33
+
34
+ describe "#ready?" do
35
+ subject { ftp.ready? }
36
+
37
+ context "Always" do
38
+ it { is_expected.to be true }
39
+ end
40
+ end
41
+
42
+ describe "#upload!" do
43
+ subject{ lambda{ ftp.upload! } }
44
+
45
+ it "should upload CSV file to the path on FTP server" do
46
+ expect(net_ftp).to receive(:storlines)
47
+ subject.call
48
+ end
49
+ end
50
+
51
+ describe "#upload" do
52
+ subject{ lambda{ ftp.upload } }
53
+
54
+ context "FTP server is ready" do
55
+ it "should call #upload!" do
56
+ expect(ftp).to receive(:ready?).and_return(true)
57
+ expect(ftp).to receive(:upload!).and_return(true)
58
+ subject.call
59
+ end
60
+ end
61
+
62
+ context "FTP server is not ready" do
63
+ it "should not call #upload!" do
64
+ expect(ftp).to receive(:ready?).and_return(false)
65
+ expect(ftp).not_to receive(:upload!)
66
+ subject.call
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Dech::Dena do
5
+ end
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.5
4
+ version: 0.0.6
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-19 00:00:00.000000000 Z
11
+ date: 2014-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: double-bag-ftps
@@ -113,6 +113,9 @@ files:
113
113
  - lib/dech.rb
114
114
  - lib/dech/csv.rb
115
115
  - lib/dech/csvio.rb
116
+ - lib/dech/dena.rb
117
+ - lib/dech/dena/csv.rb
118
+ - lib/dech/dena/ftp.rb
116
119
  - lib/dech/hash_key_mapper.rb
117
120
  - lib/dech/price_uploader.rb
118
121
  - lib/dech/price_uploader/dena.rb
@@ -128,6 +131,9 @@ files:
128
131
  - lib/dech/yahoo/ftp.rb
129
132
  - spec/dech/csv_spec.rb
130
133
  - spec/dech/csvio_spec.rb
134
+ - spec/dech/dena/csv_spec.rb
135
+ - spec/dech/dena/ftp_spec.rb
136
+ - spec/dech/dena_spec.rb
131
137
  - spec/dech/hash_key_mapper_spec.rb
132
138
  - spec/dech/price_uploader/dena/ftp_spec.rb
133
139
  - spec/dech/price_uploader/ponpare/ftps_spec.rb
@@ -166,6 +172,9 @@ summary: Utilities gem for e-commerce mall in Japan.
166
172
  test_files:
167
173
  - spec/dech/csv_spec.rb
168
174
  - spec/dech/csvio_spec.rb
175
+ - spec/dech/dena/csv_spec.rb
176
+ - spec/dech/dena/ftp_spec.rb
177
+ - spec/dech/dena_spec.rb
169
178
  - spec/dech/hash_key_mapper_spec.rb
170
179
  - spec/dech/price_uploader/dena/ftp_spec.rb
171
180
  - spec/dech/price_uploader/ponpare/ftps_spec.rb