strum-cache 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
+ SHA256:
3
+ metadata.gz: 7824ca447c1951377b226f41a8086425babf82fd5662341e26997027b0d97eb9
4
+ data.tar.gz: 906510b8b5afcad2465677d70b7c7eedbcfeb801bd33689ec1c14531c3f6a64e
5
+ SHA512:
6
+ metadata.gz: c931c79f42393873dd03ec21fcc14d6ab425736d06d59d16c2ffc9406ad7179145460162b0d9301186db06f0edd91a014743d07e2c4170a1e1637639e241e415
7
+ data.tar.gz: e40c0e5f2da8c6bdef9ad705fc697324ec7473506b2060f169316dcc1df1a4fce6d2cafeb149cf3fb2967e7a57d0bb455ea88f1460f417164ac9aa48970a9deb
@@ -0,0 +1,23 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # Editor directories and files
14
+ .idea
15
+ .vscode
16
+ *.suo
17
+ *.ntvs*
18
+ *.njsproj
19
+ *.sln
20
+ *.sw*
21
+
22
+ # builds
23
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
6
+ Style/HashSyntax:
7
+ Enabled: true
8
+
9
+ # Method definitions after `private` or `protected` isolated calls need one
10
+ # extra level of indentation.
11
+ Layout/IndentationConsistency:
12
+ Enabled: true
13
+ EnforcedStyle: indented_internal_methods
14
+
15
+ # Two spaces, no tabs (for indentation).
16
+ Layout/IndentationWidth:
17
+ Enabled: true
18
+
19
+ # Check quotes usage according to lint rule below.
20
+ Style/StringLiterals:
21
+ Enabled: true
22
+ EnforcedStyle: double_quotes
23
+
24
+ Layout/LineLength:
25
+ Max: 120
26
+ IgnoredPatterns: ['\A#']
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
@@ -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 sn@nazarov.com.ua. 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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in strum-cache.gemspec
6
+ gemspec
7
+
8
+ gem "bundler", "~> 2.1.4"
9
+ gem "rspec", "~> 3"
10
+ gem "rubocop", "~> 0.90.0"
11
+
12
+ gem "debase", "~> 0.2.4"
13
+ gem "ruby-debug-ide", "~> 0.7.0"
@@ -0,0 +1,117 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ strum-cache (0.0.6)
5
+ dry-configurable (~> 0.11.6)
6
+ dry-inflector
7
+ faraday
8
+ json
9
+ redis
10
+ strum
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ addressable (2.7.0)
16
+ public_suffix (>= 2.0.2, < 5.0)
17
+ ast (2.4.1)
18
+ concurrent-ruby (1.1.7)
19
+ debase (0.2.4.1)
20
+ debase-ruby_core_source (>= 0.10.2)
21
+ debase-ruby_core_source (0.10.11)
22
+ diff-lcs (1.4.4)
23
+ dry-configurable (0.11.6)
24
+ concurrent-ruby (~> 1.0)
25
+ dry-core (~> 0.4, >= 0.4.7)
26
+ dry-equalizer (~> 0.2)
27
+ dry-container (0.7.2)
28
+ concurrent-ruby (~> 1.0)
29
+ dry-configurable (~> 0.1, >= 0.1.3)
30
+ dry-core (0.4.9)
31
+ concurrent-ruby (~> 1.0)
32
+ dry-equalizer (0.3.0)
33
+ dry-inflector (0.2.0)
34
+ dry-logic (1.0.8)
35
+ concurrent-ruby (~> 1.0)
36
+ dry-core (~> 0.2)
37
+ dry-equalizer (~> 0.2)
38
+ dry-struct (1.3.0)
39
+ dry-core (~> 0.4, >= 0.4.4)
40
+ dry-equalizer (~> 0.3)
41
+ dry-types (~> 1.3)
42
+ ice_nine (~> 0.11)
43
+ dry-types (1.4.0)
44
+ concurrent-ruby (~> 1.0)
45
+ dry-container (~> 0.3)
46
+ dry-core (~> 0.4, >= 0.4.4)
47
+ dry-equalizer (~> 0.3)
48
+ dry-inflector (~> 0.1, >= 0.1.2)
49
+ dry-logic (~> 1.0, >= 1.0.2)
50
+ faraday (1.1.0)
51
+ multipart-post (>= 1.2, < 3)
52
+ ruby2_keywords
53
+ ice_nine (0.11.2)
54
+ json (2.3.1)
55
+ json-schema (2.8.1)
56
+ addressable (>= 2.4)
57
+ multipart-post (2.1.1)
58
+ parallel (1.19.2)
59
+ parser (2.7.2.0)
60
+ ast (~> 2.4.1)
61
+ public_suffix (4.0.6)
62
+ rainbow (3.0.0)
63
+ rake (13.0.1)
64
+ redis (4.2.2)
65
+ regexp_parser (1.8.2)
66
+ rexml (3.2.4)
67
+ rspec (3.9.0)
68
+ rspec-core (~> 3.9.0)
69
+ rspec-expectations (~> 3.9.0)
70
+ rspec-mocks (~> 3.9.0)
71
+ rspec-core (3.9.3)
72
+ rspec-support (~> 3.9.3)
73
+ rspec-expectations (3.9.3)
74
+ diff-lcs (>= 1.2.0, < 2.0)
75
+ rspec-support (~> 3.9.0)
76
+ rspec-mocks (3.9.1)
77
+ diff-lcs (>= 1.2.0, < 2.0)
78
+ rspec-support (~> 3.9.0)
79
+ rspec-support (3.9.4)
80
+ rubocop (0.90.0)
81
+ parallel (~> 1.10)
82
+ parser (>= 2.7.1.1)
83
+ rainbow (>= 2.2.2, < 4.0)
84
+ regexp_parser (>= 1.7)
85
+ rexml
86
+ rubocop-ast (>= 0.3.0, < 1.0)
87
+ ruby-progressbar (~> 1.7)
88
+ unicode-display_width (>= 1.4.0, < 2.0)
89
+ rubocop-ast (0.8.0)
90
+ parser (>= 2.7.1.5)
91
+ ruby-debug-ide (0.7.2)
92
+ rake (>= 0.8.1)
93
+ ruby-progressbar (1.10.1)
94
+ ruby2_keywords (0.0.2)
95
+ sequel (5.37.0)
96
+ strum (0.1.3)
97
+ dry-inflector (~> 0.2.0)
98
+ dry-struct (~> 1.2)
99
+ json-schema (~> 2.8.1)
100
+ sequel (~> 5.29)
101
+ thor (~> 0.20)
102
+ thor (0.20.3)
103
+ unicode-display_width (1.7.0)
104
+
105
+ PLATFORMS
106
+ ruby
107
+
108
+ DEPENDENCIES
109
+ bundler (~> 2.1.4)
110
+ debase (~> 0.2.4)
111
+ rspec (~> 3)
112
+ rubocop (~> 0.90.0)
113
+ ruby-debug-ide (~> 0.7.0)
114
+ strum-cache!
115
+
116
+ BUNDLED WITH
117
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Serhiy Nazarov
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.
@@ -0,0 +1,68 @@
1
+ # Strum::Cache
2
+
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/strum/cache`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'strum-cache'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install strum-cache
22
+
23
+ ## Usage
24
+
25
+ ###ENV variables:
26
+
27
+ `CACHE_REDIS_URL` - connection string to Redis
28
+
29
+ `{RESOURCE}_CACHE_REDIS_URL` - separated Redis connection string for resource
30
+
31
+ `BASE_RESOURCE_URL` - full url to resource find endpoint. example: "http://example.com/entities"
32
+
33
+ `{RESOURCE}_RESOURCE_URL` - separated full url to resource find endpoint. example: "http://example.com/entities"
34
+
35
+ `BASE_RESOURCE_SEARCH_URL` - full url to resource search endpoint. example: "http://example.com/entities/search"
36
+
37
+ `{RESOURCE}_RESOURCE_SEARCH_URL` - separated full url to resource search endpoint. example: "http://example.com/entities/search"
38
+
39
+ ###Configurable
40
+
41
+ `Strum::Cache.config.cache_headers` - hash with headers that should be sent on http request
42
+
43
+ ###How to use
44
+
45
+ `Strum::Cache::Entity::Find.(id)` - find Entity by id
46
+
47
+ `Strum::Cache::Entity::Search.(params)` - search Entity by params
48
+
49
+ `Strum::Cache::Entity::Push.(params)` - push Entity to cache
50
+
51
+ ## Development
52
+
53
+ 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.
54
+
55
+ 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).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/strum-cache. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/strum-cache/blob/master/CODE_OF_CONDUCT.md).
60
+
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
65
+
66
+ ## Code of Conduct
67
+
68
+ Everyone interacting in the Strum::Cache project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/strum-cache/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "strum/cache"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strum/cache/version"
4
+ require "strum/service"
5
+ require "strum/pipe"
6
+ require "dry/inflector"
7
+ require "dry/configurable"
8
+ require "strum/cache_utils/redis"
9
+ require "strum/cache_utils/find"
10
+ require "strum/cache_utils/search"
11
+
12
+ module Strum
13
+ module Cache
14
+ extend Dry::Configurable
15
+
16
+ setting :cache_headers
17
+
18
+ class Error < StandardError; end
19
+
20
+ def self.const_missing(resource_name)
21
+ # Strum::Cache::Entity::Find.(id)
22
+ # Strum::Cache::Entity::Search.(params)
23
+ # Strum::Cache::Entity::Put.(params)
24
+
25
+ Module.new do
26
+ const_set(:Push, Class.new do
27
+ include Strum::Service
28
+
29
+ define_method :call do
30
+ redis_connection.hset(id, input)
31
+ redis_connection.expire(id, redis_resource_expire)
32
+ end
33
+
34
+ define_method :audit do
35
+ required(:id)
36
+ end
37
+
38
+ private
39
+
40
+ define_method :resource_code do
41
+ resource_name.downcase.to_s
42
+ end
43
+
44
+ define_method :redis_connection do
45
+ Strum::CacheUtils::RedisStorage.const_get(resource_code.capitalize, false).instance.redis
46
+ end
47
+
48
+ def redis_resource_expire
49
+ ENV.fetch("#{resource_code.to_s.upcase}_CACHE_EXPIRE", ENV.fetch("CACHE_EXPIRE", 60 * 15))
50
+ end
51
+ end)
52
+
53
+ const_set(:Find, Class.new do
54
+ include Strum::Service
55
+
56
+ define_method :call do
57
+ if entity = from_cache_by_id(input) || find_by_id(input)
58
+ output(entity)
59
+ else
60
+ not_found
61
+ end
62
+ end
63
+
64
+ define_method :audit do
65
+ self.input = input.to_s.to_i
66
+ add_error(:id, :invalid) unless input.positive?
67
+ end
68
+
69
+ private
70
+
71
+ define_method :resource_code do
72
+ resource_name.downcase
73
+ end
74
+
75
+ define_method :from_cache_by_id do |resource_id|
76
+ Strum::CacheUtils::Redis.call(resource_code: resource_code, resource_id: resource_id)
77
+ end
78
+
79
+ define_method :find_by_id do |resource_id|
80
+ Strum::CacheUtils::Find.call(resource_code: resource_code, resource_id: resource_id)
81
+ end
82
+
83
+ define_method :not_found do
84
+ add_error(:entity, :not_found)
85
+ end
86
+ end)
87
+
88
+ const_set(:Search, Class.new do
89
+ include Strum::Service
90
+
91
+ define_method :call do
92
+ if entity = from_cache_by_params || search_by_params
93
+ output(entity)
94
+ else
95
+ not_found
96
+ end
97
+ end
98
+
99
+ define_method :audit do
100
+ required
101
+ end
102
+
103
+ private
104
+
105
+ define_method :resource_code do
106
+ resource_name.downcase
107
+ end
108
+
109
+ define_method :from_cache_by_params do
110
+ false
111
+ end
112
+
113
+ define_method :search_by_params do
114
+ Strum::CacheUtils::Search.call(resource_code: resource_code, params: input)
115
+ end
116
+
117
+ define_method :not_found do
118
+ add_error(:entity, :not_found)
119
+ end
120
+ end)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strum
4
+ module Cache
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strum/cache_utils/build_resources_url"
4
+
5
+ module Strum
6
+ module CacheUtils
7
+ class BuildResourceUrl
8
+ include Strum::Service
9
+
10
+ def call
11
+ underscore_resource_code = inflector.underscore(resource_code)
12
+ base_url = ENV.fetch("#{underscore_resource_code.upcase}_RESOURCE_URL") do
13
+ host = ENV.fetch("BASE_RESOURCE_URL")
14
+ File.join(host, inflector.pluralize(underscore_resource_code.gsub(/_/, "-")))
15
+ end
16
+ output(url: File.join(base_url, resource_id.to_s))
17
+ rescue KeyError
18
+ Strum::CacheUtils::BuildResourcesUrl.call(resource_code: resource_code, params: { id: resource_id }) do |m|
19
+ m.success { |result| output(result) }
20
+ m.failure { |errors| add_errors(errors) }
21
+ end
22
+ end
23
+
24
+ def audit
25
+ required(:resource_code, :resource_id)
26
+ end
27
+
28
+ private
29
+
30
+ def inflector
31
+ @inflector ||= Dry::Inflector.new
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ module Strum
6
+ module CacheUtils
7
+ class BuildResourcesUrl
8
+ include Strum::Service
9
+
10
+ def call
11
+ underscore_resource_code = inflector.underscore(resource_code)
12
+ base_url = ENV.fetch("#{underscore_resource_code.upcase}_RESOURCE_SEARCH_URL") do
13
+ host = ENV.fetch("BASE_RESOURCE_SEARCH_URL")
14
+ File.join(host, inflector.pluralize(underscore_resource_code.gsub(/_/, "-")))
15
+ end
16
+ output(url: base_url, params: params)
17
+ rescue KeyError
18
+ add_error("#{resource_code} SEARCH ENV", :not_found)
19
+ end
20
+
21
+ def audit
22
+ required(:resource_code, :params)
23
+ add_error(:params, :hash_required) unless params.is_a?(Hash)
24
+ end
25
+
26
+ private
27
+
28
+ def inflector
29
+ @inflector ||= Dry::Inflector.new
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strum/json_deserializer"
4
+ require "strum/cache_utils/build_resource_url"
5
+
6
+ module Strum
7
+ module CacheUtils
8
+ class Find
9
+ include Strum::Service
10
+
11
+ def call
12
+ Strum::Pipe.call(Strum::CacheUtils::BuildResourceUrl,
13
+ Strum::CacheUtils::SendRequest,
14
+ Strum::JsonDeserializer,
15
+ input: input) do |m|
16
+ m.success { |responce| output(responce) }
17
+ m.failure { |errors| add_errors(errors) }
18
+ end
19
+ end
20
+
21
+ def audit
22
+ required(:resource_code, :resource_id)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "redis"
4
+ require "singleton"
5
+ require "strum/cache_utils/redis_storage"
6
+
7
+ module Strum
8
+ module CacheUtils
9
+ class Redis
10
+ include Strum::Service
11
+
12
+ def call
13
+ if (entity = redis_connection.hgetall(resource_id)).key?("id")
14
+ redis_connection.expire(resource_id, redis_resource_expire)
15
+ output(entity)
16
+ else
17
+ add_error(:entity, :not_found)
18
+ end
19
+ end
20
+
21
+ def audit
22
+ required(:resource_code, :resource_id)
23
+ end
24
+
25
+ private
26
+
27
+ def redis_connection
28
+ Strum::CacheUtils::RedisStorage.const_get(resource_code.to_s.capitalize, false).instance.redis
29
+ end
30
+
31
+ def redis_resource_expire
32
+ ENV.fetch("#{resource_code.to_s.upcase}_CACHE_EXPIRE", ENV.fetch("CACHE_EXPIRE", 60 * 15))
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+ require "redis"
5
+
6
+ module Strum
7
+ module CacheUtils
8
+ module RedisStorage
9
+ def self.const_missing(resource_name)
10
+ const_set(resource_name.to_s.strip.capitalize, Class.new do
11
+ include Singleton
12
+ attr_reader :redis
13
+
14
+ define_method :initialize do
15
+ redis_url = ENV.fetch("#{resource_name.upcase}_CACHE_REDIS_URL", ENV.fetch("CACHE_REDIS_URL", nil))
16
+ @redis = redis_url ? ::Redis.new(url: redis_url) : ::Redis.new
17
+ end
18
+ end)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strum/pipe"
4
+ require "strum/cache_utils/build_resources_url"
5
+ require "strum/cache_utils/send_request"
6
+ require "strum/json_deserializer"
7
+
8
+ module Strum
9
+ module CacheUtils
10
+ class Search
11
+ include Strum::Service
12
+
13
+ def call
14
+ Strum::Pipe.call(Strum::CacheUtils::BuildResourcesUrl,
15
+ Strum::CacheUtils::SendRequest,
16
+ Strum::JsonDeserializer,
17
+ input: input) do |m|
18
+ m.success { |responce| output(responce) }
19
+ m.failure { |errors| add_errors(errors) }
20
+ end
21
+ end
22
+
23
+ def audit
24
+ required(:resource_code, :params)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+
6
+ module Strum
7
+ module CacheUtils
8
+ class SendRequest
9
+ include Strum::Service
10
+
11
+ def call
12
+ cache_headers = (Strum::Cache.config.cache_headers).is_a?(Hash) ? Strum::Cache.config.cache_headers : {}
13
+ conn = Faraday.new(url: url)
14
+ resp = conn.get do |req|
15
+ req.params = params if input[:params]
16
+ req.headers["Content-Type"] = "application/json"
17
+ req.headers["Accept"] = "application/json"
18
+ cache_headers.each { |k, v| req.headers[k] = v }
19
+ end
20
+ resp.success? && output(JSON.parse(resp.body))
21
+ rescue Faraday::Error => e
22
+ add_error(:connection, e)
23
+ rescue JSON::JSONError => e
24
+ add_error(:payload, e)
25
+ end
26
+
27
+ def audit
28
+ required :url
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/strum/cache/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "strum-cache"
7
+ spec.version = Strum::Cache::VERSION
8
+ spec.authors = ["Serhiy Nazarov"]
9
+ spec.email = ["sn@nazarov.com.ua"]
10
+
11
+ spec.summary = "Cache resources"
12
+ spec.homepage = "https://code.qpard.com/strum/strum-cache"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://code.qpard.com/strum/strum-cache"
19
+ spec.metadata["changelog_uri"] = "https://code.qpard.com/strum/strum-cacheCHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "dry-configurable", "~> 0.11.6"
31
+ spec.add_dependency "dry-inflector", "~> 0.11.6"
32
+ spec.add_dependency "faraday", "~> 1.1"
33
+ spec.add_dependency "json", "~> 2.3"
34
+ spec.add_dependency "redis", "~> 4.2"
35
+ spec.add_dependency "strum", "~> 0.0.55"
36
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strum-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Serhiy Nazarov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.11.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-inflector
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.11.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.11.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: strum
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.0.55
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.0.55
97
+ description:
98
+ email:
99
+ - sn@nazarov.com.ua
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - lib/strum/cache.rb
117
+ - lib/strum/cache/version.rb
118
+ - lib/strum/cache_utils/build_resource_url.rb
119
+ - lib/strum/cache_utils/build_resources_url.rb
120
+ - lib/strum/cache_utils/find.rb
121
+ - lib/strum/cache_utils/redis.rb
122
+ - lib/strum/cache_utils/redis_storage.rb
123
+ - lib/strum/cache_utils/search.rb
124
+ - lib/strum/cache_utils/send_request.rb
125
+ - strum-cache.gemspec
126
+ homepage: https://code.qpard.com/strum/strum-cache
127
+ licenses: []
128
+ metadata:
129
+ allowed_push_host: https://rubygems.org
130
+ homepage_uri: https://code.qpard.com/strum/strum-cache
131
+ source_code_uri: https://code.qpard.com/strum/strum-cache
132
+ changelog_uri: https://code.qpard.com/strum/strum-cacheCHANGELOG.md
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: 2.3.0
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubygems_version: 3.0.3
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Cache resources
152
+ test_files: []