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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 558691d6d3a193e1034f53c757653d8b445e0ce1
4
- data.tar.gz: 7eff60d509fe99172ec652dec658b41af0b60541
3
+ metadata.gz: 1e37c4e3ffda0a6566eb7bfd5315130ad4badcc5
4
+ data.tar.gz: 21995ddea993c65866b478cc49721f2a25c3cffe
5
5
  SHA512:
6
- metadata.gz: 1532a2a6c9daa0005fc6b937304b8bf4e450da8117e144f3064d0592fbe0bca1344fc98e1a18c2b23197c0b71469d235936142a224ca5d8226f241c53e064ffc
7
- data.tar.gz: dfbc97fc9eb0673f8f770abc13217a98695a31fda768a23d26b77edb5c457819f99de71bf40b84eee48eccccfc831882545ad3e44e26530d3def468980692438
6
+ metadata.gz: 29e3d5644ada4cfc0dd21cc6fba9588442fac3d32c818e82d8011f74007a1e7179d74869e7d865066c1111462310116dc4c1ca825bb12f9098a56b72beb4c138
7
+ data.tar.gz: bf6f9bcf903be27f07f28710ae5a55a0212d626086a4e46a11d3e8b9486356ec9b1c1f6508d6266da4eea4ae9fbcdbcd345f5021dbd9f84d09f62dca3b04e8f1
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in dech.gemspec
4
4
  gemspec
5
+
6
+ gem "coveralls", require: false
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Dech
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/dech.svg)](http://badge.fury.io/rb/dech)
3
4
  ![travis](https://travis-ci.org/e-maido/dech.svg?branch=master)
5
+ [![Coverage Status](https://coveralls.io/repos/e-maido/dech/badge.png?branch=master)](https://coveralls.io/r/e-maido/dech?branch=master)
4
6
 
5
7
  ## Installation
6
8
 
@@ -4,6 +4,7 @@ require "dech/hash_key_mapper"
4
4
  require "dech/version"
5
5
 
6
6
  require "dech/rakuten"
7
+ require "dech/yahoo"
7
8
 
8
9
  # deprecated classes
9
10
  require "dech/price_uploader"
@@ -7,6 +7,10 @@ module Dech
7
7
  class CSV < StringIO
8
8
  DEFAULT_ENCODING = Encoding::Windows_31J
9
9
 
10
+ HEADER_MAPPINGS = {}
11
+ REQUIRED_HEADERS = []
12
+ STATIC_COLUMNS = {}
13
+
10
14
  def initialize(array, args={})
11
15
  @array = array
12
16
  @option = {}
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
2
 
3
3
  module Dech
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
  end
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+ require 'dech/yahoo/ftp'
3
+
4
+ module Dech
5
+ class Yahoo
6
+ end
7
+ end
@@ -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
@@ -12,8 +12,6 @@ describe Dech::Rakuten::CSV do
12
12
  it { is_expected.to be_an_instance_of(Dech::Rakuten::CSV) }
13
13
  it { is_expected.to be_a(Dech::CSV) }
14
14
  it { is_expected.to be_a(StringIO) }
15
-
16
- p Dech::Rakuten::CSV.new(products).to_a
17
15
  end
18
16
 
19
17
  describe "#valid?" do
@@ -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
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Dech::Yahoo do
5
+ end
@@ -6,3 +6,6 @@ $: << project_root
6
6
  Dir[File.join(File.dirname(__FILE__), "support", "*")].each {|f| require f }
7
7
 
8
8
  require 'lib/dech'
9
+
10
+ require 'coveralls'
11
+ Coveralls.wear!
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
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-07 00:00:00.000000000 Z
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