ringu 0.1.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +173 -0
  5. data/Gemfile +12 -0
  6. data/Gemfile.lock +55 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +115 -0
  9. data/Rakefile +12 -0
  10. data/bin/console +15 -0
  11. data/bin/setup +8 -0
  12. data/example/.ruby-version +1 -0
  13. data/example/Gemfile +37 -0
  14. data/example/Gemfile.lock +176 -0
  15. data/example/README.md +24 -0
  16. data/example/Rakefile +6 -0
  17. data/example/app/controllers/application_controller.rb +2 -0
  18. data/example/app/controllers/characters_controller.rb +12 -0
  19. data/example/app/controllers/concerns/.keep +0 -0
  20. data/example/app/deps/container.rb +6 -0
  21. data/example/app/fetchers/characters_fetcher.rb +13 -0
  22. data/example/app/models/application_record.rb +3 -0
  23. data/example/app/models/concerns/.keep +0 -0
  24. data/example/app/parsers/characters_parser.rb +15 -0
  25. data/example/bin/bundle +114 -0
  26. data/example/bin/rails +4 -0
  27. data/example/bin/rake +4 -0
  28. data/example/bin/setup +33 -0
  29. data/example/config.ru +6 -0
  30. data/example/config/application.rb +40 -0
  31. data/example/config/boot.rb +3 -0
  32. data/example/config/credentials.yml.enc +1 -0
  33. data/example/config/database.yml +25 -0
  34. data/example/config/environment.rb +5 -0
  35. data/example/config/environments/development.rb +58 -0
  36. data/example/config/environments/production.rb +96 -0
  37. data/example/config/environments/test.rb +49 -0
  38. data/example/config/initializers/application_controller_renderer.rb +8 -0
  39. data/example/config/initializers/backtrace_silencers.rb +8 -0
  40. data/example/config/initializers/cors.rb +16 -0
  41. data/example/config/initializers/filter_parameter_logging.rb +6 -0
  42. data/example/config/initializers/inflections.rb +16 -0
  43. data/example/config/initializers/mime_types.rb +4 -0
  44. data/example/config/initializers/wrap_parameters.rb +14 -0
  45. data/example/config/locales/en.yml +33 -0
  46. data/example/config/master.key +1 -0
  47. data/example/config/puma.rb +43 -0
  48. data/example/config/routes.rb +4 -0
  49. data/example/db/schema.rb +15 -0
  50. data/example/db/seeds.rb +7 -0
  51. data/example/lib/tasks/.keep +0 -0
  52. data/example/public/robots.txt +1 -0
  53. data/example/test/controllers/.keep +0 -0
  54. data/example/test/controllers/characters_controller_test.rb +23 -0
  55. data/example/test/fetchers/characters_fetcher_test.rb +31 -0
  56. data/example/test/fixtures/files/.keep +0 -0
  57. data/example/test/integration/.keep +0 -0
  58. data/example/test/models/.keep +0 -0
  59. data/example/test/parsers/characters_parser_test.rb +37 -0
  60. data/example/test/test_helper.rb +15 -0
  61. data/example/vendor/.keep +0 -0
  62. data/lib/ringu.rb +12 -0
  63. data/lib/ringu/container.rb +26 -0
  64. data/lib/ringu/deps.rb +37 -0
  65. data/lib/ringu/injector.rb +43 -0
  66. data/lib/ringu/resolver.rb +31 -0
  67. data/lib/ringu/version.rb +5 -0
  68. data/ringu.gemspec +34 -0
  69. metadata +111 -0
