rao-service 0.0.48.pre → 0.0.50.pre

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: 5ae2d70f4e63752d4ac5a78e588a07d7929b3ef45b696d650ad8f3d3cdae0777
4
- data.tar.gz: 28d0a194eac75ddbb0011b0552b325a51b33ce7ccf67b77e44e9b63a6a4342ca
3
+ metadata.gz: 294f3a65680c8418c165d267126dbfdb339563eeac0f10aacf532f3f2eb62bcc
4
+ data.tar.gz: d78dfc7c61bdef1fcfcff9786aca13e8d667d00da890b16253f1d588bbb67bc8
5
5
  SHA512:
6
- metadata.gz: 7ded1050beb995cf9de1403dac421c36ca0e2f362c8c06838edc0eff1d7980d1bbfa7e440f50cbcc7a23101cabca7442654429eb04ca535ed6848d19035748fa
7
- data.tar.gz: 3ec72a8fc91a7317713af254177f9e9da394ee26301697f9992eb2b234e976a08213636da9c9ecfa37e049fa088ebb0331bdea9cd51ca450e2bdd813b4516f1a
6
+ metadata.gz: 1d614bc3dc2b93cfc2fc5b8521a78fe2b56aedfcf18d3a5326a5392a3c89f9bc218218337916fc235df1326b2d90944d7ae9c59238968bf3cec2ee462bb002fc
7
+ data.tar.gz: 3442dc89d8f1f844bd2d37ee9216c1960382e17e36da888ab7f0cdb735b48877f62416559bc032c5f710b23a2123921b83302285e3a8ec244c4dda866e5b246b
data/README.md CHANGED
@@ -1,8 +1,52 @@
1
1
  # Rails Add-Ons Service
2
- Short description and motivation.
2
+
3
+ Rails is missing a service layer. For simple applications the default MVC impementation may be enought, but once you begin adding serious business lovgic you either end up with fat controllers, models, callback hell or all of them.
4
+
5
+ Rao::Service solves this problem by providing you a service object that is easy to use. It can be simultaneously used for UIs and APIs by using Rao::ServiceController and Rao::Api::Service Controller as its frontend.
6
+
3
7
 
4
8
  ## Usage
5
- How to use my plugin.
9
+
10
+ Here is a basic example that queries the nasa api:
11
+ ```
12
+ # nethttp.rb
13
+ require 'uri'
14
+ require 'net/http'
15
+
16
+ class NasaService < Rao::Service::Base
17
+ class Result < Rao::Service::Result::Base
18
+ attr_accessor :response
19
+
20
+ def parsed_response
21
+ @parsed_response ||= JSON.parse(response)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def _perform
28
+ response = Net::HTTP.get_response(uri)
29
+
30
+ if response.is_a?(Net::HTTPSuccess)
31
+ @result.response = response.body
32
+ else
33
+ add_error_and_say(:base, "API request failed.")
34
+ end
35
+ end
36
+
37
+ def uri
38
+ @uri ||= URI('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
39
+ end
40
+ end
41
+
42
+ result = PlanetService.call
43
+
44
+ if result.ok?
45
+ result.parsed_response
46
+ else
47
+ result.errors.full_messages.to_sentence
48
+ end
49
+ ```
6
50
 
7
51
  ## Installation
8
52
  Add this line to your application's Gemfile:
@@ -16,4 +16,10 @@ Rao::Service.configure do |config|
16
16
  # Default: config.default_notification_recipient = ->(result) { ::Rails.env }
17
17
  #
18
18
  config.notification_environment = ->(result) { ::Rails.env }
19
+
20
+ # Sets the base class for service jobs.
21
+ #
22
+ # default: config.job_base_class_name = 'ActiveJob::Base'
23
+ #
24
+ config.job_base_class_name = 'ActiveJob::Base'
19
25
  end
@@ -25,6 +25,10 @@ module Rao
25
25
  @options[:autosave] = true
26
26
  self
27
27
  end
28
+
29
+ def autosave=(value)
30
+ @options[:autosave] = ActiveModel::Type::Boolean.new.cast(value)
31
+ end
28
32
  end
29
33
  end
30
34
  end
@@ -9,6 +9,9 @@ module Rao
9
9
  def before_validation; end
10
10
  def after_validation; end
11
11
  def after_perform; end
12
+ def around_perform
13
+ yield
14
+ end
12
15
 
13
16
  def perform(options = {})
14
17
  options.reverse_merge!(validate: true)
@@ -19,8 +22,10 @@ module Rao
19
22
  after_validation
20
23
  end
21
24
  before_perform
