mockolate 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af8d46e40e4912a888bd6e2fc702d57855588736f628e1b72fe8496423dadc11
4
- data.tar.gz: 7677af9ed20ef12cbd2d16b3589bbb3ee38557882ce1b3b6581d406d54497f18
3
+ metadata.gz: 5995709b20221b067022d29e769c8b71e06165f2e947d02dc58f9f5208497d33
4
+ data.tar.gz: 69678030088be972616b562c39e050bcaa7c61b7ec024fb99d0769438d6f3304
5
5
  SHA512:
6
- metadata.gz: 02ee6e2d51b825eaac270d50b51aa21e01529227fe0dbf700fc19f072d80711775faaa09a17692e8f9bd047ce699755a081f719383d7c34108790b081531726a
7
- data.tar.gz: 50b2edfa9071d1778bad47e69794d588cec66aa343e58aeb1fa40cf2a391f322a779b25d23182dba15bbec81b9fd169fb9fc0502e39706f32a9fa844b7666f7f
6
+ metadata.gz: 269f132b9bc65e7d7e2faa21d4cf63d2b2a8b56ca4e0aa18d00555e2559b3da53595782e576ddd58575f2ac8427f65bcdf0734f68ecb6e1eddb72ea99873eded
7
+ data.tar.gz: bc3a0d18e15e8d1a673e4c2411442764e8c180257535eae671d5435a9e98f4e18afcc46ed8b136a9f26c1b66bf99bc55bc93774d7edcb9f1c6cfae2491486858
@@ -2,11 +2,17 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  mockolate (0.0.0)
5
+ faker
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
10
+ concurrent-ruby (1.1.5)
9
11
  diff-lcs (1.3)
12
+ faker (2.10.0)
13
+ i18n (>= 1.6, < 1.8)
14
+ i18n (1.7.0)
15
+ concurrent-ruby (~> 1.0)
10
16
  rake (10.5.0)
11
17
  rspec (3.9.0)
12
18
  rspec-core (~> 3.9.0)
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
+
1
2
  # mockolate
