gfresh_point 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +9 -0
  3. data/.gitignore +66 -0
  4. data/Gemfile +6 -0
  5. data/Gemfile.lock +96 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +16 -0
  8. data/Rakefile +10 -0
  9. data/bin/console +14 -0
  10. data/bin/gfresh_point +5 -0
  11. data/bin/setup +8 -0
  12. data/gfresh_point.gemspec +43 -0
  13. data/lib/generators/gfresh_point/install/install_generator.rb +22 -0
  14. data/lib/generators/gfresh_point/install/templates/create_gfresh_point_rule.rb +25 -0
  15. data/lib/gfresh_point/domain/balance.rb +16 -0
  16. data/lib/gfresh_point/domain/rule.rb +15 -0
  17. data/lib/gfresh_point/repository/active_record_repo.rb +58 -0
  18. data/lib/gfresh_point/repository/mem_repo.rb +17 -0
  19. data/lib/gfresh_point/request_objects/consume_point_request.rb +30 -0
  20. data/lib/gfresh_point/request_objects/earn_point_request.rb +26 -0
  21. data/lib/gfresh_point/request_objects/list_user_points_request.rb +22 -0
  22. data/lib/gfresh_point/request_objects/request_object.rb +21 -0
  23. data/lib/gfresh_point/request_objects/rule_list_request.rb +19 -0
  24. data/lib/gfresh_point/request_objects/update_rule_point_request.rb +24 -0
  25. data/lib/gfresh_point/response_objects/response_failure.rb +21 -0
  26. data/lib/gfresh_point/response_objects/response_success.rb +15 -0
  27. data/lib/gfresh_point/usecase/base_usecase.rb +31 -0
  28. data/lib/gfresh_point/usecase/consume_point_usecase.rb +20 -0
  29. data/lib/gfresh_point/usecase/earn_point_usecase.rb +15 -0
  30. data/lib/gfresh_point/usecase/list_user_points_usecase.rb +9 -0
  31. data/lib/gfresh_point/usecase/rule_list_usecase.rb +9 -0
  32. data/lib/gfresh_point/usecase/update_rule_point_usecase.rb +9 -0
  33. data/lib/gfresh_point/version.rb +3 -0
  34. data/lib/gfresh_point.rb +72 -0
  35. metadata +189 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 66e3158bc268a3c817d77f98d55cb236d420d9e54ef09282b4f93c85a493649e
