smooth_operator 1.22.6 → 1.30.0

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
- NzdmNTA5NmYzMTU2YzczNzYyYjRlOTdlMDkwMDRkOTlmNTI3YjBiYQ==
4
+ ZGJiZDVhYzQxYmIwMjNkZDcyZWI2ZWYwNzA5YTdkNDdlN2UzMGFjZg==
5
5
  data.tar.gz: !binary |-
6
- MmI2Mjc0YWUyYTA0MWIyZjYxNDc0N2EzZDA5Zjg1ZjIzNTk0MDBlNQ==
6
+ MjBkM2NhMWRjMThmZTg2NmM1NzA4MjVjNzM2ZTdjOTBjZjU3Mjk0OQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- N2QyOTcwYzdhMWM1YzkzYTZhMDBjMjA4NjI1NWUwZWM4NWRkZmJjMWQ2N2Vi
10
- MDZhMjQxYTA0OGFiNDYyZjUyODc0YzBkNThjNGJmOWEzMjJkNDE5OGNlZTUx
11
- ZDIwOTI2YmY0NGZhNDNiYjQyYWVhZWRjYmU4NjMxZmEwMDBhNGQ=
9
+ YzdlZWMwN2VkMDE4N2EwNjZkMjBhZmM0MzZmMTY5ZDU5Nzg1NzJiNjBkNjE2
10
+ NzRhYjVmZDJhNGE4MWIwMTBhZTExZWJjZmRmODBiMzI2ZGQ3MjliMWM1OTFl
11
+ NmEzOWNhMjRiNmQ1ZmJlNzAzMTY4ZWFiZmViMjE4ZjRmN2NmNDE=
12
12
  data.tar.gz: !binary |-
13
- YmE4NTgxNWQwMGM1NDJjMTk5ODU2ZTVmOGYzYzA1MjFlZWM5MzFhZWFkNzdl
14
- OTNlMDA1N2VkMDlkMmYzMjhiNmE1Y2EyZjJjOWVlOTk2NzY3MjJlNDcwNDI2
15
- NmM0MzQ3N2M0YmViYjZkMzc0ODZjNDk3OTdiYzU3NTQ5ZDZkNTk=
13
+ MDA1MDc3NzQxMWQ4ZDYwYzhlZjc3ZmI5YmRiNzgwMTVkMGUyNmVmN2JjZWYx
14
+ YmU2NDE2OTI0OGU1YWIyYTczM2VmMGMzY2NhN2Y4M2M0NWIyMDVhNzUzOTc0
15
+ MjYyYmUwYWQyNmQxOTNjMzJiNTQyMDhkZTE2NGFiZGU2OTgyNzQ=
data/README.md CHANGED
@@ -37,11 +37,14 @@ Or install it yourself as:
37
37
 
38
38
  ```ruby
39
39
  class MyBlogResource < SmoothOperator::Base
40
- self.endpoint = 'http://myblog.com/api/v0'
41
40
 
42
41
  # HTTP BASIC AUTH
43
- self.endpoint_user = 'admin'
44
- self.endpoint_pass = 'admin'
42
+ options endpoint_user: 'admin',
43
+ endpoint_pass: 'admin',
44
+ endpoint: 'http://myblog.com/api/v0'
45
+
46
+ # OR
47
+ # smooth_operator_options
45
48
  end
46
49
 
47
50
  class Post < MyBlogResource
@@ -134,7 +137,9 @@ post.save('/save_and_add_to_list')
134
137
  ### 2.4) Saving using HTTP Patch verb
135
138
  ```ruby
136
139
  class Page < MyBlogResource
137
- self.update_http_verb = :patch
140
+ options update_http_verb: 'patch'
141
+ # OR
142
+ #smooth_operator_options update_http_verb: 'patch'
138
143
  end
139
144
 
140
145
  page = Page.find(2)
