smooth_operator 1.10.0 → 1.10.3

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
- ZjJmMWRkYmQxMzdiOTM2MDJiYzViZTk5ZmY1MDI1OGUxYWM0MjAyZg==
4
+ ZDQ4MTZhNjkzMDYwMjRmNTUwYzZhMjU3YzZlZjFlZWE2YTJjODE3Zg==
5
5
  data.tar.gz: !binary |-
6
- MGZiODc4YzY5OTAwY2EzYzRkNTE4NWJlOWNjMmM3YTliNTg0ZWM4Yg==
6
+ MGVhNzg5MzQyNTU0ZWVlMDZmMmEwZjcxNjlkOTI4MWMxNTc0MWQxYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OGRhMjljNWE2MmI4YmRkZTkwN2FhYzIxMDdhYzhiMjA0Y2Y3NTJiYTk4NTAz
10
- ZmQ2MmI2YWRkMzI4ODBjZGI2YTY3N2U4ZjY3NTRhZTk2MjUyNzhhNDdiMDU3
11
- ZWNmZWY3NjExMjQyY2E1NjNjNDhiNTM5NjlhNDhiN2RlNGM2ZGY=
9
+ MmUzOTIxOTgwNWE2ZGM1NjhiNmMzYzE0YjA1MGNhYTRiMzhlMTRlOWVhY2Mw
10
+ NDdlZWU2YmEyZmVkMDY2MTFhYTdjYmFjN2Q4ZGIyYjM5YTFkMzllNmY2M2Jl
11
+ NmRlYjRkZGIwM2Q1NTAzZTg3ZjcxNWJkNzEzMmJiNmMzZTcyZWE=
12
12
  data.tar.gz: !binary |-
13
- MDRkZmQxNGMwODg4MmQ0NTdhYWU3NGYyYWQ4MDU3YzAxZGQyMmUxZmIzMWU1
14
- ZjU5NGI0ZjhlZjMyOWIwNTExZTVlMjY2Y2RiOGZiOWYyZmMwZWM3ZTE2Mzc5
15
- MGJhOGNhZDdkMDU1YTJmZGFmNDQzMWE3NDUxOGM5ZjAxZDdlMDI=
13
+ MTE2YzIyNzVkMjRhZGY3MGMyOTdmNzAyODQ5ODY0NjI3ZDNkMmVkYzYwZTBk
14
+ MmY0MDVhNmQ4ZDM3Mjc0MTc2NzMxMjFlN2I0ZjY2NTBjMjY0ZGUxZWU4MmM1
15
+ MjE5YjE0NDc3Y2Y1ZWUyYWE5MjU4ZjY0NzcyY2ZlYzM4NjM0MTA=
data/console.rb CHANGED
@@ -7,14 +7,8 @@ require "spec/spec_helper"
7
7
 
8
8
  #User::Base.post('', { user: { age: 1, posts: [{ body: 'post1' }, 2] } })
9
9
 
10
-
11
- conn = SmoothOperator::Base.generate_parallel_connection
12
-
13
- conn.in_parallel do
14
- remote_call = User::Base.find('http://localhost:4567/users', nil, { connection: conn })
15
- end
16
-
17
- remote_call
10
+ #user = UserWithAddressAndPosts::Son.new(FactoryGirl.attributes_for(:user_with_address_and_posts))
11
+ #user.save('', { status: 200 })
18
12
 
19
13
  binding.pry
20
14
 
@@ -43,6 +43,8 @@ module SmoothOperator
43
43
  case object
44
44
  when String
45
45
  object.to_s == ''
46
+ when Array
47
+ object.empty?
46
48
  else
47
49
  object.nil?
48
50
  end
@@ -1,7 +1,9 @@
1
+ require "smooth_operator/operators/base"
1
2
  require "smooth_operator/remote_call/base"
2
- require "smooth_operator/operator_call/base"
3
- require "smooth_operator/operator_call/faraday"
4
- require "smooth_operator/operator_call/typhoeus"
3
+ require "smooth_operator/operators/faraday"
4
+ require "smooth_operator/operators/typhoeus"
5
+ require "smooth_operator/remote_call/errors/timeout"
6
+ require "smooth_operator/remote_call/errors/connection_failed"
5
7
 