4
+ data.tar.gz: ea87c05ac1a5728291dc9173820f9ce3f85e015031cc70b863a7167094bae080
5
+ SHA512:
6
+ metadata.gz: 0c649b18fcf0f07f1ac4f6c1ecf113a1a8b34d2f178c3da2f0480a659ccb707111424a1c9eb6880d82d7631964844f6b46cb955006fa8b61cccc3c1259637ba5
7
+ data.tar.gz: 190c8f762be2b613ed28cff1db07aac1156258f9194559ef5779b9ac883f4447dbf56b72415d26dd89c0ff7164b5fa38acfa0563539247722c0d59a0c4405388
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
data/.gitignore ADDED
@@ -0,0 +1,66 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*
16
+ /tmp/*
17
+ !/log/.keep
18
+ !/tmp/.keep
19
+
20
+ # Ignore uploaded files in development
21
+ /storage/*
22
+
23
+ /node_modules
24
+ /yarn-error.log
25
+
26
+ /public/assets
27
+ .byebug_history
28
+
29
+ # Ignore master key for decrypting credentials and more.
30
+ # /config/master.key
31
+ /public/packs
32
+ /public/packs-test
33
+ /node_modules
34
+ yarn-debug.log*
35
+ .yarn-integrity
36
+ *.swp
37
+ *.swo
38
+ *.swn
39
+ *.swm
40
+ *.swk
41
+ *.swl
42
+ *.swj
43
+
44
+ .idea
45
+ srm.iml
46
+ .rvmrc
47
+
48
+ config/database.yml
49
+ .envrc
50
+ .ruby-version
51
+ .editorconfig
52
+ dump.rdb
53
+ config/settings.local.yml
54
+ config/settings/*.local.yml
55
+ config/environments/*.local.yml
56
+ *.swo
57
+ *.swp
58
+ tags
59
+ */tags
60
+ beachpricex.iml
61
+ .env
62
+ .DS_Store
63
+ .vscode
64
+ coverage
65
+ .rake_tasks
66
+ .tool-versions
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in GfreshPoint.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,96 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gfresh_point (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionpack (5.2.3)
10
+ actionview (= 5.2.3)
11
+ activesupport (= 5.2.3)
12
+ rack (~> 2.0)
13
+ rack-test (>= 0.6.3)
14
+ rails-dom-testing (~> 2.0)
15
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
16
+ actionview (5.2.3)
17
+ activesupport (= 5.2.3)
18
+ builder (~> 3.1)
19
+ erubi (~> 1.4)
20
+ rails-dom-testing (~> 2.0)
21
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
22
+ activemodel (5.2.3)
23
+ activesupport (= 5.2.3)
24
+ activerecord (5.2.3)
25
+ activemodel (= 5.2.3)
26
+ activesupport (= 5.2.3)
27
+ arel (>= 9.0)
28
+ activesupport (5.2.3)
29
+ concurrent-ruby (~> 1.0, >= 1.0.2)
30
+ i18n (>= 0.7, < 2)
31
+ minitest (~> 5.1)
32
+ tzinfo (~> 1.1)
33
+ arel (9.0.0)
34
+ builder (3.2.3)
35
+ coderay (1.1.2)
36
+ concurrent-ruby (1.1.5)
37
+ crass (1.0.4)
38
+ database_cleaner (1.7.0)
39
+ erubi (1.8.0)
40
+ generator_spec (0.9.4)
41
+ activesupport (>= 3.0.0)
42
+ railties (>= 3.0.0)
43
+ i18n (1.6.0)
44
+ concurrent-ruby (~> 1.0)
45
+ loofah (2.2.3)
46
+ crass (~> 1.0.2)
47
+ nokogiri (>= 1.5.9)
48
+ method_source (0.9.2)
49
+ mini_portile2 (2.4.0)
50
+ minitest (5.11.3)
51
+ nokogiri (1.10.3)
52
+ mini_portile2 (~> 2.4.0)
53
+ pry (0.12.2)
54
+ coderay (~> 1.1.0)
55
+ method_source (~> 0.9.0)
56
+ pry-doc (1.0.0)
57
+ pry (~> 0.11)
58
+ yard (~> 0.9.11)
59
+ rack (2.0.7)
60
+ rack-test (1.1.0)
61
+ rack (>= 1.0, < 3)
62
+ rails-dom-testing (2.0.3)
63
+ activesupport (>= 4.2.0)
64
+ nokogiri (>= 1.6)
65
+ rails-html-sanitizer (1.0.4)
66
+ loofah (~> 2.2, >= 2.2.2)
67
+ railties (5.2.3)
68
+ actionpack (= 5.2.3)
69
+ activesupport (= 5.2.3)
70
+ method_source
71
+ rake (>= 0.8.7)
72
+ thor (>= 0.19.0, < 2.0)
73
+ rake (10.5.0)
74
+ sqlite3 (1.4.0)
75
+ thor (0.20.3)
76
+ thread_safe (0.3.6)
77
+ tzinfo (1.2.5)
78
+ thread_safe (~> 0.1)
79
+ yard (0.9.19)
80
+
81
+ PLATFORMS
82
+ ruby
83
+
84
+ DEPENDENCIES
85
+ activerecord
86
+ bundler (~> 2.0)
87
+ database_cleaner
88
+ generator_spec
89
+ gfresh_point!
90
+ minitest (~> 5.0)
91
+ pry-doc
92
+ rake (~> 10.0)
93
+ sqlite3
94
+
95
+ BUNDLED WITH
96
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 teddy
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.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # Gfresh Point
2
+
3
+ ## Install
4
+
5
+ `gem 'gfresh_point'`
6
+
7
+ ## Initialize
8
+
9
+ run `bundle exec rails generate gfresh_point:install`
10
+
11
+ In `config/initializers/gfresh_point.rb` file
12
+
13
+ ```ruby
14
+ require "gfresh_point"
15
+ $gfresh_point = GfreshPoint::Client.new('your_app_id')
16
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gfresh_point"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/gfresh_point ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gfresh_point"
5
+ require 'securerandom'
data/bin/setup ADDED
@@ -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,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "gfresh_point/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gfresh_point"
8
+ spec.version = GfreshPoint::VERSION
9
+ spec.authors = ["teddy"]
10
+ spec.email = ["mlc880926@gmail.com"]
11
+
12
+ spec.summary = %q{Gfresh Point Gem}
13
+ spec.description = %q{Gfresh Point Gem}
14
+ spec.homepage = "https://github.com/gfreshinc/points"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 2.0"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "minitest", "~> 5.0"
38
+ spec.add_development_dependency "pry-doc"
39
+ spec.add_development_dependency 'generator_spec'
40
+ spec.add_development_dependency "activerecord"
41
+ spec.add_development_dependency "sqlite3"
42
+ spec.add_development_dependency "database_cleaner"
43
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module GfreshPoint
5
+ module Generators
6
+ class InstallGenerator < ::Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path('../templates', __FILE__)
9
+ desc "Add the migrations for Gfresh Point Rule"
10
+
11
+ def self.next_migration_number(path)
12
+ next_migration_number = current_migration_number(path) + 1
13
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
14
+ end
15
+
16
+ def copy_migrations
17
+ migration_template "create_gfresh_point_rule.rb",
18
+ "db/migrate/create_gfresh_point_rule.rb"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ class CreateGfreshPointRule < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :gfresh_point_rules do |t|
4
+ t.string :app_id
5
+ t.string :event_name
6
+ t.integer :point
7
+
8
+ t.timestamps
9
+ end
10
+ add_index :gfresh_point_rules, [:app_id, :event_name], unique: true
11
+
12
+ create_table :gfresh_point_balances do |t|
13
+ t.string :app_id
14
+ t.string :user_id
15
+ t.integer :point
16
+ t.integer :balance
17
+ t.string :event_name
18
+ t.string :origin_id
19
+ t.json :comment
20
+ t.timestamps
21
+ end
22
+ add_index :gfresh_point_balances, :app_id
23
+ add_index :gfresh_point_balances, :user_id
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ module GfreshPoint
2
+ module Domain
3
+ class Balance
4
+
5
+ attr_accessor :app_id, :user_id, :point, :balance
6
+
7
+ def initialize(app_id:, user_id:, point:, balance:)
8
+ @app_id = app_id
9
+ @user_id = user_id
10
+ @point = point
11
+ @balance = balance
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module GfreshPoint
2
+ module Domain
3
+ class Rule
4
+
5
+ attr_accessor :app_id, :event_name, :point
6
+
7
+ def initialize(app_id:, event_name:, point:)
8
+ @app_id = app_id
9
+ @event_name = event_name
10
+ @point = point
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,58 @@
1
+ require 'active_record'
2
+
3
+ module GfreshPoint
4
+ module Repository
5
+ class ActiveRecordRepo
6
+
7
+ def list_rules(app_id)
8
+ Rule.where(app_id: app_id)
9
+ end
10
+
11
+ def update_rule_point(app_id, rule_id, point)
12
+ Rule.find(rule_id).update!(point: point)
13
+ end
14
+
15
+ def get_user_last_balance(app_id, user_id)
16
+ Balance.where(app_id: app_id).where(user_id: user_id).
17
+ order(created_at: :desc).first
18
+ end
19
+
20
+ def create_balance(app_id, user_id, point, balance, event_name, origin_id, comment)
21
+ transaction_protect
22
+ Balance.create!(app_id: app_id, user_id: user_id, point: point, balance: balance,
23
+ origin_id: origin_id, event_name: event_name, comment: comment)
24
+ end
25
+
26
+ def get_rule_by_event_name(app_id, event_name)
27
+ Rule.where(app_id: app_id).where(event_name: event_name).first
28
+ end
29
+
30
+ def list_user_points(app_id, user_id, event_name, origin_id)
31
+ query = Balance.where(app_id: app_id).where(user_id: user_id)
32
+ query = query.where(event_name: event_name) if event_name.presence
33
+ query = query.where(origin_id: origin_id) if origin_id.presence
34
+ query.order(created_at: :desc)
35
+ end
36
+
37
+ private
38
+
39
+ def is_in_transcation?
40
+ !(ActiveRecord::Base.connection.open_transactions == 0)
41
+ end
42
+
43
+ def transaction_protect
44
+ unless is_in_transcation?
45
+ raise "Point operation must be in a transaction "
46
+ end
47
+ end
48
+ end
49
+
50
+ class Rule < ActiveRecord::Base
51
+ self.table_name = "gfresh_point_rules"
52
+ end
53
+
54
+ class Balance < ActiveRecord::Base
55
+ self.table_name = "gfresh_point_balances"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ module GfreshPoint
2
+ module Repository
3
+ class MemRepo
4
+
5
+ attr_accessor :data
6
+
7
+ def initialize(data)
8
+ @data = data
9
+ end
10
+
11
+ def list_rules(app_id)
12
+ data.select {|d| d.app_id == app_id}
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module GfreshPoint
2
+ module RequestObjects
3
+ class ConsumePointRequest < RequestObject
4
+ attr_accessor :app_id, :user_id, :point, :event_name, :origin_id, :force, :comment
5
+
6
+ def initialize(app_id, user_id, point, event_name, origin_id, force, comment)
7
+ @app_id = app_id
8
+ @user_id = user_id
9
+ @point = point
10
+ @event_name = event_name
11
+ @origin_id = origin_id
12
+ @comment = comment
13
+ @force = force
14
+
15
+ if app_id.blank?
16
+ add_error('app_id', "App id can not be blank")
17
+ end
18
+ if user_id.blank?
19
+ add_error('user_id', "User id can not be blank")
20
+ end
21
+ if point.blank?
22
+ add_error('point', "point can not be blank")
23
+ elsif point < 0
24
+ add_error('point', "point must larger than 0")
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ module GfreshPoint
2
+ module RequestObjects
3
+ class EarnPointRequest < RequestObject
4
+ attr_accessor :app_id, :user_id, :event_name, :origin_id, :comment
5
+
6
+ def initialize(app_id, user_id, event_name, origin_id, comment)
7
+ @app_id = app_id
8
+ @user_id = user_id
9
+ @event_name = event_name
10
+ @origin_id = origin_id
11
+ @comment = comment
12
+
13
+ if app_id.blank?
14
+ add_error('app_id', "App id can not be blank")
15
+ end
16
+ if user_id.blank?
17
+ add_error('user_id', "User id can not be blank")
18
+ end
19
+ if event_name.blank?
20
+ add_error('event_name', "event_name can not be blank")
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ module GfreshPoint
2
+ module RequestObjects
3
+ class ListUserPointsRequest < RequestObject
4
+ attr_reader :app_id, :user_id, :event_name, :origin_id
5
+
6
+ def initialize(app_id, user_id, event_name, origin_id)
7
+ @app_id = app_id
8
+ @user_id = user_id
9
+ @event_name = event_name
10
+ @origin_id = origin_id
11
+
12
+ if user_id.blank?
13
+ add_error('user_id', "User id can not be blank")
14
+ end
15
+ if app_id.blank?
16
+ add_error('app_id', "App id can not be blank")
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module GfreshPoint
2
+ module RequestObjects
3
+ class RequestObject
4
+ def add_error(param, message)
5
+ errors << {'param': param, message: message}
6
+ end
7
+
8
+ def has_errors
9
+ errors.count > 0
10
+ end
11
+
12
+ def success?
13
+ !has_errors
14
+ end
15
+
16
+ def errors
17
+ @errors ||= []
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module GfreshPoint
2
+ module RequestObjects
3
+ class RuleListRequest < RequestObject
4
+ attr_accessor :filters
5
+
6
+ ACCEPTED_FILTERS = ['app_id']
7
+
8
+ def initialize(params = {})
9
+ @filters = params[:filters] || {}
10
+ @filters.keys.each do |filter|
11
+ unless ACCEPTED_FILTERS.include?(filter.to_s)
12
+ add_error('filters', "Key #{filter} can not be used")
13
+ end
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ module GfreshPoint
2
+ module RequestObjects
3
+ class UpdateRulePointRequest < RequestObject
4
+ attr_accessor :app_id, :rule_id, :point
5
+
6
+ def initialize(app_id, rule_id, point)
7
+ @app_id = app_id
8
+ @rule_id = rule_id
9
+ @point = point
10
+
11
+ if app_id.blank?
12
+ add_error('app_id', "App id can not be blank")
13
+ end
14
+ if rule_id.blank?
15
+ add_error('rule_id', "Rule id can not be blank")
16
+ end
17
+ if point.blank?
18
+ add_error('app_id', "Point can not be blank")
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module GfreshPoint
2
+ module ResponseObjects
3
+ class ResponseFailure
4
+ RESOURCE_ERROR = 'ResourceError'
5
+ PARAMETERS_ERROR = 'ParametersError'
6
+ SYSTEM_ERROR = 'SystemError'
7
+
8
+ attr_accessor :message
9
+
10
+ def initialize(type, message)
11
+ @type = type
12
+ @message = message
13
+ end
14
+
15
+ def success?
16
+ false
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module GfreshPoint
2
+ module ResponseObjects
3
+ class ResponseSuccess
4
+ attr_accessor :value
5
+
6
+ def initialize(value = nil)
7
+ @value = value
8
+ end
9
+
10
+ def success?
11
+ true
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,31 @@
1
+ module GfreshPoint
2
+ module Usecase
3
+ class BaseUsecase
4
+ attr_accessor :repo # or attr_reader ???
5
+
6
+ def initialize( repo)
7
+ @repo = repo
8
+ end
9
+
10
+ def call(request)
11
+ if request.success?
12
+ result = execute(request)
13
+ generate_response(result)
14
+ else
15
+ GfreshPoint::ResponseObjects::ResponseFailure.new(GfreshPoint::ResponseObjects::ResponseFailure::PARAMETERS_ERROR, request.errors)
16
+ end
17
+ rescue StandardError => e
18
+ GfreshPoint::ResponseObjects::ResponseFailure.new(GfreshPoint::ResponseObjects::ResponseFailure::SYSTEM_ERROR, e.message)
19
+ end
20
+
21
+ def generate_response(result)
22
+ GfreshPoint::ResponseObjects::ResponseSuccess.new(result)
23
+ end
24
+
25
+ def execute(request)
26
+ raise "Not impl"
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ module GfreshPoint
2
+ module Usecase
3
+ class ConsumePointUsecase < BaseUsecase
4
+ def execute(request)
5
+ last_balance = repo.get_user_last_balance(request.app_id, request.user_id).try(:balance).to_i
6
+ if request.force
7
+ raise "not enough point" if request.point > last_balance
8
+ consumed_point = request.point
9
+ else
10
+ consumed_point = [request.point, last_balance].min
11
+ end
12
+ # save balance and rule snapshot
13
+ if consumed_point > 0
14
+ repo.create_balance(request.app_id, request.user_id, consumed_point * -1, last_balance - consumed_point, request.event_name, request.origin_id, request.comment)
15
+ end
16
+ consumed_point
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module GfreshPoint
2
+ module Usecase
3
+ class EarnPointUsecase < BaseUsecase
4
+ def execute(request)
5
+ rule = repo.get_rule_by_event_name(request.app_id, request.event_name)
6
+ raise "Rule not exist" if rule.blank?
7
+ last_balance = repo.get_user_last_balance(request.app_id, request.user_id).try(:balance).to_i
8
+ # save balance and rule snapshot
9
+ if rule.point > 0
10
+ repo.create_balance(request.app_id, request.user_id, rule.point, last_balance + rule.point, request.event_name, request.origin_id, request.comment)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module GfreshPoint
2
+ module Usecase
3
+ class ListUserPointsUsecase < BaseUsecase
4
+ def execute(request)
5
+ repo.list_user_points(request.app_id, request.user_id, request.event_name, request.origin_id)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module GfreshPoint
2
+ module Usecase
3
+ class RuleListUsecase < BaseUsecase
4
+ def execute(request)
5
+ rules = repo.list_rules(request.filters[:app_id])
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module GfreshPoint
2
+ module Usecase
3
+ class UpdateRulePointUsecase < BaseUsecase
4
+ def execute(request)
5
+ rules = repo.update_rule_point(request.app_id, request.rule_id, request.point)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module GfreshPoint
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,72 @@
1
+ require "gfresh_point/version"
2
+ require "gfresh_point/domain/rule"
3
+ # require "gfresh_point/serializers/rule_json_encoder"
4
+ require "gfresh_point/usecase/base_usecase"
5
+ require "gfresh_point/usecase/rule_list_usecase"
6
+ require "gfresh_point/usecase/update_rule_point_usecase"
7
+ require "gfresh_point/usecase/earn_point_usecase"
8
+ require "gfresh_point/usecase/consume_point_usecase"
9
+ require "gfresh_point/usecase/list_user_points_usecase"
10
+
11
+ require "gfresh_point/repository/mem_repo"
12
+ require "gfresh_point/repository/active_record_repo"
13
+
14
+ require "gfresh_point/request_objects/request_object"
15
+ require "gfresh_point/request_objects/rule_list_request"
16
+ require "gfresh_point/request_objects/update_rule_point_request"
17
+ require "gfresh_point/request_objects/earn_point_request"
18
+ require "gfresh_point/request_objects/consume_point_request"
19
+ require "gfresh_point/request_objects/list_user_points_request"
20
+
21
+ require "gfresh_point/response_objects/response_success"
22
+ require "gfresh_point/response_objects/response_failure"
23
+
24
+ # Generators
25
+ require 'generators/gfresh_point/install/install_generator'
26
+ require 'active_record'
27
+
28
+ module GfreshPoint
29
+ class Client
30
+ attr_reader :app_id, :repo
31
+ def initialize(app_id, repo = GfreshPoint::Repository::ActiveRecordRepo.new)
32
+ @app_id = app_id
33
+ @repo = repo
34
+ end
35
+
36
+ def list_rules
37
+ request = GfreshPoint::RequestObjects::RuleListRequest.new(filters: {app_id: app_id})
38
+ use_case = GfreshPoint::Usecase::RuleListUsecase.new(repo)
39
+ response = use_case.call(request)
40
+ response
41
+ end
42
+
43
+ def update_rule_point(rule_id, point)
44
+ request = GfreshPoint::RequestObjects::UpdateRulePointRequest.new(app_id, rule_id, point)
45
+ use_case = GfreshPoint::Usecase::UpdateRulePointUsecase.new(repo)
46
+ response = use_case.call(request)
47
+ response
48
+ end
49
+
50
+ def consume_point(user_id, point, event_name, origin_id, force, comment = {})
51
+ request = GfreshPoint::RequestObjects::ConsumePointRequest.new(app_id, user_id, point, event_name, origin_id, force, comment)
52
+ use_case = GfreshPoint::Usecase::ConsumePointUsecase.new(repo)
53
+ response = use_case.call(request)
54
+ response
55
+ end
56
+
57
+ def earn_point(user_id, event_name, origin_id = nil, comment = {})
58
+ request = GfreshPoint::RequestObjects::EarnPointRequest.new(app_id, user_id, event_name, origin_id, comment)
59
+ use_case = GfreshPoint::Usecase::EarnPointUsecase.new(repo)
60
+ response = use_case.call(request)
61
+ response
62
+ end
63
+
64
+ def list_user_points(user_id, event_name = nil, origin_id = nil)
65
+ request = GfreshPoint::RequestObjects::ListUserPointsRequest.new(app_id, user_id, event_name, origin_id)
66
+ use_case = GfreshPoint::Usecase::ListUserPointsUsecase.new(repo)
67
+ response = use_case.call(request)
68
+ response
69
+ end
70
+
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gfresh_point
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - teddy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-13 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-doc
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
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: generator_spec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: database_cleaner
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Gfresh Point Gem
126
+ email:
127
+ - mlc880926@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".editorconfig"
133
+ - ".gitignore"
134
+ - Gemfile
135
+ - Gemfile.lock
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/gfresh_point
141
+ - bin/setup
142
+ - gfresh_point.gemspec
143
+ - lib/generators/gfresh_point/install/install_generator.rb
144
+ - lib/generators/gfresh_point/install/templates/create_gfresh_point_rule.rb
145
+ - lib/gfresh_point.rb
146
+ - lib/gfresh_point/domain/balance.rb
147
+ - lib/gfresh_point/domain/rule.rb
148
+ - lib/gfresh_point/repository/active_record_repo.rb
149
+ - lib/gfresh_point/repository/mem_repo.rb
150
+ - lib/gfresh_point/request_objects/consume_point_request.rb
151
+ - lib/gfresh_point/request_objects/earn_point_request.rb
152
+ - lib/gfresh_point/request_objects/list_user_points_request.rb
153
+ - lib/gfresh_point/request_objects/request_object.rb
154
+ - lib/gfresh_point/request_objects/rule_list_request.rb
155
+ - lib/gfresh_point/request_objects/update_rule_point_request.rb
156
+ - lib/gfresh_point/response_objects/response_failure.rb
157
+ - lib/gfresh_point/response_objects/response_success.rb
158
+ - lib/gfresh_point/usecase/base_usecase.rb
159
+ - lib/gfresh_point/usecase/consume_point_usecase.rb
160
+ - lib/gfresh_point/usecase/earn_point_usecase.rb
161
+ - lib/gfresh_point/usecase/list_user_points_usecase.rb
162
+ - lib/gfresh_point/usecase/rule_list_usecase.rb
163
+ - lib/gfresh_point/usecase/update_rule_point_usecase.rb
164
+ - lib/gfresh_point/version.rb
165
+ homepage: https://github.com/gfreshinc/points
166
+ licenses:
167
+ - MIT
168
+ metadata:
169
+ homepage_uri: https://github.com/gfreshinc/points
170
+ post_install_message:
171
+ rdoc_options: []
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ requirements: []
185
+ rubygems_version: 3.0.3
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: Gfresh Point Gem
189
+ test_files: []