@@ -241,13 +246,13 @@ Models
241
246
  ```ruby
242
247
  class SmoothResource < SmoothOperator::Rails
243
248
 
244
- def self.headers
245
- headers = super
249
+ options headers: :custom_headers
246
250
 
247
- headers.merge({
251
+ def self.custom_headers
252
+ {
248
253
  cookie: current_user.blog_cookie,
249
254
  "X_CSRF_TOKEN" => current_user.blog_auth_token
250
- })
255
+ }
251
256
  end
252
257
 
253
258
  protected ############## PROTECTED #################
@@ -23,7 +23,7 @@ module SmoothOperator
23
23
  include Persistence
24
24
  include FinderMethods
25
25
 
26
- self.strict_behaviour = true
26
+ options strict_behaviour: true
27
27
 
28
28
  def self.smooth_operator?
29
29
  true
@@ -38,7 +38,7 @@ module SmoothOperator
38
38
  include ActiveModel::Validations::Callbacks
39
39
  include ActiveModel::Conversion
40
40
 
41
- self.unknown_hash_class = SmoothOperator::OpenStruct
41
+ options unknown_hash_class: SmoothOperator::OpenStruct
42
42
 
43
43
  validate :validate_induced_errors, :validate_nested_objects
44
44
 
@@ -29,11 +29,9 @@ module SmoothOperator
29
29
  end
30
30
 
31
31
  def rails_serialization
32
- Helpers.get_instance_variable(self, :rails_serialization, false)
32
+ get_option :rails_serialization, false
33
33
  end
34
34
 
35
- attr_writer :rails_serialization
36
-
37
35
  protected ###################### PROTECTED ###################
38
36
 
39
37
  # TODO: THIS MUST GO TO A HELPER_METHODS MODULE
@@ -67,10 +67,8 @@ module SmoothOperator
67
67
 
68
68
  module ClassMethods
69
69
 
70
- attr_writer :unknown_hash_class
71
-
72
70
  def unknown_hash_class
73
- Helpers.get_instance_variable(self, :unknown_hash_class, ::OpenStruct)
71
+ get_option :unknown_hash_class, ::OpenStruct
74
72
  end
75
73
 
76
74
  def attributes_white_list
@@ -24,11 +24,9 @@ module SmoothOperator
24
24
  module ClassMethods
25
25
 
26
26
  def strict_behaviour
27
- Helpers.get_instance_variable(self, :strict_behaviour, false)
27
+ get_option :strict_behaviour, false
28
28
  end
29
29
 
30
- attr_writer :strict_behaviour
31
-
32
30
  end
33
31
 
34
32
  end
@@ -1,3 +1,4 @@
1
+ require "smooth_operator/options"
1
2
  require "smooth_operator/delegation"
2
3
  require "smooth_operator/validations"
3
4
  require "smooth_operator/resource_name"
@@ -8,6 +9,7 @@ require "smooth_operator/attribute_assignment"
8
9
  module SmoothOperator
9
10
  class OpenStruct
10
11
 
12
+ extend Options
11
13
  extend ResourceName
12
14
 
13
15
  include Delegation
@@ -55,17 +55,13 @@ module SmoothOperator
55
55
  OPTIONS = [:endpoint, :endpoint_user, :endpoint_pass, :timeout, :server_name]
56
56
 
57
57
  OPTIONS.each do |option|
58
- define_method(option) { Helpers.get_instance_variable(self, option, '') }
58
+ define_method(option) { get_option option, '' }
59
59
  end
60
60
 
61
- attr_writer *OPTIONS
62
-
63
61
  def headers
64
- Helpers.get_instance_variable(self, :headers, {})
62
+ get_option :headers, {}
65
63
  end
66
64
 
67
- attr_writer :headers
68
-
69
65
  def make_the_call(http_verb, relative_path = '', data = {}, options = {})
70
66
  options = HelperMethods.populate_options(self, options)
71
67
 
@@ -0,0 +1,30 @@
1
+ module SmoothOperator
2
+ module Options
3
+
4
+ def get_option(option, default, *args)
5
+ return default unless config_options.include?(option)
6
+
7
+ _option = config_options[option]
8
+
9
+ case _option
10
+ when Symbol
11
+ respond_to?(_option) ? send(_option, *args) : _option
12
+ when Proc
13
+ _option.call(*args)
14
+ else
15
+ _option
16
+ end
17
+ end
18
+
19
+ def config_options
20
+ Helpers.get_instance_variable(self, :config_options, {})
21
+ end
22
+
23
+ def smooth_operator_options(options = {})
24
+ config_options.merge!(options)
25
+ end
26
+
27
+ alias_method :options, :smooth_operator_options
28
+
29
+ end
30
+ end
@@ -49,11 +49,11 @@ module SmoothOperator
49
49
  end
50
50
 
51
51
  def save(relative_path = nil, data = {}, options = {})
52
- data = resource_body(data, options)
52
+ resource_data = resource_data_for_server(data)
53
53
 
54
54
  method = new_record? ? :create : :update
55
55
 
56
- make_a_persistence_call(method, relative_path, data, options) do |remote_call|
56
+ make_a_persistence_call(method, relative_path, resource_data, options) do |remote_call|
57
57
  @new_record = false if method == :create && remote_call.status
58
58
 
59
59
  block_given? ? yield(remote_call) : remote_call.status
@@ -78,14 +78,28 @@ module SmoothOperator
78
78
 
79
79
  protected ######################### PROTECTED ##################
80
80
 
81
- def resource_body(data, options)
82
- data = Helpers.stringify_keys(data)
81
+ def internal_data_for_server
82
+ data = self.class.get_option(:internal_data_for_server, false)
83
83
 
84
- hash = serializable_hash(options[:serializable_options]).dup
84
+ if data == false
85
+ serializable_hash.dup.tap { |hash| hash.delete(self.class.primary_key) }
86
+ else
87
+ data
88
+ end
89
+ end
90
+
91
+ def resource_data_for_server(data)
92
+ resource_data =
93
+ self.class.get_option(:resource_data_for_server, false, data)
85
94
 
86
- hash.delete(self.class.primary_key)
95
+ if resource_data == false
96
+ data = Helpers.stringify_keys(data)
97
+ resource_data = Helpers.stringify_keys(internal_data_for_server)
87
98
 
88
- { self.class.resource_name => hash }.merge(data)
99
+ { self.class.resource_name.to_s => resource_data }.merge(data)
100
+ else
101
+ resource_data
102
+ end
89
103
  end
90
104
 
91
105
  private ##################### PRIVATE ##################
@@ -93,7 +107,7 @@ module SmoothOperator
93
107
  def make_a_persistence_call(method, relative_path, data, options)
94
108
  options ||= {}
95
109
 
96
- http_verb = options[:http_verb] || self.class.methods_vs_http_verbs[method]
110
+ http_verb = options[:http_verb] || self.class.http_verb_for(method)
97
111
 
98
112
  make_the_call(http_verb, relative_path, data, options) do |remote_call|
99
113
  @last_remote_call = remote_call
@@ -110,26 +124,16 @@ module SmoothOperator
110
124
 
111
125
  METHODS_VS_HTTP_VERBS = { reload: :get, create: :post, update: :put, destroy: :delete }
112
126
 
113
- def methods_vs_http_verbs
114
- Helpers.get_instance_variable(self, :methods_vs_http_verbs, METHODS_VS_HTTP_VERBS.dup)
127
+ def http_verb_for(method)
128
+ get_option "#{method}_http_verb".to_sym, METHODS_VS_HTTP_VERBS[method]
115
129
  end
116
130
 
117
131
  def primary_key
118
- Helpers.get_instance_variable(self, :primary_key, 'id')
132
+ get_option :primary_key, 'id'
119
133
  end
120
134
 
121
- attr_writer :primary_key
122
-
123
135
  def destroy_key
124
- Helpers.get_instance_variable(self, :destroy_key, '_destroy')
125
- end
126
-
127
- attr_writer :destroy_key
128
-
129
- METHODS_VS_HTTP_VERBS.keys.each do |method|
130
- define_method("#{method}_http_verb=") do |http_verb|
131
- methods_vs_http_verbs[method] = http_verb
132
- end
136
+ get_option :destroy_key, '_destroy'
133
137
  end
134
138
 
135
139
  def create(attributes = nil, relative_path = nil, data = {}, options = {})
@@ -1,22 +1,14 @@
1
1
  module SmoothOperator
2
2
  module ResourceName
3
3
 
4
- def resources_name(default_bypass = nil)
5
- return @resources_name if defined?(@resources_name)
6
-
7
- (Helpers.super_method(self, :resources_name, true) || (default_bypass ? nil : self.resource_name.pluralize))
4
+ def resources_name
5
+ get_option :resources_name, self.resource_name.pluralize
8
6
  end
9
7
 
10
- attr_writer :resources_name
11
-
12
- def resource_name(default_bypass = nil)
13
- return @resource_name if defined?(@resource_name)
14
-
15
- (Helpers.super_method(self, :resource_name, true) || (default_bypass ? nil : self.model_name.to_s.underscore))
8
+ def resource_name
9
+ get_option :resource_name, self.model_name.to_s.underscore
16
10
  end
17
11
 
18
- attr_writer :resource_name
19
-
20
12
  def model_name
21
13
  return '' if custom_model_name == :none
22
14
 
@@ -103,7 +103,7 @@ module SmoothOperator
103
103
  end
104
104
 
105
105
  def new_unknown_hash(hash, unknown_hash_class, parent_object)
106
- if unknown_hash_class == :none
106
+ if unknown_hash_class.nil?
107
107
  hash
108
108
  else
109
109
  unknown_hash_class.new(cast_params(hash, unknown_hash_class, parent_object))
@@ -28,11 +28,9 @@ module SmoothOperator
28
28
  module ClassMethods
29
29
 
30
30
  def errors_key
31
- Helpers.get_instance_variable(self, :errors_key, 'errors')
31
+ get_option :errors_key, 'errors'
32
32
  end
33
33
 
34
- attr_writer :errors_key
35
-
36
34
  end
37
35
 
38
36
  end
@@ -1,3 +1,3 @@
1
1
  module SmoothOperator
2
- VERSION = "1.22.6"
2
+ VERSION = '1.30.0'
3
3
  end
@@ -89,7 +89,7 @@ describe SmoothOperator::AttributeAssignment do
89
89
  end
90
90
  end
91
91
 
92
- context "when the .unknown_hash_class is set to :none" do
92
+ context "when the .unknown_hash_class is set to nil" do
93
93
  subject { User::UnknownHashClass::None.new(creator: { first_name: 'admin', address: { street: 'something' } }) }
94
94
 
95
95
  it "the hash will be copied as it is" do
@@ -1,12 +1,12 @@
1
1
  class Address < SmoothOperator::Base
2
2
 
3
- self.resource_name = ''
4
-
5
- self.endpoint_user = 'admin'
6
- self.endpoint_pass = 'admin'
7
-
8
- self.endpoint = 'http://localhost:4567/'
9
-
10
- self.headers = { "X-APPTOKEN" => "joaquim_app_token", "X-LAYERTOKEN" => "joaquim_layer_token" }
3
+ options resource_name: '',
4
+ endpoint_user: 'admin',
5
+ endpoint_pass: 'admin',
6
+ endpoint: 'http://localhost:4567/',
7
+ headers: {
8
+ "X-APPTOKEN" => "app_token",
9
+ "X-LAYERTOKEN" => "layer_token"
10
+ }
11
11
 
12
12
  end
@@ -1,5 +1,5 @@
1
1
  class Comment < SmoothOperator::Base
2
2
 
3
- self.unknown_hash_class = SmoothOperator::OpenStruct
3
+ options unknown_hash_class: SmoothOperator::OpenStruct
4
4
 
5
5
  end
@@ -1,14 +1,10 @@
1
1
  class Post < SmoothOperator::Base
2
2
 
3
- self.endpoint_user = 'admin'
4
-
5
- self.endpoint_pass = 'admin'
6
-
7
- self.endpoint = 'http://localhost:4567/'
8
-
9
- self.rails_serialization = true
10
-
11
- self.unknown_hash_class = SmoothOperator::OpenStruct
3
+ options endpoint_user: 'admin',
4
+ endpoint_pass: 'admin',
5
+ rails_serialization: true,
6
+ endpoint: 'http://localhost:4567/',
7
+ unknown_hash_class: SmoothOperator::OpenStruct
12
8
 
13
9
  has_many :comments#, rails_serialization: true
14
10
 
@@ -2,13 +2,10 @@ module User
2
2
 
3
3
  class Base < SmoothOperator::Base
4
4
 
5
- self.resource_name = 'user'
6
-
7
- self.endpoint_user = 'admin'
8
-
9
- self.endpoint_pass = 'admin'
10
-
11
- self.endpoint = 'http://localhost:4567/'
5
+ options resource_name: 'user',
6
+ endpoint_user: 'admin',
7
+ endpoint_pass: 'admin',
8
+ endpoint: 'http://localhost:4567/'
12
9
 
13
10
  def self.query_string(params)
14
11
  params['query_string_param'] = true
@@ -21,21 +18,21 @@ module User
21
18
  module UnknownHashClass
22
19
 
23
20
  class OpenStructBase < User::Base
24
- self.unknown_hash_class = SmoothOperator::OpenStruct
21
+ options unknown_hash_class: SmoothOperator::OpenStruct
25
22
  end
26
23
 
27
24
  class None < User::Base
28
- self.unknown_hash_class = :none
25
+ options unknown_hash_class: nil
29
26
  end
30
27
 
31
28
  end
32
29
 
33
30
  class BrokenConnection < SmoothOperator::Base
34
- self.endpoint = 'http://localhost:1234/'
31
+ options endpoint: 'http://localhost:1234/'
35
32
  end
36
33
 
37
34
  class TimeoutConnection < Base
38
- self.timeout = 1
35
+ options timeout: 1
39
36
  end
40
37
 
41
38
  end
@@ -2,7 +2,7 @@ module UserWithAddressAndPosts
2
2
 
3
3
  class Father < User::Base
4
4
 
5
- self.resource_name = 'user'
5
+ options resource_name: 'user'
6
6
 
7
7
  schema(
8
8
  posts: Post,
@@ -27,13 +27,13 @@ module UserWithAddressAndPosts
27
27
 
28
28
  class SoftBehaviour < Son
29
29
 
30
- self.strict_behaviour = false
30
+ options strict_behaviour: false
31
31
 
32
32
  end
33
33
 
34
34
  class WithPatch < Son
35
35
 
36
- self.update_http_verb = :patch
36
+ options update_http_verb: 'patch'
37
37
 
38
38
  end
39
39
 
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.22.6
4
+ version: 1.30.0
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-06-17 00:00:00.000000000 Z
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,6 +98,7 @@ files:
98
98
  - lib/smooth_operator/operators/connection_wrapper.rb
99
99
  - lib/smooth_operator/operators/faraday.rb
100
100
  - lib/smooth_operator/operators/typhoeus.rb
101
+ - lib/smooth_operator/options.rb
101
102
  - lib/smooth_operator/persistence.rb
102
103
  - lib/smooth_operator/remote_call/base.rb
103
104
  - lib/smooth_operator/remote_call/errors/connection_failed.rb