6
8
  module SmoothOperator
7
9
 
@@ -43,12 +45,20 @@ module SmoothOperator
43
45
 
44
46
  def make_the_call(http_verb, relative_path = '', data = {}, options = {})
45
47
  if Helpers.present?(options[:hydra])
46
- operator_call = OperatorCall::Faraday.new(self, http_verb, relative_path, data, options)
48
+ operator_call = Operators::Typhoeus.new(self, http_verb, relative_path, data, options)
47
49
  else
48
- operator_call = OperatorCall::Typhoeus.new(self, http_verb, relative_path, data, options)
50
+ operator_call = Operators::Faraday.new(self, http_verb, relative_path, data, options)
49
51
  end
50
52
 
51
- operator_call.make_the_call
53
+ remote_call = {}
54
+
55
+ operator_call.make_the_call do |_remote_call|
56
+ remote_call = _remote_call
57
+
58
+ yield(remote_call) if block_given?
59
+ end
60
+
61
+ remote_call
52
62
  end
53
63
 
54
64
 
@@ -1,6 +1,6 @@
1
1
  module SmoothOperator
2
2
 
3
- module OperatorCall
3
+ module Operators
4
4
 
5
5
  class Base
6
6
 
@@ -25,7 +25,7 @@ module SmoothOperator
25
25
 
26
26
  def strip_params(http_verb, data)
27
27
  data ||= {}
28
-
28
+
29
29
  ([:get, :head, :delete].include?(http_verb) ? [@operator_class.query_string(data), nil] : [@operator_class.query_string({}), data])
30
30
  end
31
31
 
@@ -1,14 +1,15 @@
1
1
  require 'faraday'
2
2
  require 'typhoeus/adapters/faraday'
3
+ require "smooth_operator/remote_call/faraday"
3
4
 
4
5
  module SmoothOperator
5
6
 
6
- module OperatorCall
7
+ module Operators
7
8
 
8
9
  class Faraday < Base
9
10
 
10
11
  def make_the_call
11
- begin
12
+ remote_call = begin
12
13
  set_basic_authentication
13
14
 
14
15
  response = connection.send(http_verb) do |request|
@@ -20,12 +21,16 @@ module SmoothOperator
20
21
  request.body = body
21
22
  end
22
23
 
23
- RemoteCall::Base.new(response)
24
+ RemoteCall::Faraday.new(response)
24
25
  rescue ::Faraday::Error::ConnectionFailed
25
26
  RemoteCall::Errors::ConnectionFailed.new(response)
26
27
  rescue ::Faraday::Error::TimeoutError
27
28
  RemoteCall::Errors::Timeout.new(response)
28
29
  end
30
+
31
+ yield(remote_call) if block_given?
32
+
33
+ remote_call
29
34
  end
30
35
 
31
36
 
