genny 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 775a8ea859c1d9e327238c283dccee5bc11018bf
4
- data.tar.gz: 683a7a9a6549e6accbb29e87ab497a55db71d157
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ M2VkZDQ5YTM3M2RhMjU3YTRlOWI4ODM1NGNmNDE1ODg4YzM0NjkxZg==
5
+ data.tar.gz: !binary |-
6
+ ZDNhNWE1MWUwZDU5OWIxNWUzNWIwMTk4NzM3YmU3ZWMxNzE1NTJjZA==
5
7
  SHA512:
6
- metadata.gz: b12781c213a72a7f8ba5fb27a61195162d93f7ff8ee4f3ecbc1a315e975c2aabc134398b42be3c1d64de6098768e28469bdf8ec03c0efae6370857595872196b
7
- data.tar.gz: baf4d7cecd6da46d907f45ab14c5e30c08606d9b46d96f30368663d0fe62c282d22c6bdd2e22352794ec50f44ab6c1dce43b55322c881e0d8bff222d29fd312d
8
+ metadata.gz: !binary |-
9
+ MjNhZTA3NDBlMjJkYjVkMTY1OWI5MDBmMTQ2M2E5NDFjY2NiMmRkZDhmOWQ3
10
+ NTgzMWVmMDQwMjczZjU1ZGM0ZTI3ODk1YTE1OGE0MWRlZjk3NmM3OGM4NTky
11
+ MjMxM2NlOWRhODU3OWM1M2FjMGUxNDU3MTg4MzQxNDQ5YmQyYTQ=
12
+ data.tar.gz: !binary |-
13
+ MTMzNmIwOTBlNjZlNzNiNmZjMzFlNWJmNWNiMTRlNGFlNDlhZGM0ZjYxZjFi
14
+ MDVhMTMwNzYyZGE1ZTU0ZmM0MjQyODNjNjIxNDdhYmNlNjY1NTc2YmU5MGEy
15
+ NTE4Y2YwMTNjZDJjNDAxNTBkMjQxNDk4YTk0OTFkOWQxYThjZjM=
data/README.md CHANGED
@@ -64,13 +64,25 @@ js.genny
64
64
 
65
65
  ### Arrays
66
66
 
67
- A generated array will always be empty unless a `:classes` option has been defined. Any class in that array that responds to `genny` may be picked as the prototype for an element of the generated array.
67
+ A generated array will always be empty unless a `:items` option has been defined. Any class in that array that responds to `genny` may be picked as the prototype for an element of the generated array.
68
68
 
69
69
  ```
70
70
  Genny::Array.genny
71
71
  # => []
72
72
 
73
- Genny::Array.genny(classes: [Genny::String, Genny::Integer])
73
+ Genny::Array.genny(items: [Genny::String, Genny::Integer])
74
+ # => ["mcztjgoriq", "nohfcavjyz", 739]
75
+
76
+ Genny::Array.genny(
77
+ items: JSONSchema.new(
78
+ "type" => "object",
79
+ "properties" => {
80
+ "key" => {
81
+ "type" => "string"
82
+ }
83
+ }
84
+ )
85
+ )
74
86
  # => ["mcztjgoriq", "nohfcavjyz", 739]
75
87
  ```
76
88
 
@@ -178,4 +190,6 @@ Yes please! Pull requests would be lovely.
178
190
 
179
191
  ## Contact
180
192
 
