active_loader 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +6 -0
- data/active_loader.gemspec +21 -0
- data/bin/hack +10 -0
- data/bin/setup +8 -0
- data/lib/active_loader.rb +27 -0
- data/lib/active_loader/content_loader.rb +43 -0
- data/lib/active_loader/json_loader.rb +26 -0
- data/lib/active_loader/loader_dispatcher.rb +34 -0
- data/lib/active_loader/parse_error.rb +8 -0
- data/lib/active_loader/version.rb +5 -0
- data/lib/active_loader/yaml_loader.rb +40 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 294c857c9a8a676542ffbe18c31efad605340b64
|
4
|
+
data.tar.gz: a74ed3e0f545bf21d00d13516dbef93208505cce
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 61fc69e98e7cb667fe5504241fce7f4f9da975d827fa4e7290b4a2a062bb52e0b9226030833ceeba986f1b943242fb9b2c985ca75b51a630ae11f6c326180f8d
|
7
|
+
data.tar.gz: fd33228e8d8080634a9eb785aa272f1f1cde53417f104d238fc78467f3ac30ba6394a49909cfa06561424a180ba64dc047297904af93ef06f652cdb2ee764b83
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 JuanitoFatas
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# ActiveLoader
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/active_loader.svg)](https://badge.fury.io/rb/active_loader)
|
4
|
+
[![Build Status](https://travis-ci.org/JuanitoFatas/active_loader.svg?branch=master)](https://travis-ci.org/JuanitoFatas/active_loader)
|
5
|
+
|
6
|
+
Load YAML and JSON easily and safely with Active Loader.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem "active_loader"
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install active_loader
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
yaml_content = IO.read("./.travis.yml")
|
28
|
+
json_content = IO.read("spec/fixtures/github/users/juanitofatas.json")
|
29
|
+
|
30
|
+
# Load YAML content
|
31
|
+
ActiveLoader.load yaml_content, type: :yaml
|
32
|
+
|
33
|
+
# Load JSON content
|
34
|
+
ActiveLoader.load json_content, type: :json
|
35
|
+
|
36
|
+
# Use convenient methods
|
37
|
+
ActiveLoader.yaml yaml_content
|
38
|
+
ActiveLoader.json json_content
|
39
|
+
|
40
|
+
# Can also pass in URL
|
41
|
+
ActiveLoader.yaml "https://raw.githubusercontent.com/JuanitoFatas/active_loader/master/.travis.yml"
|
42
|
+
ActiveLoader.json "https://api.github.com/repos/juanitofatas/active_loader"
|
43
|
+
|
44
|
+
# Can also pass in file path
|
45
|
+
ActiveLoader.yaml "./.travis.yml"
|
46
|
+
ActiveLoader.json "spec/fixtures/github/users/juanitofatas.json"
|
47
|
+
```
|
48
|
+
|
49
|
+
## Development
|
50
|
+
|
51
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/hack` for an interactive prompt that will allow you to experiment.
|
52
|
+
|
53
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/JuanitoFatas/active_loader.
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
62
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "active_loader/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_loader"
|
8
|
+
spec.version = ActiveLoader::VERSION
|
9
|
+
spec.authors = ["JuanitoFatas"]
|
10
|
+
spec.email = ["katehuang0320@gmail.com"]
|
11
|
+
spec.summary = %q{Load YAML and JSON easily and safely with Active Loader.}
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/JuanitoFatas/active_loader"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "http"
|
21
|
+
end
|
data/bin/hack
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_loader"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
require "irb"
|
10
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_loader/version"
|
4
|
+
require "active_loader/content_loader"
|
5
|
+
require "active_loader/loader_dispatcher"
|
6
|
+
|
7
|
+
module ActiveLoader
|
8
|
+
def self.load(path, type:)
|
9
|
+
meta_load(path, type)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.yaml(path)
|
13
|
+
meta_load(path, :yaml)
|
14
|
+
end
|
15
|
+
singleton_class.send :alias_method, :yml, :yaml
|
16
|
+
|
17
|
+
def self.json(path)
|
18
|
+
meta_load(path, :json)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.meta_load(path, type)
|
22
|
+
content = ContentLoader.new(path).call
|
23
|
+
|
24
|
+
LoaderDispatcher.new(content, type).call
|
25
|
+
end
|
26
|
+
private_class_method :meta_load
|
27
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveLoader
|
4
|
+
class ContentLoader
|
5
|
+
def initialize(path)
|
6
|
+
@path = path
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
return default_content unless path
|
11
|
+
|
12
|
+
if url?
|
13
|
+
require "http"
|
14
|
+
HTTP.get(path).to_s
|
15
|
+
elsif file?
|
16
|
+
IO.read(path)
|
17
|
+
else
|
18
|
+
path
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_reader :path
|
25
|
+
|
26
|
+
def default_content
|
27
|
+
"{}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def url?
|
31
|
+
return false if path.empty?
|
32
|
+
|
33
|
+
require "uri"
|
34
|
+
!!URI.parse(path).scheme
|
35
|
+
rescue URI::BadURIError, URI::InvalidURIError
|
36
|
+
false
|
37
|
+
end
|
38
|
+
|
39
|
+
def file?
|
40
|
+
File.file?(path)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "active_loader/parse_error"
|
5
|
+
|
6
|
+
module ActiveLoader
|
7
|
+
class JsonLoader
|
8
|
+
def initialize(raw_content)
|
9
|
+
@raw_content = raw_content.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
safe_load
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :raw_content
|
19
|
+
|
20
|
+
def safe_load
|
21
|
+
JSON.parse(raw_content)
|
22
|
+
rescue JSON::ParserError => exception
|
23
|
+
raise ActiveLoader::ParseError.new(exception.message, loader_name: self.class.to_s)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActiveLoader
|
2
|
+
UnknownTypeError = Class.new(TypeError)
|
3
|
+
|
4
|
+
class LoaderDispatcher
|
5
|
+
def initialize(content, type)
|
6
|
+
@content = content
|
7
|
+
@type = type
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
if type == :json
|
12
|
+
load_json
|
13
|
+
elsif [:yaml, :yml].include?(type)
|
14
|
+
load_yaml
|
15
|
+
else
|
16
|
+
raise UnknownTypeError, "Unknown type of file: #{type}."
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :content, :type
|
23
|
+
|
24
|
+
def load_yaml
|
25
|
+
require "active_loader/yaml_loader"
|
26
|
+
YamlLoader.new(content).call
|
27
|
+
end
|
28
|
+
|
29
|
+
def load_json
|
30
|
+
require "active_loader/json_loader"
|
31
|
+
JsonLoader.new(content).call
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "yaml"
|
4
|
+
require "active_loader/parse_error"
|
5
|
+
|
6
|
+
module ActiveLoader
|
7
|
+
class YamlLoader
|
8
|
+
def initialize(raw_content)
|
9
|
+
@raw_content = raw_content.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
ensure_correct_type(safe_load)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :raw_content
|
19
|
+
|
20
|
+
def safe_load
|
21
|
+
YAML.safe_load(raw_content)
|
22
|
+
rescue Psych::Exception, Psych::SyntaxError => exception
|
23
|
+
raise ActiveLoader::ParseError.new(exception.message, loader_name: klass_name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def klass_name
|
27
|
+
@_klass_name ||= self.class.to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
def ensure_correct_type(loaded_content)
|
31
|
+
return nil if loaded_content.to_s.empty?
|
32
|
+
|
33
|
+
if loaded_content.is_a? Hash
|
34
|
+
loaded_content
|
35
|
+
else
|
36
|
+
raise ActiveLoader::ParseError.new("Must be a hash.", loader_name: klass_name)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_loader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- JuanitoFatas
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Load YAML and JSON easily and safely with Active Loader.
|
28
|
+
email:
|
29
|
+
- katehuang0320@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".travis.yml"
|
37
|
+
- CHANGELOG.md
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- active_loader.gemspec
|
43
|
+
- bin/hack
|
44
|
+
- bin/setup
|
45
|
+
- lib/active_loader.rb
|
46
|
+
- lib/active_loader/content_loader.rb
|
47
|
+
- lib/active_loader/json_loader.rb
|
48
|
+
- lib/active_loader/loader_dispatcher.rb
|
49
|
+
- lib/active_loader/parse_error.rb
|
50
|
+
- lib/active_loader/version.rb
|
51
|
+
- lib/active_loader/yaml_loader.rb
|
52
|
+
homepage: https://github.com/JuanitoFatas/active_loader
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.6.4
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Load YAML and JSON easily and safely with Active Loader.
|
76
|
+
test_files: []
|