data_processor 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c5d3e4b459833af7ae7a6fae536aed4e0fe7c0bf
4
+ data.tar.gz: 16eebb950572912b767e7d81560642568add98da
5
+ SHA512:
6
+ metadata.gz: 42727dbc3633d50c8e5f2fe21e2b76d20426a76c9b88b25fa07a0afbf452cb01505c536d57e3edc36a4ba29206f01657b88c79c245efc5d25086e8e8d02a0f6f
7
+ data.tar.gz: 3d6d00e5015020c34a8add41c3801cda92349d265a2b4647d79d180ab39f5e2b0f2ded48561bb2bc7b50c7444c896cd3dfe889798e24bf9baea4c81e1a39c404
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in data_processor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Steven Vandevelde
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Data Processor
2
+
3
+ Parse YAML and Markdown files, manipulate their data and convert it to JSON.
4
+
5
+
6
+
7
+ ## Dependencies
8
+
9
+ - Redcarpet
10
+ - Oj
11
+
12
+
13
+
14
+ ## How it works
15
+
16
+ _File structure:_
17
+
18
+ ```
19
+ /example_path/en/base.yaml
20
+ /example_path/en/pages/index.yaml
21
+ /example_path/en/pages/index.md
22
+ ```
23
+
24
+ _JSON Output: (by using the code below)_
25
+
26
+ ```json
27
+ {
28
+ "en": {
29
+ "is_directory": true,
30
+ "locale": "en",
31
+
32
+ "base": {
33
+ "value_from_base_yaml": "1"
34
+ },
35
+
36
+ "pages": {
37
+ "is_directory": true,
38
+
39
+ "index": {
40
+ "title": "Homepage Title",
41
+ "slug": "homepage-title",
42
+
43
+ "parsed_markdown": "<p>Parsed markdown from index.md</p>",
44
+
45
+ "is_yaml": true,
46
+ "is_markdown": true
47
+ }
48
+ }
49
+ }
50
+ }
51
+ ```
52
+
53
+ _Ruby Hash:_
54
+
55
+ Pretty much the same as the JSON output, strings as keys, not symbols.
56
+
57
+
58
+
59
+ ## Usage
60
+
61
+ ```ruby
62
+ # New instance
63
+ # -> takes path as first argument, default is current directory
64
+ d = DataProcessor.new("/")
65
+
66
+ # Import data
67
+ # -> takes path as first argument, default is initial directory
68
+ d.import("example_path/")
69
+
70
+ # Manipulate data
71
+ # -> 1st arg, the path without file extension (string)
72
+ # -> 2nd arg, override, obj will be set to the return value of the block (boolean, optional)
73
+ # -> block to execute manipulations in
74
+ d.manipulate("en") { |obj| obj["locale"] = "en" }
75
+ d.manipulate("en/pages/index") { |obj| obj["slug"] = obj["title"].to_slug }
76
+
77
+ # Output
78
+ d.get_data # ruby hash
79
+ d.output_json # json
80
+ ```
81
+
82
+
83
+
84
+ ## Markdown
85
+
86
+ Redcarpet is used here to parse markdown. You can override the markdown renderer by overriding `DataProcessor::markdown_renderer`, which returns a renderer.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "data_processor/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "data_processor"
8
+ spec.version = DataProcessor::VERSION
9
+ spec.authors = ["Steven Vandevelde"]
10
+ spec.email = ["icid.asset@gmail.com"]
11
+ spec.summary = %q{Parse YAML and Markdown files, manipulate their data and convert it to JSON}
12
+ spec.description = spec.summary
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "redcarpet", "~> 3.1.2"
22
+ spec.add_runtime_dependency "oj", "~> 2.10.2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "minitest"
27
+ end
@@ -0,0 +1,9 @@
1
+ class DataProcessor
2
+ module Getters
3
+
4
+ def get_data
5
+ @data
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ class DataProcessor
2
+ module Import
3
+
4
+ def import(path="", data_obj=nil)
5
+ path = path.sub(/^\//, "")
6
+
7
+ data_hash = data_obj || @data
8
+ full_path = @initial_path + path
9
+
10
+ Dir.foreach(full_path) do |filename|
11
+ next if filename[0] == "."
12
+ combined_path = File.join(full_path, filename)
13
+
14
+ # directory
15
+ if File.directory?(combined_path)
16
+ data_hash[filename] ||= {}
17
+ data_hash[filename].merge!(import(File.join(path, filename), data_hash[filename]))
18
+ data_hash[filename].merge!({ "is_directory" => true })
19
+
20
+ # yaml file
21
+ elsif filename.end_with?(".yml")
22
+ without_ext = File.basename(filename, ".yml")
23
+ yaml_data = YAML.load_file(combined_path) || {}
24
+
25
+ data_hash[without_ext] ||= {}
26
+ data_hash[without_ext].merge!(yaml_data)
27
+ data_hash[without_ext].merge!({ "is_yaml" => true })
28
+
29
+ # markdown file
30
+ elsif filename.end_with?(".md")
31
+ without_ext = File.basename(filename, ".md")
32
+ file = File.open(combined_path, "r")
33
+ parsed_markdown = self.parse_markdown(file.read)
34
+ file.close
35
+
36
+ data_hash[without_ext] ||= {}
37
+ data_hash[without_ext].merge!({ "parsed_markdown" => parsed_markdown })
38
+ data_hash[without_ext].merge!({ "is_markdown" => true })
39
+
40
+ end
41
+ end
42
+
43
+ data_hash
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,37 @@
1
+ class DataProcessor
2
+ module Manipulate
3
+
4
+ def manipulate(path, override=false)
5
+ obj = @data
6
+ parent_obj = nil
7
+
8
+ if block_given?
9
+ counter = 0
10
+ path_split = path.split("/")
11
+
12
+ # down the rabbit hole
13
+ path_split.each do |p|
14
+ if obj && obj[p]
15
+ parent_obj = obj
16
+ obj = obj[p]
17
+ counter = counter + 1
18
+ else
19
+ parent_obj = nil
20
+ obj = nil
21
+ end
22
+ end
23
+
24
+ # execute block with object as param
25
+ # ie. if the object is found
26
+ # + override object with return value of block if needed
27
+ if obj && (counter == path_split.length)
28
+ return_value = yield(obj, parent_obj)
29
+ parent_obj[path_split.last] = return_value if override
30
+ end
31
+ end
32
+
33
+ obj
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ class DataProcessor
2
+ module Output
3
+
4
+ def output_json
5
+ Oj.dump(@data)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ class DataProcessor
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,49 @@
1
+ require "yaml"
2
+ require "redcarpet"
3
+ require "oj"
4
+
5
+ require "data_processor/getters"
6
+ require "data_processor/import"
7
+ require "data_processor/manipulate"
8
+ require "data_processor/output"
9
+
10
+ class DataProcessor
11
+ include DataProcessor::Getters
12
+ include DataProcessor::Import
13
+ include DataProcessor::Manipulate
14
+ include DataProcessor::Output
15
+
16
+ def initialize(initial_path="./")
17
+ @data = {}
18
+ @initial_path = initial_path
19
+
20
+ create_markdown_parser
21
+ end
22
+
23
+
24
+ #
25
+ # Markdown
26
+ #
27
+ def self.markdown_renderer
28
+ Redcarpet::Render::HTML
29
+ end
30
+
31
+
32
+ def create_markdown_parser
33
+ @markdown_parser = Redcarpet::Markdown.new(
34
+ DataProcessor::markdown_renderer,
35
+ fenced_code_blocks: true,
36
+ strikethrough: true,
37
+ superscript: true,
38
+ underline: true,
39
+ highlight: true,
40
+ smartypants: true
41
+ )
42
+ end
43
+
44
+
45
+ def parse_markdown(string)
46
+ @markdown_parser.render(string)
47
+ end
48
+
49
+ end
@@ -0,0 +1 @@
1
+ value_from_base: "1"
@@ -0,0 +1 @@
1
+ Parsed markdown from index.md
@@ -0,0 +1 @@
1
+ value_from_index_yaml: "1"
data/test/test.rb ADDED
@@ -0,0 +1,76 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
+
3
+ require "minitest/autorun"
4
+ require "data_processor"
5
+
6
+ class TestDataProcessor < MiniTest::Unit::TestCase
7
+
8
+ def setup
9
+ @d = ::DataProcessor.new("./test/fixtures/")
10
+ end
11
+
12
+
13
+ def test_initial_data_object
14
+ assert_kind_of Hash, @d.get_data
15
+ end
16
+
17
+
18
+ def test_import
19
+ @d.import("data/")
20
+ data = @d.get_data
21
+
22
+ assert_kind_of Hash, data["en"]
23
+ assert_kind_of Hash, data["en"]["base"]
24
+ assert_kind_of Hash, data["en"]["pages"]
25
+ assert_kind_of Hash, data["en"]["pages"]["index"]
26
+
27
+ assert_equal(
28
+ "<p>Parsed markdown from index.md</p>\n",
29
+ data["en"]["pages"]["index"]["parsed_markdown"]
30
+ )
31
+
32
+ assert_equal(
33
+ "1",
34
+ data["en"]["pages"]["index"]["value_from_index_yaml"]
35
+ )
36
+ end
37
+
38
+
39
+ def test_manipulate
40
+ @d.import("data/")
41
+
42
+ # manipulate
43
+ @d.manipulate("en") { |obj| obj["locale"] = "en" }
44
+ @d.manipulate("en/pages", true) { |obj| obj.to_a }
45
+
46
+ # get data
47
+ data = @d.get_data
48
+
49
+ # assertions
50
+ assert_equal(
51
+ "en",
52
+ data["en"]["locale"]
53
+ )
54
+
55
+ assert_kind_of(
56
+ Array,
57
+ data["en"]["pages"]
58
+ )
59
+ end
60
+
61
+
62
+ def test_output
63
+ @d.import("data/")
64
+
65
+ # data
66
+ data = @d.get_data
67
+ parsed_json = Oj.load(@d.output_json)
68
+
69
+ # assertions
70
+ assert_equal(
71
+ parsed_json["en"]["pages"]["index"]["value_from_index_yaml"],
72
+ data["en"]["pages"]["index"]["value_from_index_yaml"]
73
+ )
74
+ end
75
+
76
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_processor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steven Vandevelde
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redcarpet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.10.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.10.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Parse YAML and Markdown files, manipulate their data and convert it to
84
+ JSON
85
+ email:
86
+ - icid.asset@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - data_processor.gemspec
97
+ - lib/data_processor.rb
98
+ - lib/data_processor/getters.rb
99
+ - lib/data_processor/import.rb
100
+ - lib/data_processor/manipulate.rb
101
+ - lib/data_processor/output.rb
102
+ - lib/data_processor/version.rb
103
+ - test/fixtures/data/en/base.yml
104
+ - test/fixtures/data/en/pages/index.md
105
+ - test/fixtures/data/en/pages/index.yml
106
+ - test/test.rb
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.2.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Parse YAML and Markdown files, manipulate their data and convert it to JSON
131
+ test_files:
132
+ - test/fixtures/data/en/base.yml
133
+ - test/fixtures/data/en/pages/index.md
134
+ - test/fixtures/data/en/pages/index.yml
135
+ - test/test.rb