22
- say "Performing" do
23
- _perform
25
+ around_perform do
26
+ say "Performing" do
27
+ _perform
28
+ end
24
29
  end
25
30
  after_perform
26
31
  save if autosave? && respond_to?(:save, true)
@@ -37,6 +37,11 @@ module Rao
37
37
  add_attribute_names(*args)
38
38
  end
39
39
 
40
+ def self.attr_reader(*args)
41
+ super
42
+ add_attribute_names(*args)
43
+ end
44
+
40
45
  def self.add_attribute_names(*args)
41
46
  args.each do |attr_name|
42
47
  attribute_names << attr_name
@@ -11,6 +11,7 @@ module Rao
11
11
  mattr_accessor(:default_notification_sender) { 'john.doe@domain.local' }
12
12
  mattr_accessor(:default_notification_recipient) { 'john.doe@domain.local' }
13
13
  mattr_accessor(:notification_environment) { Object.const_defined?('::Rails') ? ::Rails.env : nil }
14
+ mattr_accessor(:job_base_class_name) { 'ActiveJob::Base' }
14
15
  end
15
16
  end
16
17
  end
@@ -1,6 +1,6 @@
1
1
  module Rao
2
2
  module Service
3
- class Job < ApplicationJob
3
+ class Job < Rao::Service::Configuration.job_base_class_name.constantize
4
4
  def perform(service_class_name, attributes = {}, options = {})
5
5
  service_class_name.constantize.call(attributes, options)
6
6
  end
@@ -1,3 +1,5 @@
1
+ require "rao/service/application_mailer"
2
+
1
3
  module Rao
2
4
  module Service
3
5
  class NotificationMailer < ApplicationMailer
@@ -0,0 +1,35 @@
1
+ module Rao
2
+ module Service
3
+ module Result
4
+ module Base::AttributeNamesConcern
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ end
9
+
10
+ class_methods do
11
+ def attr_accessor(*args)
12
+ super
13
+ add_attribute_names(*args)
14
+ end
15
+
16
+ def attr_reader(*args)
17
+ super
18
+ add_attribute_names(*args)
19
+ end
20
+
21
+ def add_attribute_names(*args)
22
+ args.each do |attr_name|
23
+ attribute_names << attr_name
24
+ end
25
+ end
26
+
27
+ def attribute_names
28
+ (@attr_names ||= [])
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
@@ -12,6 +12,9 @@ module Rao
12
12
  include MailableConcern
13
13
  end
14
14
 
15
+ require 'rao/service/result/base/attribute_names_concern'
16
+ include AttributeNamesConcern
17
+
15
18
  require 'rao/service/result/base/succeedable_concern'
16
19
  include SucceedableConcern
17
20
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rao-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.48.pre
4
+ version: 0.0.50.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Vasquez Angel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-21 00:00:00.000000000 Z
11
+ date: 2023-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -188,14 +188,12 @@ files:
188
188
  - MIT-LICENSE
189
189
  - README.md
190
190
  - Rakefile
191
- - app/jobs/rao/service/job.rb
192
- - app/mailers/rao/service/application_mailer.rb
193
- - app/mailers/rao/service/notification_mailer.rb
194
191
  - app/views/rao/service/notification_mailer/result_email.text.erb
195
192
  - lib/generators/rao/service/install_generator.rb
196
193
  - lib/generators/rao/service/templates/initializer.rb
197
194
  - lib/rao-service.rb
198
195
  - lib/rao/service.rb
196
+ - lib/rao/service/application_mailer.rb
199
197
  - lib/rao/service/base.rb
200
198
  - lib/rao/service/base/active_job_concern.rb
201
199
  - lib/rao/service/base/autosave_concern.rb
@@ -206,8 +204,11 @@ files:
206
204
  - lib/rao/service/base/result_concern.rb
207
205
  - lib/rao/service/configuration.rb
208
206
  - lib/rao/service/engine.rb
207
+ - lib/rao/service/job.rb
209
208
  - lib/rao/service/message/base.rb
209
+ - lib/rao/service/notification_mailer.rb
210
210
  - lib/rao/service/result/base.rb
211
+ - lib/rao/service/result/base/attribute_names_concern.rb
211
212
  - lib/rao/service/result/base/mailable_concern.rb
212
213
  - lib/rao/service/result/base/succeedable_concern.rb
213
214
  - lib/rao/service/version.rb
@@ -231,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
232
  - !ruby/object:Gem::Version
232
233
  version: 1.3.1
233
234
  requirements: []
234
- rubygems_version: 3.2.24
235
+ rubygems_version: 3.4.20
235
236
  signing_key:
236
237
  specification_version: 4
237
238
  summary: Services for Ruby on Rails.