@@ -0,0 +1,73 @@
1
+ require 'typhoeus'
2
+ require "smooth_operator/remote_call/typhoeus"
3
+
4
+ module SmoothOperator
5
+
6
+ module Operators
7
+
8
+ class Typhoeus < Base
9
+
10
+ def make_the_call
11
+ set_basic_authentication
12
+
13
+ request = ::Typhoeus::Request.new(url, typhoeus_options)
14
+
15
+ remote_call = {}
16
+
17
+ hydra.queue(request)
18
+
19
+ request.on_complete do |typhoeus_response|
20
+ remote_call_class = if typhoeus_response.timed_out?
21
+ RemoteCall::Errors::Timeout
22
+ else
23
+ RemoteCall::Typhoeus
24
+ end
25
+ binding.pry
26
+ remote_call = remote_call_class.new(typhoeus_response)
27
+
28
+ yield(remote_call) if block_given?
29
+ end
30
+
31
+ hydra.run
32
+
33
+ remote_call
34
+ end
35
+
36
+
37
+ protected ################ PROTECTED ################
38
+
39
+ def set_basic_authentication
40
+ typhoeus_options[:userpwd] = "#{endpoint_user}:#{endpoint_pass}" if Helpers.present?(endpoint_user)
41
+ end
42
+
43
+ def url
44
+ url = (options[:endpoint] || operator_class.endpoint)
45
+
46
+ slice = url[-1] != '/' ? '/' : ''
47
+
48
+ url = "#{url}#{slice}#{relative_path}" if Helpers.present?(relative_path)
49
+ end
50
+
51
+ def typhoeus_options
52
+ return @typhoeus_options if defined?(@typhoeus_options)
53
+
54
+ @typhoeus_options = { method: http_verb, headers: options[:headers] }
55
+
56
+ timeout = (options[:timeout] || operator_class.timeout)
57
+
58
+ @typhoeus_options[:timeout] = timeout if Helpers.present?(timeout)
59
+
60
+ @typhoeus_options[:body] = body if Helpers.present?(body)
61
+ @typhoeus_options[:params] = params if Helpers.present?(params)
62
+
63
+ @typhoeus_options
64
+ end
65
+
66
+ def hydra
67
+ @hydra ||= operator_options[:hydra] || ::Typhoeus::Hydra::hydra
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
@@ -27,6 +27,8 @@ module SmoothOperator
27
27
  end
28
28
 
29
29
 
30
+ attr_accessor :extra_params
31
+
30
32
  def new_record?
31
33
  return @new_record if defined?(@new_record)
32
34
 
@@ -56,9 +58,13 @@ module SmoothOperator
56
58
 
57
59
  relative_path = id.to_s if Helpers.blank?(relative_path)
58
60
 
59
- success = make_remote_call(self.class.methods_http_verbs[:destroy], relative_path, data, options)
61
+ success = {}
62
+
63
+ success = make_remote_call(self.class.methods_http_verbs[:destroy], relative_path, data, options) do |remote_call|
64
+ success = remote_call.status
60
65
 
61
- @destroyed = true if success
66
+ @destroyed = true if success
67
+ end
62
68
 
63
69
  success
64
70
  end
@@ -71,34 +77,49 @@ module SmoothOperator
71
77
  end
72
78
 
73
79
  def create(relative_path, data, options)
74
- success = make_remote_call(self.class.methods_http_verbs[:create], relative_path, data, options)
80
+ success = {}
81
+
82
+ make_remote_call(self.class.methods_http_verbs[:create], relative_path, data, options) do |remote_call|
83
+ success = remote_call.status
75
84
 
76
- @new_record = false if success
85
+ @new_record = false if success
86
+ end
77
87
 
78
88
  success
79
89
  end
80
90
 
81
91
  def update(relative_path, data, options)
82
92
  relative_path = id.to_s if Helpers.blank?(relative_path)
93
+
94
+ success = {}
95
+
96
+ make_remote_call(self.class.methods_http_verbs[:save], relative_path, data, options) do |remote_call|
97
+ success = remote_call.status
98
+ end
83
99
 
84
- make_remote_call(self.class.methods_http_verbs[:save], relative_path, data, options)
100
+ success
85
101
  end
86
102
 
87
103
 
88
104
  private ##################### PRIVATE ####################
89
105
 
90
106
  def make_remote_call(http_verb, relative_path, data, options)
107
+ data ||= {}
108
+ data.merge!(extra_params || {})
109
+
91
110
  data, options = build_remote_call_args(http_verb, data, options)
92
111
 
93
- @last_remote_call = self.class.send(http_verb, relative_path, data, options)
112
+ self.class.make_the_call(http_verb, relative_path, data, options) do |remote_call|
113
+ @last_remote_call = remote_call
94
114
 
95
- returning_data = @last_remote_call.parsed_response
115
+ returning_data = @last_remote_call.parsed_response
96
116
 
97
- if !@last_remote_call.error? && returning_data.is_a?(Hash)
98
- assign_attributes returning_data.include?(model_name) ? returning_data[model_name] : returning_data
99
- end
117
+ if !@last_remote_call.error? && returning_data.is_a?(Hash)
118
+ assign_attributes returning_data.include?(model_name) ? returning_data[model_name] : returning_data
119
+ end
100
120
 