@@ -0,0 +1 @@
1
+ 65a6628c04584f2f6e427511c4de57bd
@@ -0,0 +1,43 @@
1
+ # Puma can serve each request in a thread from an internal thread pool.
2
+ # The `threads` method setting takes two numbers: a minimum and maximum.
3
+ # Any libraries that use thread pools should be configured to match
4
+ # the maximum value specified for Puma. Default is set to 5 threads for minimum
5
+ # and maximum; this matches the default thread size of Active Record.
6
+ #
7
+ max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
8
+ min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
9
+ threads min_threads_count, max_threads_count
10
+
11
+ # Specifies the `worker_timeout` threshold that Puma will use to wait before
12
+ # terminating a worker in development environments.
13
+ #
14
+ worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
15
+
16
+ # Specifies the `port` that Puma will listen on to receive requests; default is 3000.
17
+ #
18
+ port ENV.fetch("PORT") { 3000 }
19
+
20
+ # Specifies the `environment` that Puma will run in.
21
+ #
22
+ environment ENV.fetch("RAILS_ENV") { "development" }
23
+
24
+ # Specifies the `pidfile` that Puma will use.
25
+ pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
26
+
27
+ # Specifies the number of `workers` to boot in clustered mode.
28
+ # Workers are forked web server processes. If using threads and workers together
29
+ # the concurrency of the application would be max `threads` * `workers`.
30
+ # Workers do not work on JRuby or Windows (both of which do not support
31
+ # processes).
32
+ #
33
+ # workers ENV.fetch("WEB_CONCURRENCY") { 2 }
34
+
35
+ # Use the `preload_app!` method when specifying a `workers` number.
36
+ # This directive tells Puma to first boot the application and load code
37
+ # before forking the application. This takes advantage of Copy On Write
38
+ # process behavior so workers use less memory.
39
+ #
40
+ # preload_app!
41
+
42
+ # Allow puma to be restarted by `rails restart` command.
43
+ plugin :tmp_restart
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ resources :characters, only: :index
3
+ # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
4
+ end
@@ -0,0 +1,15 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # This file is the source Rails uses to define your schema when running `bin/rails
6
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
7
+ # be faster and is potentially less error prone than running all of your
8
+ # migrations from scratch. Old migrations may fail to apply correctly if those
9
+ # migrations use external dependencies or application code.
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(version: 0) do
14
+
15
+ end
@@ -0,0 +1,7 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
7
+ # Character.create(name: 'Luke', movie: movies.first)
File without changes
@@ -0,0 +1 @@
1
+ # See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
File without changes
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+
3
+ class CharactersControllerTest < ActionDispatch::IntegrationTest
4
+ setup do
5
+ @mock_fetcher = mock('object')
6
+ Container.deps.replace(:characters_fetcher, @mock_fetcher)
7
+
8
+ end
9
+
10
+ teardown do
11
+ Container.deps.restore!
12
+ end
13
+
14
+ test "should get index" do
15
+ characters = []
16
+ @mock_fetcher.expects(:fetch).returns(characters)
17
+
18
+ get characters_url, as: :json
19
+
20
+ assert_response :success
21
+ assert response.body == '[]'
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ require "test_helper"
2
+ require "minitest/mock"
3
+
4
+ def stub()
5
+ mock = MiniTest::Mock.new
6
+ mock.expect(:is_a?, data, [CharactersFetcher::URL])
7
+ end
8
+
9
+ class CharactersFetcherTest < ActiveSupport::TestCase
10
+ setup do
11
+ @mock_client = mock('object')
12
+ @mock_parser = mock('object')
13
+
14
+ @subject = CharactersFetcher.new(
15
+ http_client: @mock_client,
16
+ characters_parser: @mock_parser
17
+ )
18
+ end
19
+
20
+ test "should parse characters returns characters" do
21
+ data = OpenStruct.new body: '[]'
22
+ characters = []
23
+
24
+ @mock_client.expects(:get).with(CharactersFetcher::URL).returns(data)
25
+ @mock_parser.expects(:parse).returns(characters)
26
+
27
+ results = @subject.fetch
28
+
29
+ assert results == characters
30
+ end
31
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,37 @@
1
+ require "test_helper"
2
+
3
+ class CharactersParserTest < ActiveSupport::TestCase
4
+ setup do
5
+ @subject = CharactersParser.new
6
+ end
7
+
8
+ test "should parse characters" do
9
+ data = <<-JSON
10
+ {
11
+ "results": [
12
+ { "id": 1, "name": "character 1", "image": "/image1.jpg"},
13
+ { "id": 2, "name": "character 2", "image": "/image2.jpg"}
14
+ ]
15
+ }
16
+ JSON
17
+
18
+ results = @subject.parse(data)
19
+
20
+ assert results.length == 2
21
+
22
+ assert results[0][:id] == 1
23
+ assert results[0][:name] == 'character 1'
24
+ assert results[0][:image] == '/image1.jpg'
25
+
26
+ assert results[1][:id] == 2
27
+ assert results[1][:name] == 'character 2'
28
+ assert results[1][:image] == '/image2.jpg'
29
+ end
30
+
31
+ test "should return empty array when result is null" do
32
+ data = '{ "results": null }'
33
+ results = @subject.parse(data)
34
+
35
+ assert results.length == 0
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ require_relative "../config/environment"
3
+ require "rails/test_help"
4
+ require 'mocha/minitest'
5
+
6
+
7
+ class ActiveSupport::TestCase
8
+ # Run tests in parallel with specified workers
9
+ parallelize(workers: :number_of_processors)
10
+
11
+ # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
12
+ fixtures :all
13
+
14
+ # Add more helper methods to be used by all tests here...
15
+ end
File without changes
data/lib/ringu.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ringu/version"
4
+ require_relative "ringu/deps"
5
+ require_relative "ringu/container"
6
+ require_relative "ringu/injector"
7
+ require_relative "ringu/resolver"
8
+
9
+ module Ringu
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # TODO: add tests
4
+ module Ringu
5
+ module Container
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ const_set "Inject", Ringu::Injector.create(base.deps)
9
+ const_set "Resolve", Ringu::Resolver.create(base.deps)
10
+ end
11
+
12
+ module ClassMethods
13
+ def deps
14
+ @deps ||= Ringu::Deps.new
15
+ end
16
+
17
+ def register(name, value)
18
+ deps.register(name, value)
19
+ end
20
+
21
+ def resolve(name)
22
+ deps.resolve(name)
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/ringu/deps.rb ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-container"
4
+
5
+ # TODO: add tests
6
+ module Ringu
7
+ class Deps
8
+ def initialize
9
+ @deps = {}
10
+ @backup = {}
11
+ end
12
+
13
+ def restore!
14
+ @backup.each do |key, value|
15
+ @deps[key] = value
16
+ end
17
+ end
18
+
19
+ def register(key, value)
20
+ @deps[key] = value
21
+ end
22
+
23
+ def replace(key, value)
24
+ raise "\"#{key}\" key does not exist" \
25
+ unless @deps.key?(key)
26
+
27
+ @backup[key] = @deps[key]
28
+ @deps[key] = value
29
+ end
30
+
31
+ def resolve(key, term = nil)
32
+ value = term || @deps.fetch(key)
33
+
34
+ value.is_a?(Class) ? value.new : value
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # TODO: add tests
4
+ module Ringu
5
+ module Injector
6
+ extend self
7
+
8
+ def create(deps)
9
+ Module.new do
10
+ define_method(:deps) { deps }
11
+
12
+ def self.included(klass)
13
+ klass.extend ClassMethods
14
+ klass.send :include, InstanceMethods
15
+ end
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+ def inject(name, klass = nil)
21
+ attr_accessor name
22
+
23
+ injector[name] = {
24
+ klass: klass
25
+ }
26
+ end
27
+
28
+ def injector
29
+ @injector ||= {}
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+ def initialize(**kwargs)
35
+ self.class.injector.each do |name, options|
36
+ term = kwargs[name] || options[:klass]
37
+ resource = deps.resolve(name, term)
38
+ instance_variable_set("@#{name}", resource)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # TODO: add tests
4
+ module Ringu
5
+ module Resolver
6
+ extend self
7
+
8
+ def create(deps)
9
+ Module.new do
10
+ define_method(:deps) { deps }
11
+
12
+ def self.included(klass)
13
+ klass.extend ClassMethods
14
+ end
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ def resolve(name)
20
+ define_method(name) do
21
+ var_name = "@#{name}"
22
+
23
+ return instance_variable_get(var_name) \
24
+ if instance_variable_defined?(var_name)
25
+
26
+ instance_variable_set(var_name, deps.resolve(name))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ringu
4
+ VERSION = "0.1.0-alpha"
5
+ end
data/ringu.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ringu/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ringu"
7
+ spec.version = Ringu::VERSION
8
+ spec.authors = ["Ramón Soto"]
9
+
10
+ spec.summary = "Dependency Injection for ruby"
11
+ spec.description = "A simple dependency injection framework for ruby"
12
+ spec.homepage = "http://github.com/clouw/ringu"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ # spec.metadata["changelog_uri"] = ""
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ # Uncomment to register a new dependency of your gem
30
+ # spec.add_dependency "example-gem", "~> 1.0"
31
+
32
+ # For more information and examples about making a new gem, checkout our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ringu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Ramón Soto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple dependency injection framework for ruby
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".gitignore"
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - example/.ruby-version
30
+ - example/Gemfile
31
+ - example/Gemfile.lock
32
+ - example/README.md
33
+ - example/Rakefile
34
+ - example/app/controllers/application_controller.rb
35
+ - example/app/controllers/characters_controller.rb
36
+ - example/app/controllers/concerns/.keep
37
+ - example/app/deps/container.rb
38
+ - example/app/fetchers/characters_fetcher.rb
39
+ - example/app/models/application_record.rb
40
+ - example/app/models/concerns/.keep
41
+ - example/app/parsers/characters_parser.rb
42
+ - example/bin/bundle
43
+ - example/bin/rails
44
+ - example/bin/rake
45
+ - example/bin/setup
46
+ - example/config.ru
47
+ - example/config/application.rb
48
+ - example/config/boot.rb
49
+ - example/config/credentials.yml.enc
50
+ - example/config/database.yml
51
+ - example/config/environment.rb
52
+ - example/config/environments/development.rb
53
+ - example/config/environments/production.rb
54
+ - example/config/environments/test.rb
55
+ - example/config/initializers/application_controller_renderer.rb
56
+ - example/config/initializers/backtrace_silencers.rb
57
+ - example/config/initializers/cors.rb
58
+ - example/config/initializers/filter_parameter_logging.rb
59
+ - example/config/initializers/inflections.rb
60
+ - example/config/initializers/mime_types.rb
61
+ - example/config/initializers/wrap_parameters.rb
62
+ - example/config/locales/en.yml
63
+ - example/config/master.key
64
+ - example/config/puma.rb
65
+ - example/config/routes.rb
66
+ - example/db/schema.rb
67
+ - example/db/seeds.rb
68
+ - example/lib/tasks/.keep
69
+ - example/public/robots.txt
70
+ - example/test/controllers/.keep
71
+ - example/test/controllers/characters_controller_test.rb
72
+ - example/test/fetchers/characters_fetcher_test.rb
73
+ - example/test/fixtures/files/.keep
74
+ - example/test/integration/.keep
75
+ - example/test/models/.keep
76
+ - example/test/parsers/characters_parser_test.rb
77
+ - example/test/test_helper.rb
78
+ - example/vendor/.keep
79
+ - lib/ringu.rb
80
+ - lib/ringu/container.rb
81
+ - lib/ringu/deps.rb
82
+ - lib/ringu/injector.rb
83
+ - lib/ringu/resolver.rb
84
+ - lib/ringu/version.rb
85
+ - ringu.gemspec
86
+ homepage: http://github.com/clouw/ringu
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ homepage_uri: http://github.com/clouw/ringu
91
+ source_code_uri: http://github.com/clouw/ringu
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: 2.4.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">"
104
+ - !ruby/object:Gem::Version
105
+ version: 1.3.1
106
+ requirements: []
107
+ rubygems_version: 3.2.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Dependency Injection for ruby
111
+ test_files: []