smooth_operator 1.10.24 → 1.11.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWMwNWQwNzAzNzdiMzI3NTE3OTZiNDY0ZGYxM2JkNzZlZTg5MjBmZA==
4
+ NjZhOWFjNmFiNDQzZGFjZGJkYjIwYzQ2ZjgxM2JmZWE0ZDg1NTJmMQ==
5
5
  data.tar.gz: !binary |-
6
- NjI2NThhZThlYzQ1ZDEwYTY3MjU3ZGM0MjZjM2JjYTNhYTBkMWU3ZQ==
6
+ YjUwZDA4ZmE4NWRjZjczNGRkM2QxMTNmYWYzNjc3MzBiN2E2ZDk4Nw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTU5MDZhODAwMjZjMDdiYTRhY2E3ODA5MWE0OTRiYWNkYTEwNDgxYzQ3YmE5
10
- OWYwMjc5MmM4MDc2OWU2OTI3OTRkNjg4MGY2MTI3MmU2MjMxZmNmYmEyNzUx
11
- ZjMxMTQ1NmQ3Y2MyMDZlZjhiNzZhMzA3YTBmMGVkZTljOTU4YzU=
9
+ YzdlMjdiNzVjOTUwN2JlOTQwOTVkZjIyOTM4N2E5MDQ5NDQxNjZkOTg2YzYw
10
+ YTAyYmE4MWYyZmQyMzZkYjUxYjlmODBhMjEyZGM2Yjg3MjNkNGNmZTVmMmU1
11
+ YmZhZTk3NWEyNDkxMmJhN2FlNWJlYTZhNmM5ZTJiOTU1ZTAwYzc=
12
12
  data.tar.gz: !binary |-
13
- NmY3YTE5MThjZjA5MGQ2MWYxNzI3ZDBmZDBiMjc5NGU5MDAwODlhYTQ4NmRi
14
- NjI2OWFiYjE4ZTZlOWFhODgzZWJiYTQxMTc5ODk4NzEzM2YzNDUxMTc5ODI3
15
- ODcyMDg0YjYwZjBhOTBlYWM3ZmI5NGUxMzU0ZGI4MjZlZmVhM2Y=
13
+ MGE1MDlhN2M5ODk3M2FhZWZlZmZjNzc3ZTZmZjZlZjk5MDNkNzkxOGNkM2Yz
14
+ ZTVjYzI5YjcwYzAyZTQwZjRlNmNmODU5NDRiOWY3Nzc5MDMzMmViNjNhNGU5
15
+ NTI4OTJhMTIxNzk1MGE5M2FhN2M2NWFjZWZiNzQ2OWM1NWNjNGM=
@@ -10,13 +10,12 @@ module SmoothOperator
10
10
 
11
11
  class Base < OpenStruct::Base
12
12
 
13
- extend Operator
14
13
  extend FinderMethods
15
14
  extend Translation if defined? I18n
16
15
 
16
+ include Operator
17
17
  include Persistence
18
-
19
- attr_reader :last_remote_call
18
+ include FinderMethods
20
19
 
21
20
  self.strict_behaviour = true
22
21
 
@@ -6,15 +6,12 @@ module SmoothOperator
6
6
 
7
7
  include Enumerable
8
8
 
9
- attr_reader :meta_data, :internal_array, :object_class
9
+ attr_reader :meta_data, :internal_array
10
10
 
11
11
  def_delegators :internal_array, :length, :<<, :[]
12
12
 
13
13
  def initialize(attributes, object_class)
14
- _attributes = attributes.dup
15
-
16
- @object_class = object_class
17
- _resources_name = object_class.resources_name
14
+ _attributes, _resources_name = attributes.dup, object_class.resources_name
18
15
 
19
16
  @internal_array = [*_attributes[_resources_name]].map { |array_entry| object_class.new(array_entry).tap { |object| object.reloaded = true } }
20
17
  _attributes.delete(_resources_name)
@@ -111,6 +111,10 @@ module SmoothOperator
111
111
  else
112
112
  update_internal_data(attribute_name, attribute_value, cast)
113
113
  end
114
+
115
+ if self.class.respond_to?(:identificator) && attribute_name == self.class.identificator
116
+ new_record?(true)
117
+ end
114
118
  end
115
119
 
116
120
 