101
- @last_remote_call.status
121
+ yield(remote_call)
122
+ end
102
123
  end
103
124
 
104
125
  def build_remote_call_args(http_verb, data, options)
@@ -1,7 +1,3 @@
1
- require "smooth_operator/remote_call/errors/base"
2
- require "smooth_operator/remote_call/errors/timeout"
3
- require "smooth_operator/remote_call/errors/connection_failed"
4
-
5
1
  module SmoothOperator
6
2
 
7
3
  module RemoteCall
@@ -10,30 +6,32 @@ module SmoothOperator
10
6
 
11
7
  extend Forwardable
12
8
 
13
- attr_reader :response
9
+ attr_reader :response, :http_status, :body, :headers
14
10
 
15
11
  attr_accessor :object
16
12
 
17
- def_delegator :response, :status, :http_status
18
-
19
- def_delegators :response, :success?, :headers, :body
20
-
21
- alias :ok? :success?
22
-
23
13
  def initialize(response)
24
14
  @response = response
25
15
  end
26
16
 
27
17
 
18
+ def success?
19
+ http_status.between?(200, 299)
20
+ end
21
+
22
+ alias :ok? :success?
23
+
28
24
  def failure?
29
25
  http_status.between?(400, 499)
30
26
  end
31
27
 
32
28
  def error?
33
- http_status.between?(500, 599)
29
+ http_status.between?(500, 599) || http_status == 0
34
30
  end
35
31
 
36
32
  def parsed_response
33
+ return nil if body.nil?
34
+
37
35
  require 'json' unless defined? JSON
38
36
 
39
37
  begin
@@ -3,6 +3,12 @@ module SmoothOperator
3
3
  module Errors
4
4
 
5
5
  class ConnectionFailed < Base
6
+
7
+ def initialize(response)
8
+ @response = response
9
+ @http_status = 0
10
+ end
11
+
6
12
  end
7
13
 
8
14
  end
@@ -3,6 +3,12 @@ module SmoothOperator
3
3
  module Errors
4
4
 
5
5
  class Timeout < Base
6
+
7
+ def initialize(response)
8
+ @response = response
9
+ @http_status = 0
10
+ end
11
+
6
12
  end
7
13
 
8
14
  end
@@ -0,0 +1,19 @@
1
+ module SmoothOperator
2
+
3
+ module RemoteCall
4
+
5
+ class Faraday < Base
6
+
7
+ def initialize(response)
8
+ @response = response
9
+
10
+ @body = response.body
11
+ @headers = response.headers
12
+ @http_status = response.status
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ module SmoothOperator
2
+
3
+ module RemoteCall
4
+
5
+ class Typhoeus < Base
6
+
7
+ def initialize(response)
8
+ @response = response
9
+
10
+ @body = response.body
11
+ @http_status = response.code
12
+ @headers = response.headers_hash
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -1,3 +1,3 @@
1
1
  module SmoothOperator
2
- VERSION = "1.10.0"
2
+ VERSION = "1.10.3"
3
3
  end
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.add_dependency "json"
27
27
  spec.add_dependency 'typhoeus'
28
- spec.add_dependency 'faraday', '~> 0.8.1'
28
+ spec.add_dependency 'faraday', '~> 0.8.9'
29
29
  end
@@ -9,7 +9,7 @@ shared_examples_for "successful persistent remote call" do
9
9
  it "it should populate 'last_remote_call' with the remote_call used on this transaction" do
10
10
  expect(subject.last_remote_call).to be_nil unless method_to_execute.to_s =~ /create/
11
11
  execute_method
12
- expect(subject.last_remote_call).to be_instance_of(SmoothOperator::RemoteCall::Base)
12
+ expect(subject.last_remote_call).to be_a_kind_of(SmoothOperator::RemoteCall::Base)
13
13
  end
14
14
  end
15
15
 
@@ -70,7 +70,7 @@ shared_examples_for "persistent remote call" do
70
70
  it "it should populate 'last_remote_call' with the remote_call used on this transaction" do