181
- I am [@jphastings](https://twitter.com/jphastings) and I work at [blinkbox Books](https://github.com/blinkboxbooks), do get in touch.
193
+ I am [@jphastings](https://twitter.com/jphastings) and I work at [blinkbox Books](https://github.com/blinkboxbooks), do get in touch.
194
+
195
+ Also, let's have none of this gif/jif nonsense. Unless it sounds *ugly* in your language, you have just finished reading the documentation for **/jeni/** ;)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/lib/genny/array.rb CHANGED
@@ -2,18 +2,37 @@ require "genny/base"
2
2
 
3
3
  module Genny
4
4
  module Array
5
+ # Generates an array of items of the types specified.
6
+ #
7
+ # @example No classes specified, can only return an empty array
8
+ # Genny::Array.genny
9
+ # # => []
10
+ #
11
+ # @example Specifying classes
12
+ # Genny::Array.genny(items: [Genny::Integer, JSONSchema.new("type" => "string", "format" => "ipv4")])
13
+ # # => ["mcztjgoriq", "nohfcavjyz", 739]
14
+ #
15
+ # @param [Hash] opts Options for the generator
16
+ # @option opts [Array<#genny>,#genny] :items The items which should be used to populate the array
17
+ # @option opts [Integer] :minItems (1) The fewest number of items the array should have
18
+ # @option opts [Integer] :maxItems (5) The largest number of items the array should have
19
+ # @raise RuntimeError if no :items given and minItems > 0
20
+ # @raise RuntimeError if :maxItems is lower than :minItems
21
+ # @return [Array]
5
22
  def self.genny(opts = {})
6
23
  opts = Genny.symbolize(opts)
7
- opts[:classes] ||= []
8
- raise ArgumentError, "classes must be an array" unless opts[:classes].respond_to?(:select)
9
- klasses = [*opts[:classes]].select { |klass| klass.respond_to?(:genny) }
10
- return [] if klasses.empty?
24
+ opts[:items] = opts[:items].is_a?(Array) ? opts[:items] : [opts[:items]].compact
25
+ raise ArgumentError, "classes must be an array" unless opts[:items].respond_to?(:select)
26
+ items = opts[:items].select { |item| item.respond_to?(:genny) }
27
+ if items.empty?
28
+ raise "No items given, cannot populate the array." unless opts[:minItems].to_i == 0
29
+ return []
30
+ end
11
31
  min_count = opts[:minItems] || 1
12
- max_count = opts[:maxItems] || 5
32
+ max_count = opts[:maxItems] || [opts[:minItems], 5].max
33
+ raise "maxItems is lower than minItems" if max_count < min_count
13
34
  count = Random.rand(max_count - min_count + 1) + min_count
14
- return count.times.map do
15
- klasses.sample.genny(opts[:items] || {})
16
- end
35
+ return count.times.map { items.sample.genny }
17
36
  end
18
37
  end
19
38
  end
@@ -1,35 +1,38 @@
1
1
  begin
2
2
  require "faker"
3
3
 
4
- module Genny::String
5
- # First name
6
- hint do |opts|
7
- hint = opts[:hint].downcase
8
- next unless hint.match("first") && hint.match("name")
9
- # TODO: ensure the length limits are met
10
- Faker::Name.first_name
11
- end
4
+ # First name
5
+ Genny::String.hint do |opts|
6
+ hint = opts[:hint].downcase
7
+ next unless hint.match("first") && hint.match("name")
8
+ # TODO: ensure the length limits are met
9
+ Faker::Name.first_name
10
+ end
12
11
 
13
- # Last name
14
- hint do |opts|
15
- hint = opts[:hint].downcase
16
- next unless hint.match("last") && hint.match("name")
17
- # TODO: ensure the length limits are met
18
- Faker::Name.last_name
19
- end
12
+ # Last name
13
+ Genny::String.hint do |opts|
14
+ hint = opts[:hint].downcase
15
+ next unless hint.match("last") && hint.match("name")
16
+ # TODO: ensure the length limits are met
17
+ Faker::Name.last_name
18
+ end
20
19
 
21
- # Full name
22
- hint do |opts|
23
- next unless opts[:hint].downcase.match("name")
24
- # TODO: ensure the length limits are met
25
- Faker::Name.name
26
- end
20
+ # Full name
21
+ Genny::String.hint do |opts|
22
+ next unless opts[:hint].downcase.match("name")
23
+ # TODO: ensure the length limits are met
24
+ Faker::Name.name
25
+ end
26
+
27
+ # Email address
28
+ Genny::String.hint do |opts|
29
+ next unless opts[:hint].downcase.match("email")
30
+ Faker::Internet.safe_email
31
+ end
27
32
 
28
- # Email address
29
- hint do |opts|
30
- next unless opts[:hint].downcase.match("email")
31
- Faker::Internet.safe_email
32
- end
33
+ # Email address as format
34
+ Genny::String.format("email") do |opts|
35
+ Faker::Internet.safe_email
33
36
  end
34
37
  rescue LoadError
35
38
  end
@@ -15,8 +15,8 @@ class JSONSchema < SimpleDelegator
15
15
  super(hash)
16
16
  end
17
17
 
18
- def genny
19
- opts = Genny.symbolize(self)
18
+ def genny(additional_opts = {})
19
+ opts = Genny.symbolize(self.merge(additional_opts))
20
20
  return opts[:enum].sample if opts.has_key?(:enum)
21
21
  return schema_from_ref(opts[:'$ref']).genny if opts[:'$ref']
22
22
 
data/lib/genny/string.rb CHANGED
@@ -32,5 +32,6 @@ module Genny
32
32
  format("ipv4") { |opts| 4.times.map { Random.rand(255) }.join(".") }
33
33
  format("ipv6") { |opts| 8.times.map { Random.rand(65536).to_s(16).rjust(4, "0") }.join(":") }
34
34
  format("uri") { |opts| Genny::URI.genny(opts).to_s }
35
+ format("email") { |opts| Genny::String.genny + "@example.com" }
35
36
  end
36
37
  end
data/lib/genny/uri.rb CHANGED
@@ -3,6 +3,8 @@ require "uri"
3
3
 
4
4
  module Genny
5
5
  module URI
6
+ # @param [Hash] opts Options for the generator
7
+ # @option opts [String] :host ('example.com') The host of the generated URI
6
8
  def self.genny(opts = {})
7
9
  opts = Genny.symbolize(opts)
8
10
  ::URI::HTTP.build(host: opts[:host] || 'example.com', path: "/#{Genny::String.genny}")
data/lib/genny/version.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Genny
2
+ # Grabbed from the VERSION file in the root of the gem
2
3
  VERSION = begin
3
4
  File.read(File.join(__dir__, "../../../VERSION")).strip
4
5
  rescue Errno::ENOENT
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genny
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Hastings-Spital
@@ -14,42 +14,56 @@ dependencies:
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '3.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: simplecov
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - ! '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json-schema
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
+ - - ! '>='
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  description: Genny likes to make things up. It generates ruby objects, mainly from
@@ -78,7 +92,7 @@ files:
78
92
  - lib/genny/time.rb
79
93
  - lib/genny/uri.rb
80
94
  - lib/genny/version.rb
81
- homepage: ''
95
+ homepage: https://github.com/blinkboxbooks/genny
82
96
  licenses: []
83
97
  metadata: {}
84
98
  post_install_message:
@@ -87,17 +101,17 @@ require_paths:
87
101
  - lib
88
102
  required_ruby_version: !ruby/object:Gem::Requirement
89
103
  requirements:
90
- - - ">="
104
+ - - ! '>='
91
105
  - !ruby/object:Gem::Version
92
106
  version: '0'
93
107
  required_rubygems_version: !ruby/object:Gem::Requirement
94
108
  requirements:
95
- - - ">="
109
+ - - ! '>='
96
110
  - !ruby/object:Gem::Version
97
111
  version: '0'
98
112
  requirements: []
99
113
  rubyforge_project:
100
- rubygems_version: 2.2.2
114
+ rubygems_version: 2.4.2
101
115
  signing_key:
102
116
  specification_version: 4
103
117
  summary: Genny likes to make things up. It generates ruby objects, mainly from JSON