embulk-parser-xpath 0.0.1

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.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ embulk-parser-xpath (0.0.1)
5
+ nokogiri (~> 1.6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ nokogiri (1.6.7.1-java)
11
+ rake (10.4.2)
12
+
13
+ PLATFORMS
14
+ java
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0)
18
+ embulk-parser-xpath!
19
+ rake (~> 10.0)
20
+
21
+ BUNDLED WITH
22
+ 1.10.6
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "embulk-parser-xpath"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Tatsunori Matoba"]
9
+ spec.email = ["matobat@gmail.com"]
10
+ spec.summary = %q{Embulk parser plugin for XML with XPath support}
11
+ spec.description = %q{XPath parser plugin is Embulk plugin to fetch entries in xml format use XPath.}
12
+ spec.homepage = "https://github.com/matobat/embulk-parser-xpath"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "nokogiri", "~> 1.6"
20
+ spec.add_development_dependency "bundler", "~> 1.0"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
@@ -0,0 +1,115 @@
1
+ require "nokogiri"
2
+
3
+ module Embulk
4
+ module Parser
5
+
6
+ class XPathParserPlugin < ParserPlugin
7
+ Plugin.register_parser("xpath", self)
8
+
9
+ def self.transaction(config, &control)
10
+ schema = config.param("schema", :array)
11
+ schema_serialized = schema.inject({}) do |memo, s|
12
+ memo[s["name"]] = s["type"]
13
+ memo
14
+ end
15
+ task = {
16
+ :schema => schema_serialized,
17
+ :root => config.param("root", :string),
18
+ :namespaces => config.param("namespaces", :hash)
19
+ }
20
+ columns = schema.each_with_index.map do |c, i|
21
+ Column.new(i, c["name"], c["type"].to_sym)
22
+ end
23
+ yield(task, columns)
24
+ end
25
+
26
+ def run(file_input)
27
+ on_new_record = lambda {|record|
28
+ @page_builder.add(record)
29
+ }
30
+ doc = RecordBinder.new(@task["root"],
31
+ @task["schema"], @task["namespaces"], on_new_record)
32
+
33
+ while file = file_input.next_file
34
+ data = file.read
35
+ if !data.nil? && !data.empty?
36
+ doc.clear
37
+ doc.parse(data)
38
+ end
39
+ end
40
+ @page_builder.finish
41
+ end
42
+ end
43
+
44
+ class RecordBinder
45
+
46
+ def initialize(root, schema, namespaces, on_new_record)
47
+ @root = root
48
+ @schema = schema
49
+ @on_new_record = on_new_record
50
+ @namespaces = namespaces
51
+ clear
52
+ super()
53
+ end
54
+
55
+ def clear
56
+ @find_route_idx = 0
57
+ @enter = false
58
+ @current_element_name = nil
59
+ @current_data = new_map_by_schema
60
+ end
61
+
62
+ def parse(data)
63
+ doc = Nokogiri::XML(data)
64
+
65
+ items = doc.xpath(@root, @namespaces)
66
+ items.each do |item|
67
+ @current_data = new_map_by_schema
68
+
69
+ @schema.each do |key,value|
70
+ doc.xpath(key, @namespaces).each do |item|
71
+ @current_data[key] = item.text
72
+ end
73
+ end
74
+
75
+ @on_new_record.call(@current_data.map{|k, v| v})
76
+ end
77
+ end
78
+
79
+ def characters(string)
80
+ return if !@enter || string.strip.size == 0 || @current_element_name.nil?
81
+ val = @current_data[@current_element_name]
82
+ val = "" if val.nil?
83
+ val += string
84
+ @current_data[@current_element_name] = val
85
+ end
86
+
87
+ private
88
+
89
+ def new_map_by_schema
90
+ @schema.keys.inject({}) do |memo, k|
91
+ memo[k] = nil
92
+ memo
93
+ end
94
+ end
95
+
96
+ def convert(val, type)
97
+ v = val.nil? ? "" : val
98
+ case type
99
+ when "string"
100
+ v
101
+ when "long"
102
+ v.to_i
103
+ when "double"
104
+ v.to_f
105
+ when "boolean"
106
+ ["yes", "true", "1"].include?(v.downcase)
107
+ when "timestamp"
108
+ v.empty? ? nil : Time.strptime(v, c["format"])
109
+ else
110
+ raise "Unsupported type '#{type}'"
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: embulk-parser-xpath
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Tatsunori Matoba
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2016-01-05 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: nokogiri
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 1
32
+ - 6
33
+ version: "1.6"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 15
45
+ segments:
46
+ - 1
47
+ - 0
48
+ version: "1.0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rake
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 35
60
+ segments:
61
+ - 10
62
+ - 0
63
+ version: "10.0"
64
+ type: :development
65
+ version_requirements: *id003
66
+ description: XPath parser plugin is Embulk plugin to fetch entries in xml format use XPath.
67
+ email:
68
+ - matobat@gmail.com
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files: []
74
+
75
+ files:
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - LICENSE.txt
79
+ - Rakefile
80
+ - embulk-parser-xpath.gemspec
81
+ - lib/embulk/parser/xpath.rb
82
+ has_rdoc: true
83
+ homepage: https://github.com/matobat/embulk-parser-xpath
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ requirements: []
110
+
111
+ rubyforge_project:
112
+ rubygems_version: 1.3.7
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Embulk parser plugin for XML with XPath support
116
+ test_files: []
117
+