71
71
  expect(subject.last_remote_call).to be_nil unless method_to_execute.to_s =~ /create/
72
72
  execute_method
73
- expect(subject.last_remote_call).to be_instance_of(SmoothOperator::RemoteCall::Base)
73
+ expect(subject.last_remote_call).to be_a_kind_of(SmoothOperator::RemoteCall::Base)
74
74
  end
75
75
 
76
76
  it "it should assert the subject's persistence" do
@@ -2,6 +2,8 @@ module User
2
2
 
3
3
  class Base < SmoothOperator::Base
4
4
 
5
+ # self.timeout = 2
6
+
5
7
  self.model_name = 'user'
6
8
 
7
9
  self.endpoint_user = 'admin'
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.0
4
+ version: 1.10.3
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-06 00:00:00.000000000 Z
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 0.8.1
61
+ version: 0.8.9
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
- version: 0.8.1
68
+ version: 0.8.9
69
69
  description: ActiveResource alternative
70
70
  email:
71
71
  - goncalves.joao@gmail.com
@@ -91,14 +91,15 @@ files:
91
91
  - lib/smooth_operator/model_schema.rb
92
92
  - lib/smooth_operator/open_struct.rb
93
93
  - lib/smooth_operator/operator.rb
94
- - lib/smooth_operator/operator_call/base.rb
95
- - lib/smooth_operator/operator_call/faraday.rb
96
- - lib/smooth_operator/operator_call/typhoeus.rb
94
+ - lib/smooth_operator/operators/base.rb
95
+ - lib/smooth_operator/operators/faraday.rb
96
+ - lib/smooth_operator/operators/typhoeus.rb
97
97
  - lib/smooth_operator/persistence.rb
98
98
  - lib/smooth_operator/remote_call/base.rb
99
- - lib/smooth_operator/remote_call/errors/base.rb
100
99
  - lib/smooth_operator/remote_call/errors/connection_failed.rb
101
100
  - lib/smooth_operator/remote_call/errors/timeout.rb
101
+ - lib/smooth_operator/remote_call/faraday.rb
102
+ - lib/smooth_operator/remote_call/typhoeus.rb
102
103
  - lib/smooth_operator/serialization.rb
103
104
  - lib/smooth_operator/translation.rb
104
105
  - lib/smooth_operator/type_converter.rb
@@ -1,40 +0,0 @@
1
- require 'typhoeus'
2
-
3
- module SmoothOperator
4
-
5
- module OperatorCall
6
-
7
- class Typhoeus < Base
8
-
9
- def make_the_call
10
- begin
11
- set_basic_authentication
12
-
13
- response = connection.send(http_verb) do |request|
14
- operator_options.each { |key, value| request.options.send("#{key}=", value) }
15
- options[:headers].each { |key, value| request.headers[key] = value }
16
- params.each { |key, value| request.params[key] = value }
17
-
18
- request.url relative_path
19
- request.body = body
20
- end
21
-
22
- RemoteCall::Base.new(response)
23
- rescue ::Faraday::Error::ConnectionFailed
24
- RemoteCall::Errors::ConnectionFailed.new(response)
25
- rescue ::Faraday::Error::TimeoutError
26
- RemoteCall::Errors::Timeout.new(response)
27
- end
28
- end
29
-
30
-
31
- protected ################ PROTECTED ################
32
-
33
- def set_basic_authentication
34
- connection.basic_auth(endpoint_user, endpoint_pass) if Helpers.present?(endpoint_user)
35
- end
36
-
37
- end
38
-
39
- end
40
- end
@@ -1,27 +0,0 @@
1
- module SmoothOperator
2
- module RemoteCall
3
- module Errors
4
-
5
- class Base
6
-
7
- attr_reader :response, :data, :object, :objects, :parsed_response, :status, :headers, :body
8
-
9
- def initialize(response)
10
- @response = response
11
- end
12
-
13
- def http_status; 0; end
14
-
15
- def error?; true; end
16
-
17
- def success?; false; end
18
-
19
- def failure?; false; end
20
-
21
- alias :ok? :success?
22
-
23
- end
24
-
25
- end
26
- end
27
- end