rao-service 0.0.48.pre → 0.0.49.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: c507e0cdaa106c00934229447c8d39352a945ea43711f4ddbef22206ea8848d5
4
+ data.tar.gz: 050d513fac7820d3e303978c2a8d6b36dd1fe6912dadfd1bad90fc1d6ad7411c
5
5
  SHA512:
6
- metadata.gz: 7ded1050beb995cf9de1403dac421c36ca0e2f362c8c06838edc0eff1d7980d1bbfa7e440f50cbcc7a23101cabca7442654429eb04ca535ed6848d19035748fa
7
- data.tar.gz: 3ec72a8fc91a7317713af254177f9e9da394ee26301697f9992eb2b234e976a08213636da9c9ecfa37e049fa088ebb0331bdea9cd51ca450e2bdd813b4516f1a
6
+ metadata.gz: 4067b49a14138336746ee35f2d5ba152dfadf583e42daa75859e4339cc529e572e955a898f0b270a5e4a8ad94c6c370944434fd90362b722d154904c49856d8d
7
+ data.tar.gz: 2e4ca55c87cded18a5c5560ff2d366ee885fcef2060d82ee4c290bea2582e7c5b720129e6d424386f482214809a114e06472867ecd6e58b839a614d069765121
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:
@@ -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
@@ -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
@@ -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
@@ -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.49.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-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -208,6 +208,7 @@ files:
208
208
  - lib/rao/service/engine.rb
209
209
  - lib/rao/service/message/base.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.11
235
236
  signing_key:
236
237
  specification_version: 4
237
238
  summary: Services for Ruby on Rails.