mockingjay 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 797f689ec792fe061af0510544ecc23c09313c75
4
+ data.tar.gz: 4b73c28ce5be83118d83ced09a07faeb40af4f1d
5
+ SHA512:
6
+ metadata.gz: 1a7af94224d4cd82f6bf578511b1c8a771c330e4b91e62989e6c273474c741ef5d4a2c69cbaeff776f38592a0a517ee7979f89faab497a1340cafb5558594811
7
+ data.tar.gz: eeb1774d15a255446ef7886f737d79cfbe9661eee677a9f6b6dc561fa6624ae4a6f4728e29b926d31badb4b1ab7276cb37a6b5a9041c17c0a1d86f91dd930811
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.json
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mockingjay.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brandon Weaver
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.
@@ -0,0 +1,106 @@
1
+ # Mockingjay
2
+
3
+ Fixtures are a hastle to keep up to date, especially with an active development of the data that's being returned. Worst case, you can't predict when a little thing is going to change and it breaks all your fixtures!
4
+
5
+ Why not let the data define fixtures for you, and generate away?
6
+
7
+ Mockingjay aims to bridge that gap for you.
8
+
9
+ ## Usage
10
+
11
+ ### Mockingjay::Deserialize
12
+
13
+ Takes a JSON string and turns it into a ruby hash by calling Generator hooks in the string.
14
+
15
+ ```ruby
16
+ {
17
+ "a": {
18
+ "Generator.fixnum":"(1..100)"
19
+ },
20
+ "b": {
21
+ "c": [
22
+ {"Generator.fixnum":"(1..100)"},
23
+ {"Generator.fixnum":"(1..100)"},
24
+ {"Generator.fixnum":"(1..100)"}
25
+ ],
26
+ "d": {
27
+ "Generator.string":"Lorem.word"
28
+ },
29
+ "e": {
30
+ "f": {"Generator.float":"(1..100)"}
31
+ }
32
+ }
33
+ }
34
+ ```
35
+
36
+ Looks in Generators for matching rules, or returns that the generator is unknown.
37
+
38
+ (TODO: Date Support)
39
+
40
+ ### Mockingjay::Serialize
41
+
42
+ Takes in a Ruby hash and turns it into a set of default generators based on types. Default rulesets are configured in Rules to control this behavior
43
+
44
+ ```ruby
45
+ Mockingjay::Serialize.new({a: 1, b: {c: [1,2,3], d: 'foo!', e: { f: 1.0 } } } )
46
+ ```
47
+
48
+ ...would render the hash:
49
+
50
+ ```ruby
51
+ {
52
+ "a": {
53
+ "Generator.fixnum":"(1..100)"
54
+ },
55
+ "b": {
56
+ "c": [
57
+ {"Generator.fixnum":"(1..100)"},
58
+ {"Generator.fixnum":"(1..100)"},
59
+ {"Generator.fixnum":"(1..100)"}
60
+ ],
61
+ "d": {
62
+ "Generator.string":"Lorem.word"
63
+ },
64
+ "e": {
65
+ "f": {"Generator.float":"(1..100)"}
66
+ }
67
+ }
68
+ }
69
+ ```
70
+
71
+ ### Mockingjay::Generators
72
+
73
+ Methods for converting generator hooks into ruby values
74
+
75
+ ```ruby
76
+ def fixnum(str_range)
77
+ a, b = *str_range.split(/...?/).map(&:to_i)
78
+ b ? rand(a..b).to_i : rand(a).to_i
79
+ end
80
+ ```
81
+
82
+ When a fixnum hook is hit, such as:
83
+
84
+ ```ruby
85
+ {"Generator.fixnum":"(1..100)"}
86
+ ```
87
+
88
+ This method will be called with a string '(1..100)'
89
+
90
+ (Granted I need to fix ranges on that)
91
+
92
+ ### Mockingjay::Rules
93
+
94
+ Default Rules for serializing types of data into Generators
95
+
96
+ ```ruby
97
+ def fixnum
98
+ {'Generator.fixnum' => '(1..100)'}
99
+ end
100
+ ```
101
+
102
+ Whenever a fixnum is encountered in a raw hash, it will be substituted with this Generator hook by default.
103
+
104
+ # Notes
105
+
106
+ This was done in about a 4 hour time frame, and still has a fair amount of work to be done. It is most certainly alpha software. Be careful.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,12 @@
1
+ require 'json'
2
+ require 'faker'
3
+
4
+ module Mockingjay
5
+ # Nothing to see here folks
6
+ end
7
+
8
+ require 'mockingjay/deserialize'
9
+ require 'mockingjay/serialize'
10
+ require 'mockingjay/version'
11
+ require 'mockingjay/generators/base_generators'
12
+ require 'mockingjay/rules/base_rules'
@@ -0,0 +1,40 @@
1
+ module Mockingjay
2
+ class Deserialize
3
+ attr_reader :data
4
+
5
+ # Takes in a JSON string, and creates a new hash full of data
6
+ def initialize(raw_data)
7
+ @data = reduce_hash(JSON.parse(raw_data))
8
+ end
9
+
10
+ private
11
+
12
+ # Reduces a hash into generated values by looking for Generator hooks
13
+ def reduce_hash(data)
14
+ data.each_pair.reduce({}) { |data, (key, value)|
15
+ if (match = key.match(/Generator\.(?<f>.+)/))
16
+ generator_for(match[:f], value)
17
+ else
18
+ data.merge!({ key.to_sym => interpret(value) })
19
+ end
20
+ }
21
+ end
22
+
23
+ def reduce_array(data)
24
+ data.reduce([]) { |array, value| array << interpret(value) }
25
+ end
26
+
27
+ def interpret(value)
28
+ case
29
+ when value.class == Hash then reduce_hash(value)
30
+ when value.class == Array then reduce_array(value)
31
+ end
32
+ end
33
+
34
+ # When given a generator, if it exists it will send data to it. If not, an unknown
35
+ # generator will be called.
36
+ def generator_for(type, args = nil)
37
+ Generator.respond_to?(type) ? Generator.send(type, args) : Generator.unknown(type)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,47 @@
1
+ module Mockingjay
2
+ # Eventually this should start searching through a folder of other fixtures to
3
+ # see if they know how to make an object.
4
+ #
5
+ # Takes Generator hooks in serialized data and returns values.
6
+ module Generator
7
+ class << self
8
+ include Faker
9
+
10
+ def fixnum(str_range)
11
+ a, b = *str_range.split(/...?/).map(&:to_i)
12
+ b ? rand(a..b).to_i : rand(a).to_i
13
+ end
14
+
15
+ def float(str_range)
16
+ a, b = *str_range.split(/...?/).map(&:to_i)
17
+ b ? rand(a..b) : rand(a)
18
+ end
19
+
20
+ # Anything that you can use in faker, you can use here.
21
+ #
22
+ # https://github.com/stympy/faker
23
+ def string(type = 'Lorem.word')
24
+ self.instance_eval type
25
+ end
26
+
27
+ # Date Time will need some work, for now leaving this commented out.
28
+
29
+ # def date(str_range); time(str_range); end
30
+
31
+ # def time(str_range)
32
+ # a, b = *str_range.split('..')
33
+ # b ? time_rand(a,b) : time_rand(a)
34
+ # end
35
+
36
+ def unknown(type)
37
+ "Unknown Generator Type #{type}"
38
+ end
39
+
40
+ private
41
+
42
+ # def time_rand(from = 0.0, to = Time.now)
43
+ # Time.at(from + rand * (to.to_f - from.to_f))
44
+ # end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,23 @@
1
+ module Mockingjay
2
+
3
+ # These define basic rules for converting a deserialized value down to a generator
4
+ module Rules
5
+ class << self
6
+ def fixnum
7
+ {'Generator.fixnum' => '(1..100)'}
8
+ end
9
+
10
+ def float
11
+ {'Generator.float' => '(1..100)'}
12
+ end
13
+
14
+ def string
15
+ {'Generator.string' => 'Lorem.word'}
16
+ end
17
+
18
+ def unknown(type)
19
+ {'Generator.unknown' => '#{type}'}
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,44 @@
1
+ module Mockingjay
2
+ class Serialize
3
+ attr_reader :json
4
+
5
+ # Parses raw data to JSON
6
+ def initialize(raw_data)
7
+ @json = reduce_hash(raw_data.is_a?(Hash) ? raw_data : JSON.parse(raw_data)).to_json
8
+ end
9
+
10
+ # A bit of sugar for quicker saves
11
+ def save_as(name)
12
+ File.open("#{name}.json", 'w') { |file| file.write @json }
13
+ end
14
+
15
+ private
16
+
17
+ def reduce_hash(data)
18
+ data.each_pair.reduce({}) { |data, (key, value)| data.merge!({ key => interpret(value) }) }
19
+ end
20
+
21
+ def reduce_array(data)
22
+ data.reduce([]) { |array, value| array << interpret(value) }
23
+ end
24
+
25
+ def interpret(value)
26
+ case
27
+ when value.class == Hash then reduce_hash(value)
28
+ when value.class == Array then reduce_array(value)
29
+ else generator_for(value)
30
+ end
31
+ end
32
+
33
+ # Finds the type of a value, and turns into a symbol to hook rules
34
+ def value_type_of(value)
35
+ value.class.to_s.downcase.to_sym
36
+ end
37
+
38
+ # Finds the rule to substitute in place of a value
39
+ def generator_for(value)
40
+ type = value_type_of(value)
41
+ Generator.respond_to?(type) ? Rules.send(type) : Rules.unknown(type)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Mockingjay
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mockingjay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mockingjay"
8
+ spec.version = Mockingjay::VERSION
9
+ spec.authors = ["Brandon Weaver"]
10
+ spec.email = ["keystonelemur@gmail.com"]
11
+ spec.summary = %q{Dynamic Fixture Creation}
12
+ spec.description = %q{Let your data define your fixtures for you}
13
+ spec.homepage = "https://github.com/baweaver/mockingjay"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_development_dependency "bundler", "~> 1.4"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "faker"
25
+ spec.add_runtime_dependency "json"
26
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mockingjay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Weaver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faker
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Let your data define your fixtures for you
70
+ email:
71
+ - keystonelemur@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/mockingjay.rb
82
+ - lib/mockingjay/deserialize.rb
83
+ - lib/mockingjay/generators/base_generators.rb
84
+ - lib/mockingjay/rules/base_rules.rb
85
+ - lib/mockingjay/serialize.rb
86
+ - lib/mockingjay/version.rb
87
+ - mockingjay.gemspec
88
+ homepage: https://github.com/baweaver/mockingjay
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.0
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Dynamic Fixture Creation
112
+ test_files: []