rosetta-stone 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +21 -0
- data/README.md +158 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rosetta/csv_serializer.rb +41 -0
- data/lib/rosetta/deserializers.rb +55 -0
- data/lib/rosetta/element.rb +30 -0
- data/lib/rosetta/exceptions.rb +13 -0
- data/lib/rosetta/json_deserializer.rb +38 -0
- data/lib/rosetta/serializers.rb +56 -0
- data/lib/rosetta/translation.rb +60 -0
- data/lib/rosetta/version.rb +3 -0
- data/lib/rosetta.rb +9 -0
- data/rosetta.gemspec +27 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e3cf674dc5b340ddd9571dd573754c8053b9bb979b53cb29724c0614f3a97ca5
|
4
|
+
data.tar.gz: 9ffb03bc8efa51760fa3f9ca970fc9dd99c5041360dae61dd1ad35382bc903f9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0c9d3c191c137f481b690e91803b8e8ea80a2a47c298c66c3f0f184de06f5e91c5c9e1d9731dfd25e9e129e7c7caa40ceb25202260e0d6992ebcbbcf2d17c633
|
7
|
+
data.tar.gz: e474a60fe27c7c91e78282b4598d6de4e739f267e64799b92f2a50118385c3d06b01decfcbe5a143164062f901e2a791b7fed9833cf548fe32f04b3fb30b7ca0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at jeremie.bonal@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rosetta-stone (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
byebug (11.0.1)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
rake (10.5.0)
|
12
|
+
rspec (3.8.0)
|
13
|
+
rspec-core (~> 3.8.0)
|
14
|
+
rspec-expectations (~> 3.8.0)
|
15
|
+
rspec-mocks (~> 3.8.0)
|
16
|
+
rspec-core (3.8.2)
|
17
|
+
rspec-support (~> 3.8.0)
|
18
|
+
rspec-expectations (3.8.4)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.8.0)
|
21
|
+
rspec-mocks (3.8.1)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.8.0)
|
24
|
+
rspec-support (3.8.2)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
bundler (~> 2.0)
|
31
|
+
byebug
|
32
|
+
rake (~> 10.0)
|
33
|
+
rosetta-stone!
|
34
|
+
rspec (~> 3.0)
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Jérémie Bonal
|
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,158 @@
|
|
1
|
+
# Rosetta
|
2
|
+
|
3
|
+
A lightweight format-to-format translator.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rosetta'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rosetta
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### `Rosetta.translate`
|
24
|
+
|
25
|
+
The easiest way to use the library is to `require 'rosetta'` first, and then
|
26
|
+
where needed:
|
27
|
+
```ruby
|
28
|
+
Rosetta.translate(my_input_text, from: <input_format>, to: <output_format>)`
|
29
|
+
```
|
30
|
+
Example:
|
31
|
+
```ruby
|
32
|
+
input_text = JSON.parse(File.open('example.json'))
|
33
|
+
Rosetta.translate(input_text, from: :json, to: :csv)
|
34
|
+
```
|
35
|
+
|
36
|
+
### `Rosetta::Translation`
|
37
|
+
|
38
|
+
For more flexibility you can also use `Rosetta::Translation` to create callable
|
39
|
+
objects that will perform the translation. Example:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
translation = Rosetta::Translation.new(:json, :csv)
|
43
|
+
input_text = JSON.parse(File.open('example.json'))
|
44
|
+
translation.call(input_text)
|
45
|
+
|
46
|
+
# or
|
47
|
+
|
48
|
+
json_files = [ # ... ]
|
49
|
+
translation = Rosetta::Translation.new(:json, :csv)
|
50
|
+
json_files.map(&translation)
|
51
|
+
|
52
|
+
```
|
53
|
+
|
54
|
+
The `Rosetta.translate` method and the `Rosetta::Translation.new` method both
|
55
|
+
can take callable objects instead of formats. The first one will be ued to
|
56
|
+
deserialize/parse the input, the second one to serialize/generate the output.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
deserializer = -> (input) { input.reverse }
|
60
|
+
serializer = -> (input) { input.upcase }
|
61
|
+
Rosetta::Translation.new(deserializer, serializer).call("olleh") # => HELLO
|
62
|
+
```
|
63
|
+
|
64
|
+
When given `Symbol`s instead, they will try to lookup the registered serializers
|
65
|
+
and deserializers to find a match to use with the input.
|
66
|
+
By default, the only ones supplied with the gem are a JSON deserializer and a
|
67
|
+
CSV serializer.
|
68
|
+
See *Registering a Serializer / Deserializer* for more info on how to add your own.
|
69
|
+
|
70
|
+
You can also register `Translator`s, which are pipes connecting to specific
|
71
|
+
formats rather than using a Deserializer + Serializer combo.
|
72
|
+
See *Registering a Translator* for more info on how to use them.
|
73
|
+
|
74
|
+
### Registering a Serializer / Deserializer
|
75
|
+
|
76
|
+
You can register a new serializer or deserializer by calling the `register`
|
77
|
+
methods of respectively `Rosetta::Serializers` or `Rosetta::Deserializers`.
|
78
|
+
The method takes the Symbol that's going to be used as shorthand for it as first
|
79
|
+
parameter and the callable object you want to use as second.
|
80
|
+
|
81
|
+
Example:
|
82
|
+
```ruby
|
83
|
+
deserializer = -> (input) { input.reverse }
|
84
|
+
serializer = -> (input) { input.upcase }
|
85
|
+
|
86
|
+
Rosetta::Deserializers.register(:mirror, deserializer)
|
87
|
+
Rosetta::Serializers.register(:BIG, serializer)
|
88
|
+
|
89
|
+
Rosetta.translate(from: :mirror, to: :big, "em ti si") # => "IS IT ME"
|
90
|
+
```
|
91
|
+
|
92
|
+
Whatever the Deserializer returns will be fed to the Serializer when
|
93
|
+
translating, but to improve reusability and a "grab bag" approach to them, it's
|
94
|
+
recommended to have Deserializers return a collection of `Rosetta::Element`s and
|
95
|
+
to have Serializers assume they are receiving one as parameter.
|
96
|
+
|
97
|
+
`Rosetta::Element` are wrapper objects around what's been given to their
|
98
|
+
constructor, traditionally a Hash or nested Hashes.
|
99
|
+
They can introspect and return a flat list of all the keys in their content, and
|
100
|
+
they respond to `[]` to access the values associated with those keys.
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
simple = Rosetta::Element.new({ place: 'My home', time: 'Later today', activity: 'Chilling' })
|
104
|
+
.properties # => [:place, :time, :activity]
|
105
|
+
|
106
|
+
simple['place'] # => "My home"
|
107
|
+
|
108
|
+
nested = Rosetta::Element.new({ place: { country: 'USA', state: 'West Virginia', town: 'Kepler' },
|
109
|
+
time: { day: 'Thursday' } })
|
110
|
+
.properties # => ['place.country', 'place.state', 'place.town', 'time.day']
|
111
|
+
|
112
|
+
nested['place.town'] # => "Kepler"
|
113
|
+
|
114
|
+
```
|
115
|
+
|
116
|
+
### Registering a Translator
|
117
|
+
|
118
|
+
For many reasons (performance, development time, cohabitation with other libraries, ...)
|
119
|
+
you might want to avoid developing and using a Deserializer and a Serializer and
|
120
|
+
would rather prefer going straight from a specific format to another specific
|
121
|
+
one.
|
122
|
+
|
123
|
+
The way to do so is to register a `Translator` by calling
|
124
|
+
`Rosetta::Translation.register`.
|
125
|
+
The method takes the source and destination formats as the first two parameters and either
|
126
|
+
a callable object or a block as the translator.
|
127
|
+
|
128
|
+
Example:
|
129
|
+
```ruby
|
130
|
+
# Either
|
131
|
+
translator = lambda do |input|
|
132
|
+
input.upcase
|
133
|
+
end
|
134
|
+
Rosetta::Translation.register(:down, :up, translator)
|
135
|
+
# or
|
136
|
+
Rosetta::Translation.register(:down, :up) { |input| input.upcase }
|
137
|
+
# are equivalent.
|
138
|
+
|
139
|
+
Rosetta::Translation.new(:down, :up).call("you're looking for")
|
140
|
+
# => "YOU'RE LOOKING FOR3
|
141
|
+
|
142
|
+
## Development
|
143
|
+
|
144
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
145
|
+
|
146
|
+
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).
|
147
|
+
|
148
|
+
## Contributing
|
149
|
+
|
150
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Aquaj/rosetta. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
151
|
+
|
152
|
+
## License
|
153
|
+
|
154
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
155
|
+
|
156
|
+
## Code of Conduct
|
157
|
+
|
158
|
+
Everyone interacting in the Rosetta project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Aquaj/rosetta/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rosetta"
|
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
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
require 'rosetta/exceptions'
|
4
|
+
require 'rosetta/serializers'
|
5
|
+
|
6
|
+
module Rosetta
|
7
|
+
class CSVSerializer < Serializers::Base
|
8
|
+
def call
|
9
|
+
CSV.generate do |csv|
|
10
|
+
csv << headers
|
11
|
+
elements.each do |element|
|
12
|
+
csv << headers.map { |header| serialize_value(element[header]) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def headers
|
18
|
+
head, *_ = elements.map(&:properties).uniq
|
19
|
+
head
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate_input!
|
23
|
+
_, *others = elements.map(&:properties).uniq
|
24
|
+
|
25
|
+
raise SerializationError, <<-ERROR.strip unless others.none?
|
26
|
+
All objects need to share their structure to be serialized to CSV.
|
27
|
+
ERROR
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def serialize_value(value)
|
33
|
+
case value
|
34
|
+
when Array
|
35
|
+
value.join(',')
|
36
|
+
else
|
37
|
+
value
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rosetta/exceptions'
|
2
|
+
|
3
|
+
module Rosetta
|
4
|
+
class Deserializers
|
5
|
+
@registered = {}
|
6
|
+
|
7
|
+
def self.[](key)
|
8
|
+
@registered[key]
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.register(name, deserializer)
|
12
|
+
raise ExistingDeserializerError, <<-ERROR.strip if @registered.key? name
|
13
|
+
Deserializer #{name} is already registered.
|
14
|
+
ERROR
|
15
|
+
@registered[name] = deserializer
|
16
|
+
end
|
17
|
+
|
18
|
+
class Base
|
19
|
+
attr_reader :input
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def inherited(new_serializer)
|
23
|
+
key = new_serializer.name.match(/^(.*?)(Deserializer)?$/)[1]
|
24
|
+
key = key.split("::").last
|
25
|
+
#NOTE: Similar to Rails's #underscore
|
26
|
+
#TODO: Extract in refinement?
|
27
|
+
key = key.scan(/[A-Z]+[a-z]*/).join('_').downcase.to_sym
|
28
|
+
Deserializers.register(key, new_serializer)
|
29
|
+
end
|
30
|
+
|
31
|
+
def call(input)
|
32
|
+
new(input).call
|
33
|
+
end
|
34
|
+
alias_method :deserialize, :call
|
35
|
+
|
36
|
+
def to_proc
|
37
|
+
proc { |*args, &block| self.call(*args, &block) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(input)
|
42
|
+
@input = input.dup.freeze
|
43
|
+
end
|
44
|
+
|
45
|
+
def call
|
46
|
+
raise NotImplementedError
|
47
|
+
end
|
48
|
+
alias_method :deserialize, :call
|
49
|
+
|
50
|
+
def to_proc
|
51
|
+
proc { |*args, &block| self.call(*args, &block) }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Rosetta
|
2
|
+
class Element
|
3
|
+
attr_reader :object
|
4
|
+
|
5
|
+
def initialize(object)
|
6
|
+
@object = object
|
7
|
+
end
|
8
|
+
|
9
|
+
def properties
|
10
|
+
properties_of(object)
|
11
|
+
end
|
12
|
+
|
13
|
+
def [](key)
|
14
|
+
object.dig(*key.split("."))
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def properties_of(obj)
|
20
|
+
obj.flat_map do |key, val|
|
21
|
+
case val
|
22
|
+
when Hash
|
23
|
+
properties_of(val).map{ |header| [key, header].join('.') }
|
24
|
+
else
|
25
|
+
key
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Rosetta
|
2
|
+
class ConversionError < StandardError; end
|
3
|
+
class SerializationError < ConversionError; end
|
4
|
+
class DeserializationError < ConversionError; end
|
5
|
+
|
6
|
+
class RegistrationError < StandardError; end
|
7
|
+
class ExistingTranslatorError < RegistrationError; end
|
8
|
+
class ExistingSerializerError < RegistrationError; end
|
9
|
+
class ExistingDeserializerError < RegistrationError; end
|
10
|
+
|
11
|
+
class UnknownSerializer < StandardError; end
|
12
|
+
class UnknownDeserializer < StandardError; end
|
13
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
require 'rosetta/element'
|
4
|
+
require 'rosetta/exceptions'
|
5
|
+
require 'rosetta/deserializers'
|
6
|
+
|
7
|
+
module Rosetta
|
8
|
+
class JSONDeserializer < Deserializers::Base
|
9
|
+
def call
|
10
|
+
validate_input!
|
11
|
+
input.map { |obj| Element.new(obj) }
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def validate_input!
|
17
|
+
raise DeserializationError, <<-ERROR.strip unless parsed_input = valid_json(@input)
|
18
|
+
JSON input is invalid
|
19
|
+
ERROR
|
20
|
+
raise DeserializationError, <<-ERROR.strip unless parsed_input.is_a? Array
|
21
|
+
JSON input must be an array
|
22
|
+
ERROR
|
23
|
+
raise DeserializationError, <<-ERROR.strip unless parsed_input.all? { |o| o.is_a? Hash }
|
24
|
+
JSON input must contain objects
|
25
|
+
ERROR
|
26
|
+
|
27
|
+
@input = parsed_input.freeze
|
28
|
+
end
|
29
|
+
|
30
|
+
#HACK: Feels dirty but there's no JSON soft-parsing in ruby's json lib
|
31
|
+
def valid_json(json)
|
32
|
+
JSON(json)
|
33
|
+
#NOTE: Rescuing TypeError too in case json is not a String
|
34
|
+
rescue JSON::ParserError, TypeError
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rosetta/exceptions'
|
2
|
+
|
3
|
+
module Rosetta
|
4
|
+
class Serializers
|
5
|
+
@registered = {}
|
6
|
+
|
7
|
+
def self.[](key)
|
8
|
+
@registered[key]
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.register(name, serializer)
|
12
|
+
raise ExistingSerializerError, <<-ERROR.strip if @registered.key? name
|
13
|
+
Serializer #{name} is already registered.
|
14
|
+
ERROR
|
15
|
+
@registered[name] = serializer
|
16
|
+
end
|
17
|
+
|
18
|
+
class Base
|
19
|
+
attr_reader :elements
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def inherited(new_serializer)
|
23
|
+
key = new_serializer.name.match(/^(.*?)(Serializer)?$/)[1]
|
24
|
+
key = key.split("::").last
|
25
|
+
#NOTE: Similar to Rails's #underscore
|
26
|
+
#TODO: Extract in refinement?
|
27
|
+
key = key.scan(/[A-Z]+[a-z]*/).join('_').downcase.to_sym
|
28
|
+
Serializers.register(key, new_serializer)
|
29
|
+
end
|
30
|
+
|
31
|
+
def call(elements)
|
32
|
+
new(elements).call
|
33
|
+
end
|
34
|
+
alias_method :serialize, :call
|
35
|
+
|
36
|
+
def to_proc
|
37
|
+
proc { |*args, &block| self.call(*args, &block) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(elements)
|
42
|
+
@elements = elements.dup.freeze
|
43
|
+
validate_input!
|
44
|
+
end
|
45
|
+
|
46
|
+
def call
|
47
|
+
raise NotImplementedError
|
48
|
+
end
|
49
|
+
alias_method :serialize, :call
|
50
|
+
|
51
|
+
def to_proc
|
52
|
+
proc { |*args, &block| self.call(*args, &block) }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rosetta/exceptions'
|
2
|
+
|
3
|
+
module Rosetta
|
4
|
+
class Translation
|
5
|
+
@registered = {}
|
6
|
+
|
7
|
+
attr_reader :serializer, :deserializer, :translator
|
8
|
+
alias_method :translator?, :translator
|
9
|
+
|
10
|
+
def self.register(source, destination, callable = nil, &block)
|
11
|
+
raise ExistingTranslatorError, <<-ERROR.strip if @registered.key? name
|
12
|
+
There already is a translator from #{source} to #{destination}.
|
13
|
+
ERROR
|
14
|
+
|
15
|
+
raise ArgumentError, "Can't take both callabel object and block." if callable && block
|
16
|
+
@registered[source => destination] = callable || block
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.[](key)
|
20
|
+
@registered[key]
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(deserializer, serializer)
|
24
|
+
@translator = Translation[deserializer => serializer]
|
25
|
+
unless @translator
|
26
|
+
@serializer = find_serializer(serializer)
|
27
|
+
@deserializer = find_deserializer(deserializer)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def call(input)
|
32
|
+
if translator?
|
33
|
+
translator.call(input)
|
34
|
+
else
|
35
|
+
elements = deserializer.call(input)
|
36
|
+
serializer.call(elements)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_proc
|
41
|
+
proc { |*args, &block| self.call(*args, &block) }
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def find_serializer(name_or_object)
|
47
|
+
is_name = name_or_object.is_a?(String) || name_or_object.is_a?(Symbol)
|
48
|
+
object = name_or_object unless is_name
|
49
|
+
Serializers[name_or_object] || object ||
|
50
|
+
raise(UnknownSerializer, "Unknown serializer for: #{name_or_object.inspect}")
|
51
|
+
end
|
52
|
+
|
53
|
+
def find_deserializer(name_or_object)
|
54
|
+
is_name = name_or_object.is_a?(String) || name_or_object.is_a?(Symbol)
|
55
|
+
object = name_or_object unless is_name
|
56
|
+
Deserializers[name_or_object] || object ||
|
57
|
+
raise(UnknownSerializer, "Unknown serializer for: #{name_or_object.inspect}")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/rosetta.rb
ADDED
data/rosetta.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "rosetta/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rosetta-stone"
|
8
|
+
spec.version = Rosetta::VERSION
|
9
|
+
spec.authors = ["Jérémie Bonal"]
|
10
|
+
spec.email = ["jeremie.bonal@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Small library to allow for conversions between formats.}
|
13
|
+
spec.homepage = "https://github.com/Aquaj/rosetta-stone"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "byebug"
|
24
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rosetta-stone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jérémie Bonal
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: byebug
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- jeremie.bonal@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/rosetta.rb
|
88
|
+
- lib/rosetta/csv_serializer.rb
|
89
|
+
- lib/rosetta/deserializers.rb
|
90
|
+
- lib/rosetta/element.rb
|
91
|
+
- lib/rosetta/exceptions.rb
|
92
|
+
- lib/rosetta/json_deserializer.rb
|
93
|
+
- lib/rosetta/serializers.rb
|
94
|
+
- lib/rosetta/translation.rb
|
95
|
+
- lib/rosetta/version.rb
|
96
|
+
- rosetta.gemspec
|
97
|
+
homepage: https://github.com/Aquaj/rosetta-stone
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubygems_version: 3.0.3
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Small library to allow for conversions between formats.
|
120
|
+
test_files: []
|