@@ -4,10 +4,6 @@ module SmoothOperator
4
4
 
5
5
  module FinderMethods
6
6
 
7
- # def all(data = {}, options = {})
8
- # find(:all, data, options)
9
- # end
10
-
11
7
  def find(relative_path, data = {}, options = {})
12
8
  relative_path = '' if relative_path == :all
13
9
 
@@ -22,21 +18,26 @@ module SmoothOperator
22
18
  protected #################### PROTECTED ##################
23
19
 
24
20
  def build_object(parsed_response, options, from_array = false)
25
- options ||={}
26
-
27
21
  if parsed_response.is_a?(Array)
28
22
  parsed_response.map { |array_entry| build_object(array_entry, options, true) }
29
23
  elsif parsed_response.is_a?(Hash)
30
- if !from_array && parsed_response.include?(resources_name)
31
- ArrayWithMetaData.new(parsed_response, self)
24
+ if parsed_response.include?(object_class.resources_name) && !from_array
25
+ ArrayWithMetaData.new(parsed_response, object_class)
32
26
  else
33
- new(parsed_response, from_server: true)
27
+ object_class.new(parsed_response, from_server: true)
34
28
  end
35
29
  else
36
30
  parsed_response
37
31
  end
38
32
  end
39
33
 
34
+
35
+ private #################### PRIVATE ##################
36
+
37
+ def object_class
38
+ @object_class ||= self.class == Class ? self : self.class
39
+ end
40
+
40
41
  end
41
42
 
42
43
  end
@@ -53,6 +53,14 @@ module SmoothOperator
53
53
  def present?(object)
54
54
  !blank?(object)
55
55
  end
56
+
57
+ def absolute_path?(string)
58
+ present?(string) && string[0] == '/'
59
+ end
60
+
61
+ def remove_initial_slash(string)
62
+ string[1..-1]
63
+ end
56
64
 
57
65
  end
58
66
 
@@ -14,6 +14,7 @@ module SmoothOperator
14
14
  @internal_structure ||= self.class.internal_structure.dup
15
15
  end
16
16
 
17
+
17
18
  module ClassMethods
18
19
 
19
20
  def resources_name(default_bypass = nil)
@@ -8,90 +8,136 @@ module SmoothOperator
8
8
 
9
9
  module Operator
10
10
 
11
- OPTIONS = [:endpoint, :endpoint_user, :endpoint_pass, :timeout]
11
+ def make_the_call(http_verb, relative_path = '', data = {}, options = {})
12
+ options ||= {}
13
+
14
+ relative_path = resource_path(relative_path)
15
+
16
+ if !parent_object.nil? && options[:ignore_parent] != true
17
+ options[:resources_name] ||= "#{parent_object.class.resources_name}/#{parent_object.get_identificator}/#{self.class.resources_name}"
18
+ end
19
+
20
+ self.class.make_the_call(http_verb, relative_path, data, options) do |remote_call|
21
+ yield(remote_call)
22
+ end
23
+ end
24
+
25
+ protected ######################## PROTECTED ###################
12
26
 
13
- OPTIONS.each do |option|
14
- define_method(option) { Helpers.get_instance_variable(self, option, '') }
27
+ def resource_path(relative_path)
28
+ if Helpers.absolute_path?(relative_path)
29
+ Helpers.remove_initial_slash(relative_path)
30
+ elsif persisted?
31
+ Helpers.present?(relative_path) ? "#{get_identificator}/#{relative_path}" : get_identificator.to_s
32
+ else
33
+ relative_path
34
+ end
15
35
  end
16
36
 