2
3
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mockolate`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+
6
+ ## Description
4
7
 
5
- TODO: Delete this and the text above, and describe your gem
8
+ Mockolate is easy to us endpoint (service) faker with mock responses. It can also be used as DSL for hash
9
+
6
10
 
7
11
  ## Installation
8
12
 
@@ -14,15 +18,62 @@ gem 'mockolate'
14
18
 
15
19
  And then execute:
16
20
 
17
- $ bundle
21
+ ```
22
+ $ bundle
23
+ ```
18
24
 
19
25
  Or install it yourself as:
20
-
21
- $ gem install mockolate
26
+
27
+ ```
28
+ $ gem install mockolate
29
+ ```
22
30
 
23
31
  ## Usage
32
+
33
+ Create hash object using Request class and its dsl
34
+
35
+ ```ruby
36
+ class UserRequestParams < Mockolate::Request
37
+ payload do
38
+ string :firstname, fake_from: 'Name', key: 'first_name'
39
+ string :lastname, fake_from: 'Name', key: 'last_name'
40
+ integer :age, value: 21
41
+ array :books do
42
+ hash do
43
+ string :name, value: 'Tehlikeli Oyunlar'
44
+ string :author, value: 'Oguz Atay'
45
+ integer :published, value: 1973
46
+ end
47
+ hash do
48
+ string :name, value: '1984'
49
+ string :author, value: 'George Orwell'
50
+ integer :published, value: 1949
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ ```
57
+
58
+ You can even generate dynamic data by using `fake_from` and `key` options which using Faker gem under the hood
59
+ If you planning to access the hash with key pass `export_with` option to payload
60
+ For results use `generate_array!` or `generate_hash!` (which returns nested hash with export key) instance methods.
24
61
 
25
- TODO: Write usage instructions here
62
+ `UserRequestParams.new.generate_array!`
63
+
64
+ Consume data from remote endpoint created data by `Mockolate::Request`
65
+
66
+ ```ruby
67
+ class UserService < Mockolate::Response
68
+ object_from UserRequestParams
69
+
70
+ define_endpoint! :profile_by_email do |email|
71
+ object[email]
72
+ end
73
+ end
74
+
75
+ UserService.new.profile_by_email('johndoe@example.com')
76
+ ```
26
77
 
27
78
  ## Development
28
79
 
@@ -32,8 +83,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
83
 
33
84
  ## Contributing
34
85
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mockolate.
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/0x2C6/mockolate.
36
87
 
37
88
  ## License
38
89
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "mockolate/version"
4
- require "mockolate/parameters"
5
- require "mockolate/parameters/generator"
3
+ require 'faker'
4
+ require 'mockolate/version'
5
+ require 'mockolate/errors'
6
+ require 'mockolate/types'
7
+ require 'mockolate/register'
8
+ require 'mockolate/request'
9
+ require 'mockolate/response'
10
+ require 'mockolate/request/parser'
11
+ require 'mockolate/initializers'
6
12
 
7
13
  module Mockolate
8
- # extend ActiveSupport::Autoload
9
-
10
- # autoload :Parameters
11
14
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mockolate::Errors
4
+ class MockolateError < StandardError; end
5
+
6
+ class FakerKeyNotFound < MockolateError; end
7
+ class TypeNotFound < MockolateError; end
8
+ class MissingHashBlockError < MockolateError; end
9
+ class MissingValueError < MockolateError; end
10
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mockolate::Initializers
4
+ Mockolate::Register.set :string, Mockolate::Types::String
5
+ Mockolate::Register.set :integer, Mockolate::Types::Integer
6
+ Mockolate::Register.set :hash, Mockolate::Types::Hash
7
+ Mockolate::Register.set :array, Mockolate::Types::Array
8
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mockolate::Register
4
+ @@types = {}
5
+
6
+ def self.set(type, type_module)
7
+ @@types[type] = type_module
8
+ end
9
+
10
+ def self.get(type)
11
+ @@types[type]
12
+ end
13
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Mockolate::Request
4
+ @@public_attributes = []
5
+ @@payloads = []
6
+ @@payload_options = {}
7
+
8
+ def self.payload(options = {}, &block)
9
+ @@payloads << payload_parser(options, &block)
10
+ end
11
+
12
+ def self.payload_parser(options = {}, &block)
13
+ @@payload_options = options
14
+ dsl = DSL.new(@@payload_options)
15
+ dsl.instance_exec(&block)
16
+ @@public_attributes = dsl.attributes
17
+ end
18
+
19
+ def self.params
20
+ @@public_attributes
21
+ end
22
+
23
+ def generate_array!(count = nil)
24
+ attr_arr = @@payloads
25
+ parser = Parser.new(attr_arr)
26
+ parser.parse
27
+ end
28
+
29
+ def generate_hash!(count = nil)
30
+ attr_arr = @@payloads
31
+ parser = Parser.new(attr_arr, @@payload_options)
32
+ parser.parse
33
+ end
34
+
35
+ class DSL
36
+ attr_accessor :attributes
37
+ attr_reader :options
38
+
39
+ def initialize(options)
40
+ @attributes = []
41
+ @options = options
42
+ end
43
+
44
+ def hash(*args, &block)
45
+ raise Mockolate::Errors::MissingHashBlockError unless block_given?
46
+ method_missing(:hash, *args, &block)
47
+ end
48
+
49
+ def method_missing(type, *args, &block)
50
+ metadata = Mockolate::Register.get(type)
51
+ field_name = args.first
52
+ type_options = args[1]
53
+
54
+ attibute = metadata.create(name: field_name, options: type_options)
55
+ if block_given?
56
+ attibute.children = Mockolate::Request.payload_parser(options, &block)
57
+ end
58
+ attributes << attibute
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Mockolate::Request::Parser
4
+ attr_reader :_attr_arr
5
+ attr_reader :_export_key
6
+
7
+ def initialize(attr_arr, options = {})
8
+ @_attr_arr = attr_arr
9
+ @_export_key = options[:export_with]
10
+ end
11
+
12
+ def parse
13
+ return _parse_to_hash if _export_key
14
+ _parse_to_array
15
+ end
16
+
17
+ private
18
+ def _parse_to_hash
19
+ export_hash = {}
20
+ _attr_arr.each do |attr|
21
+ hash = {}
22
+ attr.each do |t|
23
+ hash.merge! t.parse
24
+ end
25
+ export_hash[hash[_export_key]] = hash
26
+ end
27
+ return export_hash
28
+ end
29
+
30
+ def _parse_to_array
31
+ _attr_arr.map do |attr|
32
+ _hash = Hash.new
33
+
34
+ attr.each do |t|
35
+ _hash.merge! t.parse
36
+ end
37
+ _hash
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Mockolate::Response
4
+ @@object_class = nil
5
+
6
+ def self.define_endpoint!(mtd, &block)
7
+ method(:define_method).call(mtd, &block)
8
+ end
9
+
10
+ def object
11
+ @@object_class.generate_hash!
12
+ end
13
+
14
+ def self.object_from(request_class)
15
+ raise Exception unless request_class.new.is_a? Mockolate::Request
16
+ @@object_class = request_class.new
17
+ end
18
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Mockolate::Types
4
+ def self.inherited(base)
5
+ base.extend TypeFactory
6
+ base.extend TypeMetaData
7
+ end
8
+
9
+ module TypeFactory
10
+ def create(name:, options: {})
11
+ new(
12
+ name: name,
13
+ options: options
14
+ )
15
+ end
16
+ end
17
+
18
+ module TypeMetaData
19
+ def self.extended(base)
20
+ base.class_eval do
21
+ attr_reader :name, :options
22
+ attr_accessor :children
23
+
24
+ def initialize(name:, options: nil)
25
+ @name = name
26
+ @options = options
27
+ @children = []
28
+ end
29
+
30
+ def parse
31
+ return name => value
32
+ end
33
+
34
+ def has_children?
35
+ !children.empty?
36
+ end
37
+
38
+ def value
39
+ _get_value.public_send(_cast)
40
+ end
41
+
42
+ private
43
+ def _get_value
44
+ options[:value] || _get_from_faker!
45
+ end
46
+
47
+ def _parse_child
48
+ children.flatten.map do |child|
49
+ child.parse
50
+ end
51
+ end
52
+
53
+ def _cast
54
+ class_name = self.class.name.split('::')
55
+
56
+ return "to_#{class_name.last[0].downcase}".to_sym
57
+ end
58
+
59
+ def _get_from_faker!
60
+ Object.const_get(
61
+ "Faker::#{options[:fake_from]}"
62
+ ).public_send(options[:key])
63
+ end
64
+
65
+ end
66
+ end
67
+ end
68
+
69
+ require 'mockolate/types/string'
70
+ require 'mockolate/types/integer'
71
+ require 'mockolate/types/hash'
72
+ require 'mockolate/types/array'
73
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Mockolate::Types::Array < Mockolate::Types
4
+ # Public Methods
5
+ # name: Name of the key specified in params block
6
+ # has_children: Returns children elements of hash
7
+ #
8
+ # Private Methods
9
+ # _parse_child Returns array with parsed children
10
+
11
+ def parse
12
+ return name => [].push(*_parse_child)
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Mockolate::Types::Hash < Mockolate::Types
4
+ # Public Methods
5
+ # name: Name of the key specified in params block
6
+ # has_children: Returns children elements of hash
7
+ #
8
+ # Private Methods
9
+ # _parse_child Returns array with parsed children
10
+
11
+ def parse
12
+ return name => {}.merge(*_parse_child) if name
13
+ {}.merge(*_parse_child)
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Mockolate::Types::Integer < Mockolate::Types
4
+ # Public Methods
5
+ # name: Name of the key specified in params block
6
+ # options: Options of the key specified in params block
7
+ # value: Returns default value if specified, or fetchs data from faker
8
+
9
+ # def parse
10
+ # super
11
+ # end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Mockolate::Types::String < Mockolate::Types
4
+ # Public Methods
5
+ # name: Name of the key specified in params block
6
+ # options: Options of the key specified in params block
7
+ # value: Returns default value if specified, or fetchs data from faker
8
+
9
+ # def parse
10
+ # super
11
+ # end
12
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mockolate::Validators
4
+ class << self
5
+ def check_value_given?(obj)
6
+ return if _check_has_default_value?(obj) || _check_both_given?(obj)
7
+ raise Mockolate::Errors::MissingValueError, 'Default value or all Faker options should be given'
8
+ end
9
+
10
+ def check_faker(obj)
11
+
12
+ end
13
+
14
+ private
15
+ def _check_has_default_value?(obj)
16
+ !!obj[:value]
17
+ end
18
+
19
+ def _check_has_faker_key?(obj)
20
+ return !!obj[:key]
21
+ end
22
+
23
+ def _check_has_faker_module?(obj)
24
+ return !!obj[:fake_from]
25
+ end
26
+
27
+ def _check_both_given?(obj)
28
+ _check_has_faker_key?(obj) && _check_has_faker_module?(obj)
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mockolate
4
- VERSION = "0.0.0"
4
+ VERSION = "0.1.0"
5
5
  end
@@ -26,4 +26,6 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "bundler", "~> 2.0"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
+
30
+ spec.add_dependency "faker"
29
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mockolate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Farhad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-27 00:00:00.000000000 Z
11
+ date: 2020-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faker
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'
55
69
  description: Multi purpose mock, dummy objects, dummy responses generator library
56
70
  email:
57
71
  - farhad9801@gmail.com
@@ -70,13 +84,18 @@ files:
70
84
  - bin/console
71
85
  - bin/setup
72
86
  - lib/mockolate.rb
73
- - lib/mockolate/parameters.rb
74
- - lib/mockolate/parameters/attributes.rb
75
- - lib/mockolate/parameters/attributes/macros.rb
76
- - lib/mockolate/parameters/attributes/parser.rb
77
- - lib/mockolate/parameters/attributes/types.rb
78
- - lib/mockolate/parameters/generator.rb
79
- - lib/mockolate/parameters/hooks.rb
87
+ - lib/mockolate/errors.rb
88
+ - lib/mockolate/initializers.rb
89
+ - lib/mockolate/register.rb
90
+ - lib/mockolate/request.rb
91
+ - lib/mockolate/request/parser.rb
92
+ - lib/mockolate/response.rb
93
+ - lib/mockolate/types.rb
94
+ - lib/mockolate/types/array.rb
95
+ - lib/mockolate/types/hash.rb
96
+ - lib/mockolate/types/integer.rb
97
+ - lib/mockolate/types/string.rb
98
+ - lib/mockolate/validators.rb
80
99
  - lib/mockolate/version.rb
81
100
  - mockolate.gemspec
82
101
  homepage:
@@ -98,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
117
  - !ruby/object:Gem::Version
99
118
  version: '0'
100
119
  requirements: []
101
- rubygems_version: 3.0.6
120
+ rubygems_version: 3.0.3
102
121
  signing_key:
103
122
  specification_version: 4
104
123
  summary: Like chocolate, but, way sweeter
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "mockolate/parameters/generator"
4
- require "mockolate/parameters/attributes"
5
- require "mockolate/parameters/hooks"
6
-
7
- class Mockolate::Parameters
8
- # include Mockolate::Parameters::Generator
9
- # include Mockolate::Parameters::Attributes
10
- include Mockolate::Parameters::Hooks
11
-
12
- def self.inherited(base)
13
- initialize!(base)
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "mockolate/parameters/attributes/macros"
4
-
5
- class Mockolate::Parameters
6
- module Attributes
7
- include Mockolate::Parameters::Attributes::Macros
8
- # def self.included(base)
9
- # # base.include Mockolate::Parameters::Attributes::Macros
10
- # end
11
-
12
- # module ClassMethods
13
- # end
14
- end
15
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "mockolate/parameters/attributes/parser"
4
-
5
- module Mockolate::Parameters::Attributes
6
- # This module includes main macros definitions
7
- module Macros
8
- def self.included(base)
9
- base.extend ClassMethods
10
- end
11
-
12
- module ClassMethods
13
- include Mockolate::Parameters::Attributes::Parser
14
-
15
- # Handles single attribute which may have constant values
16
- # @param attr [String] key of attribute
17
- # @yield block
18
- def attribute(attr, &block)
19
- parse!(attr, &block)
20
- # class_variable_get(:@@_public_attributes).merge
21
- end
22
-
23
- # Handles multiple attributes
24
- # @param attrs [Array<String>] key of attribute
25
- def attributes(*attrs)
26
- parse!(attrs)
27
- # attr.each do |attr|
28
- # return parse_hash(attr) if attr.is_a? Hash
29
- # parse_symbol
30
- # end
31
- # class_variable_get(:@@_public_attributes).push *attrs
32
- end
33
- end
34
- end
35
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "mockolate/parameters/attributes/types"
4
-
5
- module Mockolate::Parameters::Attributes
6
- module Parser
7
- include Mockolate::Parameters::Attributes::Types
8
-
9
- def parse!(obj, &block)
10
- return _parse_attribute_from_symbol(obj, &block) if obj.kind_of? Symbol
11
- return _parse_attributes_from_array(obj) if obj.kind_of? Array
12
- end
13
-
14
- private
15
- def _parse_attributes_from_array(attrs)
16
- attrs.map do |attr|
17
- _parse_attribute_from_symbol(attr)
18
- end
19
- end
20
-
21
- def _parse_attribute_from_symbol(sym, type = :string, &block)
22
- return yield if block_given?
23
-
24
- class_variable_get(:@@_public_attributes).merge!({sym => method(type).call(sym)})
25
- end
26
-
27
- def _parse_attribute_from_hash(obj)
28
- end
29
- end
30
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mockolate::Parameters::Attributes
4
- module Types
5
- def string(name)
6
- # class_variable_get(:@@_public_attributes).merge!(
7
- {value: '', type: :string}
8
- # )
9
- end
10
-
11
- def integer(name)
12
- class_variable_get(:@@_public_attributes).merge!(
13
- name.to_sym => {value: 0, type: :integer}
14
- )
15
- end
16
-
17
- # def type_hander(&block)
18
-
19
- # end
20
- end
21
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Mockolate::Parameters
4
- module Generator
5
- def self.included(base)
6
- base.extend ClassMethods
7
- end
8
-
9
- module ClassMethods
10
- # def generate
11
- # {is_dummy: 'Yes'}
12
- # end
13
- end
14
- end
15
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # frozen_string_literal: true
4
-
5
- class Mockolate::Parameters
6
- module Hooks
7
- def self.included(base)
8
- base.extend ClassInitializers
9
- end
10
-
11
- module ClassInitializers
12
- # Function runs after a class inherited from Mockolate::Parameters
13
- # @params [Class] class to override
14
- def initialize!(base)
15
- base.class_eval do
16
- # Included modules
17
- include Mockolate::Parameters::Attributes::Macros
18
- # include Mockolate::Parameters::Generator
19
- # include Mockolate::Parameters::Attributes
20
-
21
- # class variables and attributes
22
- class_variable_set :@@_public_attributes, {}
23
- end
24
- end
25
- end
26
-
27
- end
28
- end