17
- attr_writer *OPTIONS
18
-
19
- %w[get post put patch delete].each do |method|
20
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
21
- def #{method}(relative_path = '', params = {}, options = {})
22
- make_the_call(:#{method}, relative_path, params, options) do |remote_call|
23
- block_given? ? yield(remote_call) : remote_call
37
+
38
+ ########################### MODULES BELLOW ###############################
39
+
40
+ module HttpMethods
41
+
42
+ HTTP_VERBS = %w[get post put patch delete]
43
+
44
+ HTTP_VERBS.each do |method|
45
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
46
+ def #{method}(relative_path = '', params = {}, options = {})
47
+ make_the_call(:#{method}, relative_path, params, options) do |remote_call|
48
+ block_given? ? yield(remote_call) : remote_call
49
+ end
24
50
  end
25
- end
26
- RUBY
51
+ RUBY
52
+ end
53
+
27
54
  end
28
55
 
56
+ module ClassMethods
29
57
 
30
- def headers
31
- Helpers.get_instance_variable(self, :headers, {})
32
- end
58
+ OPTIONS = [:endpoint, :endpoint_user, :endpoint_pass, :timeout]
33
59
 
34
- attr_writer :headers
60
+ OPTIONS.each do |option|
61
+ define_method(option) { Helpers.get_instance_variable(self, option, '') }
62
+ end
35
63
 
64
+ attr_writer *OPTIONS
36
65
 
37
- def make_the_call(http_verb, relative_path = '', data = {}, options = {})
38
- operator_args = operator_method_args(http_verb, relative_path, data, options)
66
+ def headers
67
+ Helpers.get_instance_variable(self, :headers, {})
68
+ end
39
69
 
40
- if Helpers.present?(operator_args[4][:hydra])
41
- operator_call = Operators::Typhoeus
42
- else
43
- operator_call = Operators::Faraday
70
+ attr_writer :headers
71
+
72
+
73
+ def make_the_call(http_verb, relative_path = '', data = {}, options = {})
74
+ operator_args = operator_method_args(http_verb, relative_path, data, options)
75
+
76
+ if Helpers.present?(operator_args[4][:hydra])
77
+ operator_call = Operators::Typhoeus
78
+ else
79
+ operator_call = Operators::Faraday
80
+ end
81
+
82
+ operator_call.make_the_call(*operator_args) do |remote_call|
83
+ block_given? ? yield(remote_call) : remote_call
84
+ end
44
85
  end
45
-
46
- operator_call.make_the_call(*operator_args) do |remote_call|
47
- block_given? ? yield(remote_call) : remote_call
86
+
87
+ def query_string(params)
88
+ params
48
89
  end
49
- end
50
90
 
51
- def query_string(params)
52
- params
53
- end
54
91
 
92
+ protected #################### PROTECTED ##################
55
93
 
56
- protected #################### PROTECTED ##################
94
+ def operator_method_args(http_verb, relative_path, data, options)
95
+ options = populate_options(options)
57
96
 
58
- def operator_method_args(http_verb, relative_path, data, options)
59
- options = populate_options(options)
97
+ [http_verb, resource_path(relative_path, options), *strip_params(http_verb, data), options]
98
+ end
60
99
 
61
- [http_verb, resource_path(relative_path, options), *strip_params(http_verb, data), options]
62
- end
63
100
 
101
+ private #################### PRIVATE ##################
64
102
 
65
- private #################### PRIVATE ##################
103
+ def populate_options(options)
104
+ options ||= {}
66
105
 
67
- def populate_options(options)
68
- options ||= {}
106
+ OPTIONS.each { |option| options[option] ||= send(option) }
69
107
 
70
- OPTIONS.each { |option| options[option] ||= send(option) }
108
+ options[:headers] = headers.merge(options[:headers] || {})
109
+
110
+ options
111
+ end
71
112
 
72
- options[:headers] = headers.merge(options[:headers] || {})
73
-
74
- options
75
- end
113
+ def resource_path(relative_path, options)
114
+ _resources_name = options[:resources_name] || self.resources_name
76
115
 
77
- def resource_path(relative_path, options)
78
- _resources_name = options[:resources_name] || self.resources_name
116
+ if Helpers.present?(_resources_name)
117
+ Helpers.present?(relative_path) ? "#{_resources_name}/#{relative_path}" : _resources_name
118
+ else
119
+ relative_path.to_s
120
+ end
121
+ end
79
122
 
80
- if Helpers.present?(_resources_name)
81
- Helpers.present?(relative_path) ? "#{_resources_name}/#{relative_path}" : _resources_name
82
- else
83
- relative_path.to_s
123
+ def strip_params(http_verb, data)
124
+ data ||= {}
125
+
126
+ if [:get, :head, :delete].include?(http_verb)
127
+ [query_string(data), nil]
128
+ else
129
+ [query_string({}), data]
130
+ end
84
131
  end
132
+
85
133
  end
86
134
 
87
- def strip_params(http_verb, data)
88
- data ||= {}
89
-
90
- if [:get, :head, :delete].include?(http_verb)
91
- [query_string(data), nil]
92
- else
93
- [query_string({}), data]
94
- end
135
+
136
+ include HttpMethods
137
+
138
+ def self.included(base)
139
+ base.extend(ClassMethods)
140
+ base.extend(HttpMethods)
95
141
  end
96
142
 
97
143
  end
@@ -6,35 +6,25 @@ module SmoothOperator
6
6
  base.extend(ClassMethods)
7
7
  end
8
8
 
9
- module ClassMethods
10
-
11
- def methods_vs_http_verbs
12
- @methods_vs_http_verbs ||= { reload: :get, create: :post, update: :put, destroy: :delete }
13
- end
9
+ attr_reader :last_remote_call
14
10
 
15
- [:reload, :create, :update, :destroy].each do |method|
16
- define_method("#{method}_http_verb=") { |http_verb| methods_vs_http_verbs[method] = http_verb }
17
- end
18
-
19
- def create(attributes = nil, relative_path = nil, data = {}, options = {})
20
- new(attributes).tap { |object| object.save(relative_path, data, options) }
21
- end
22
11
 
12
+ def get_identificator
13
+ get_internal_data(self.class.identificator)
23
14
  end
24
15
 
25
-
26
16
  def reload(relative_path = nil, data = {}, options = {})
27
- raise 'UnknownPath' if Helpers.blank?(relative_path) && (!respond_to?(:id) || Helpers.blank?(id))
17
+ raise 'UnknownPath' if Helpers.blank?(relative_path) && (!respond_to?(self.class.identificator) || Helpers.blank?(get_identificator))
28
18
 
29
- make_the_call(*persistent_method_args(:reload, relative_path, data, options)) do |remote_call|
19
+ persistence_call(:reload, relative_path, data, options) do |remote_call|
30
20
  block_given? ? yield(remote_call) : remote_call.status
31
21
  end
32
22
  end
33
23
 
34
- def new_record?
35
- return @new_record if defined?(@new_record)
24
+ def new_record?(bypass_cache = false)
25
+ return @new_record if !bypass_cache && defined?(@new_record)
36
26
 
37
- @new_record = Helpers.blank?(get_internal_data("id"))
27
+ @new_record = Helpers.blank?(get_identificator)
38
28
  end
39
29
 
40
30
  def destroyed?
@@ -66,7 +56,7 @@ module SmoothOperator
66
56
  def destroy(relative_path = nil, data = {}, options = {})
67
57
  return false unless persisted?
68
58
 
69
- make_the_call(*persistent_method_args(:destroy, relative_path, data, options)) do |remote_call|
59
+ persistence_call(:destroy, relative_path, data, options) do |remote_call|
70
60
  @destroyed = true if remote_call.status
71
61
 
72
62
  block_given? ? yield(remote_call) : remote_call.status
@@ -77,7 +67,7 @@ module SmoothOperator
77
67
  protected ######################### PROTECTED ##################
78
68
 
79
69
  def create(relative_path, data, options)
80
- make_the_call(*persistent_method_args(:create, relative_path, data, options)) do |remote_call|
70
+ persistence_call(:create, relative_path, data, options) do |remote_call|
81
71
  @new_record = false if remote_call.status
82
72
 
83
73
  block_given? ? yield(remote_call) : remote_call
@@ -85,52 +75,25 @@ module SmoothOperator
85
75
  end
86
76
 
87
77
  def update(relative_path, data, options)
88
- make_the_call(*persistent_method_args(:update, relative_path, data, options)) do |remote_call|
78
+ persistence_call(:update, relative_path, data, options) do |remote_call|
89
79
  block_given? ? yield(remote_call) : remote_call
90
80
  end
91
81
  end
92
82
 
93
- def make_the_call(http_verb, relative_path, data, options)
94
- self.class.make_the_call(http_verb, relative_path, data, options) do |remote_call|
95
- @last_remote_call = remote_call
96
-
97
- returning_data = @last_remote_call.parsed_response
98
-
99
- if !@last_remote_call.error? && returning_data.is_a?(Hash)
100
- assign_attributes returning_data, from_server: true
101
- end
102
-
103
- yield(remote_call)
104
- end
105
- end
106
-
107
-
108
- private ##################### PRIVATE ####################
109
-
110
- def persistent_method_args(method, relative_path, data, options)
83
+ def persistence_call(method, relative_path, data, options)
111
84
  options ||= {}
112
85
 
113
- relative_path = resource_path(relative_path)
86
+ http_verb = options[:http_verb] || self.class.methods_vs_http_verbs[method]
114
87
 
115
- if !parent_object.nil? && options[:ignore_parent] != true
116
- options[:resources_name] ||= "#{parent_object.class.resources_name}/#{parent_object.id}/#{self.class.resources_name}"
117
- end
88
+ make_the_call(http_verb, relative_path, data, options) do |remote_call|
89
+ @last_remote_call = remote_call
118
90
 
119
- [http_verb_for(method, options), relative_path, data, options]
120
- end
91
+ if !@last_remote_call.error? && @last_remote_call.parsed_response.is_a?(Hash)
92
+ assign_attributes @last_remote_call.parsed_response, from_server: true
93
+ end
121
94
 
122
- def resource_path(relative_path)
123
- relative_path = if Helpers.present?(relative_path) && relative_path[0] == '/'
124
- relative_path[1..-1]
125
- elsif persisted?
126
- Helpers.present?(relative_path) ? "#{id}/#{relative_path}" : id.to_s
95
+ yield(remote_call)
127
96
  end
128
-
129
- relative_path
130
- end
131
-
132
- def http_verb_for(method, options)
133
- options[:http_verb] || self.class.methods_vs_http_verbs[method]
134
97
  end
135
98
 
136
99
  def data_with_object_attributes(data, options)
@@ -138,10 +101,35 @@ module SmoothOperator
138
101
 
139
102
  hash = serializable_hash(options[:serializable_options]).dup
140
103
 
141
- hash.delete('id')
104
+ hash.delete(self.class.identificator)
142
105
 
143
106
  { self.class.resource_name => hash }.merge(data)
144
107
  end
108
+
109
+
110
+ module ClassMethods
111
+
112
+ METHODS_VS_HTTP_VERBS = { reload: :get, create: :post, update: :put, destroy: :delete }
113
+
114
+ def methods_vs_http_verbs
115
+ Helpers.get_instance_variable(self, :methods_vs_http_verbs, METHODS_VS_HTTP_VERBS.dup)
116
+ end
117
+
118
+ def identificator
119
+ Helpers.get_instance_variable(self, :identificator, 'id')
120
+ end
121
+
122
+ attr_writer :identificator
123
+
124
+ METHODS_VS_HTTP_VERBS.keys.each do |method|
125
+ define_method("#{method}_http_verb=") { |http_verb| methods_vs_http_verbs[method] = http_verb }
126
+ end
127
+
128
+ def create(attributes = nil, relative_path = nil, data = {}, options = {})
129
+ new(attributes).tap { |object| object.save(relative_path, data, options) }
130
+ end
131
+
132
+ end
145
133
 
146
134
  end
147
135
 
@@ -1,3 +1,3 @@
1
1
  module SmoothOperator
2
- VERSION = "1.10.24"
2
+ VERSION = "1.11.1"
3
3
  end
@@ -109,16 +109,16 @@ shared_examples_for "a method that calls the #make_the_call method" do
109
109
 
110
110
  it "it should pass all arguments to #make_the_call" do
111
111
  _method_arguments = method_arguments.dup
112
- _method_arguments[0] = _method_arguments[0][1..-1]
112
+ _method_arguments[0] = SmoothOperator::Helpers.remove_initial_slash(_method_arguments[0])
113
113
 
114
- expect(subject).to receive(:make_the_call).with(:get, *_method_arguments)
114
+ expect(subject.class).to receive(:make_the_call).with(:get, *_method_arguments)
115
115
 
116
116
  execute_method
117
117
  end
118
118
 
119
119
  it "it should pass all arguments to .make_the_call" do
120
120
  _method_arguments = method_arguments.dup
121
- _method_arguments[0] = _method_arguments[0][1..-1]
121
+ _method_arguments[0] = SmoothOperator::Helpers.remove_initial_slash(_method_arguments[0])
122
122
 
123
123
  expect(subject.class).to receive(:make_the_call).with(:get, *_method_arguments)
124
124
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smooth_operator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.24
4
+ version: 1.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gonçalves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-21 00:00:00.000000000 Z
11
+ date: 2014-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler