smooth_operator 1.30.7 → 1.30.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +9 -9
- data/lib/smooth_operator/associations/has_many_relation.rb +4 -0
- data/lib/smooth_operator/operators/faraday.rb +3 -0
- data/lib/smooth_operator/operators/faraday_encoder.rb +116 -0
- data/lib/smooth_operator/version.rb +1 -1
- data/spec/factories/user_factory.rb +17 -3
- data/spec/smooth_operator/operator_spec.rb +21 -1
- data/spec/support/test_server.rb +14 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MzFiZGRjMTk0YTBlMzU0MDg5ZmExNDllMTk4ZDYwZTcyZTRhMGYxOQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
7
|
-
|
6
|
+
ZDZkOWM3MDM4ZGM5ZmRlYzIyZmM4YjUwZDBmZDE3OTUxMWZmNzY4Mg==
|
7
|
+
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDBjNDc2YmIwODVjZmY5NTc5YWFhM2MwZTljMzNhMGYzNjgxZGVmZjA1NDY2
|
10
|
+
MjgwYjRiZWM3YzUwNDFhYzM1OTI0MDZjM2EwYzYwZTViNGI1ZTljNWFlZjI4
|
11
|
+
ZGRmZjM3ZjA1ZGFkMzY2ODBkOGM1NmE3OGEzYWNmZmY3ZjA2NGM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ODc1ODMwNWU0OGU4ZDk5MmM3MWNlNzUyNzc3NWU2NGQwMmI0NjQwZGIxM2Jj
|
14
|
+
M2VmMzEyZDYzZmY0ZWVjMmEyYWViYTc0ZGQ2NzZjNzA2YzI1M2U3YTRhMDIw
|
15
|
+
ZjRiNDM3N2FhMTliOWQ3OWRkZGE5OTE5MGJlYzI0NGFlYWZlOTM=
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'faraday'
|
2
2
|
require 'typhoeus/adapters/faraday'
|
3
3
|
require "smooth_operator/remote_call/faraday"
|
4
|
+
require "smooth_operator/operators/faraday_encoder"
|
4
5
|
|
5
6
|
module SmoothOperator
|
6
7
|
|
@@ -8,6 +9,8 @@ module SmoothOperator
|
|
8
9
|
|
9
10
|
module Faraday
|
10
11
|
|
12
|
+
# ::Faraday::Utils.default_params_encoder = FaradayEncoder
|
13
|
+
|
11
14
|
extend self
|
12
15
|
|
13
16
|
# def generate_parallel_connection
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module SmoothOperator
|
2
|
+
module Operators
|
3
|
+
|
4
|
+
module FaradayEncoder
|
5
|
+
|
6
|
+
class << self
|
7
|
+
extend Forwardable
|
8
|
+
def_delegators :'Faraday::Utils', :escape, :unescape
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.encode(params)
|
12
|
+
return nil if params == nil
|
13
|
+
|
14
|
+
if !params.is_a?(Array)
|
15
|
+
if !params.respond_to?(:to_hash)
|
16
|
+
raise TypeError,
|
17
|
+
"Can't convert #{params.class} into Hash."
|
18
|
+
end
|
19
|
+
params = params.to_hash
|
20
|
+
params = params.map do |key, value|
|
21
|
+
key = key.to_s if key.kind_of?(Symbol)
|
22
|
+
[key, value]
|
23
|
+
end
|
24
|
+
# Useful default for OAuth and caching.
|
25
|
+
# Only to be used for non-Array inputs. Arrays should preserve order.
|
26
|
+
params.sort!
|
27
|
+
end
|
28
|
+
|
29
|
+
# Helper lambda
|
30
|
+
to_query = lambda do |parent, value|
|
31
|
+
if value.is_a?(Hash)
|
32
|
+
value = value.map do |key, val|
|
33
|
+
key = escape(key)
|
34
|
+
[key, val]
|
35
|
+
end
|
36
|
+
value.sort!
|
37
|
+
buffer = ""
|
38
|
+
value.each do |key, val|
|
39
|
+
new_parent = "#{parent}%5B#{key}%5D"
|
40
|
+
buffer << "#{to_query.call(new_parent, val)}&"
|
41
|
+
end
|
42
|
+
return buffer.chop
|
43
|
+
elsif value.is_a?(Array)
|
44
|
+
buffer = ""
|
45
|
+
value.each_with_index do |val, i|
|
46
|
+
new_parent = "#{parent}%5B%5D"
|
47
|
+
buffer << "#{to_query.call(new_parent, val)}&"
|
48
|
+
end
|
49
|
+
return buffer.chop
|
50
|
+
else
|
51
|
+
encoded_value = escape(value)
|
52
|
+
return "#{parent}=#{encoded_value}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# The params have form [['key1', 'value1'], ['key2', 'value2']].
|
57
|
+
buffer = ''
|
58
|
+
params.each do |parent, value|
|
59
|
+
encoded_parent = escape(parent)
|
60
|
+
buffer << "#{to_query.call(encoded_parent, value)}&"
|
61
|
+
end
|
62
|
+
return buffer.chop
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.decode(query)
|
66
|
+
return nil if query == nil
|
67
|
+
# Recursive helper lambda
|
68
|
+
dehash = lambda do |hash|
|
69
|
+
hash.each do |(key, value)|
|
70
|
+
if value.kind_of?(Hash)
|
71
|
+
hash[key] = dehash.call(value)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
# Numeric keys implies an array
|
75
|
+
if hash != {} && hash.keys.all? { |key| key =~ /^\d+$/ }
|
76
|
+
hash.sort.inject([]) do |accu, (_, value)|
|
77
|
+
accu << value; accu
|
78
|
+
end
|
79
|
+
else
|
80
|
+
hash
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
empty_accumulator = {}
|
85
|
+
return ((query.split('&').map do |pair|
|
86
|
+
pair.split('=', 2) if pair && !pair.empty?
|
87
|
+
end).compact.inject(empty_accumulator.dup) do |accu, (key, value)|
|
88
|
+
key = unescape(key)
|
89
|
+
if value.kind_of?(String)
|
90
|
+
value = unescape(value.gsub(/\+/, ' '))
|
91
|
+
end
|
92
|
+
|
93
|
+
array_notation = !!(key =~ /\[\]$/)
|
94
|
+
subkeys = key.split(/[\[\]]+/)
|
95
|
+
current_hash = accu
|
96
|
+
for i in 0...(subkeys.size - 1)
|
97
|
+
subkey = subkeys[i]
|
98
|
+
current_hash[subkey] = {} unless current_hash[subkey]
|
99
|
+
current_hash = current_hash[subkey]
|
100
|
+
end
|
101
|
+
if array_notation
|
102
|
+
current_hash[subkeys.last] = [] unless current_hash[subkeys.last]
|
103
|
+
current_hash[subkeys.last] << value
|
104
|
+
else
|
105
|
+
current_hash[subkeys.last] = value
|
106
|
+
end
|
107
|
+
accu
|
108
|
+
end).inject(empty_accumulator.dup) do |accu, (key, value)|
|
109
|
+
accu[key] = value.kind_of?(Hash) ? dehash.call(value) : value
|
110
|
+
accu
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
FactoryGirl.define do
|
2
|
-
|
2
|
+
|
3
3
|
factory :user, class: User::Base do
|
4
|
-
|
4
|
+
|
5
5
|
id 1
|
6
6
|
admin true
|
7
7
|
last_name 'Doe'
|
@@ -19,13 +19,27 @@ FactoryGirl.define do
|
|
19
19
|
end
|
20
20
|
factory :user_with_my_method, traits: [:with_address_and_posts, :has_my_method]
|
21
21
|
|
22
|
+
trait :with_complex_hash do
|
23
|
+
alarms [{
|
24
|
+
:action=>"AMQP",
|
25
|
+
:attach=>"amqp:bus/routing_key",
|
26
|
+
:description=>{"alarm_id"=>"542d2cd24a756e6006000000", "alarm_type"=>"MedicationAlarm", "medicine_id"=>"25", "medicine_type"=>"medicine", "body"=>""},
|
27
|
+
:trigger=>{:positive=>false, :relative=>"START", :value=>"RELATIVE"}
|
28
|
+
}]
|
29
|
+
recurrence_rules [
|
30
|
+
{:by_hour=>[0], :by_minute=>[30], :by_weekday=>[], :cycle=>nil, :frequency=>"DAILY", :interval=>1, :repeat_until=>nil},
|
31
|
+
{:by_hour=>[1], :by_minute=>[0], :by_weekday=>[], :cycle=>nil, :frequency=>"DAILY", :interval=>1, :repeat_until=>nil}
|
32
|
+
]
|
33
|
+
end
|
34
|
+
factory :user_with_complex_hash, traits: [:with_complex_hash]
|
35
|
+
|
22
36
|
end
|
23
37
|
|
24
38
|
factory :white_list, class: User::Base do
|
25
39
|
id 1
|
26
40
|
first_name 'John'
|
27
41
|
end
|
28
|
-
|
42
|
+
|
29
43
|
factory :black_list, class: User::Base do
|
30
44
|
admin true
|
31
45
|
last_name 'Doe'
|
@@ -9,7 +9,17 @@ describe SmoothOperator::Operator do
|
|
9
9
|
|
10
10
|
it 'should correctly encode that hash' do
|
11
11
|
remote_call = subject.get('test_hash_with_array', attributes_for(:user_with_address_and_posts))
|
12
|
-
|
12
|
+
|
13
|
+
expect(remote_call.status).to eq(true)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
context "submiting a complex hash" do
|
19
|
+
|
20
|
+
it 'should correctly encode that hash' do
|
21
|
+
remote_call = subject.get('test_complex_hash', attributes_for(:user_with_complex_hash))
|
22
|
+
|
13
23
|
expect(remote_call.status).to eq(true)
|
14
24
|
end
|
15
25
|
|
@@ -35,6 +45,16 @@ describe SmoothOperator::Operator do
|
|
35
45
|
|
36
46
|
end
|
37
47
|
|
48
|
+
context "submiting a complex hash", current: true do
|
49
|
+
|
50
|
+
it 'should correctly encode that hash' do
|
51
|
+
remote_call = subject.post('test_complex_hash', attributes_for(:user_with_complex_hash))
|
52
|
+
|
53
|
+
expect(remote_call.status).to eq(true)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
38
58
|
it "should send the extra params set by .query_string method" do
|
39
59
|
remote_call = subject.post('test_query_string', { normal_param: true })
|
40
60
|
|
data/spec/support/test_server.rb
CHANGED
@@ -23,10 +23,18 @@ class TestServer < Sinatra::Base
|
|
23
23
|
status test_hash_with_array
|
24
24
|
end
|
25
25
|
|
26
|
+
get '/users/test_complex_hash' do
|
27
|
+
status test_complex_hash
|
28
|
+
end
|
29
|
+
|
26
30
|
post '/users/test_hash_with_array' do
|
27
31
|
status test_hash_with_array
|
28
32
|
end
|
29
33
|
|
34
|
+
post '/users/test_complex_hash' do
|
35
|
+
status test_complex_hash
|
36
|
+
end
|
37
|
+
|
30
38
|
get '/users/test_query_string' do
|
31
39
|
status test_query_string
|
32
40
|
end
|
@@ -137,6 +145,12 @@ class TestServer < Sinatra::Base
|
|
137
145
|
(params == data) ? 200 : 422
|
138
146
|
end
|
139
147
|
|
148
|
+
def test_complex_hash
|
149
|
+
data = stringify_data FactoryGirl.attributes_for(:user_with_complex_hash)
|
150
|
+
binding.pry
|
151
|
+
(params == data) ? 200 : 422
|
152
|
+
end
|
153
|
+
|
140
154
|
def test_query_string
|
141
155
|
params[:normal_param] == 'true' && params[:query_string_param] == 'true' ? 200 : 422
|
142
156
|
end
|
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.30.
|
4
|
+
version: 1.30.8
|
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-
|
11
|
+
date: 2014-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- lib/smooth_operator/operator.rb
|
98
98
|
- lib/smooth_operator/operators/connection_wrapper.rb
|
99
99
|
- lib/smooth_operator/operators/faraday.rb
|
100
|
+
- lib/smooth_operator/operators/faraday_encoder.rb
|
100
101
|
- lib/smooth_operator/operators/typhoeus.rb
|
101
102
|
- lib/smooth_operator/options.rb
|
102
103
|
- lib/smooth_operator/persistence.rb
|
@@ -153,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
154
|
version: '0'
|
154
155
|
requirements: []
|
155
156
|
rubyforge_project:
|
156
|
-
rubygems_version: 2.
|
157
|
+
rubygems_version: 2.0.7
|
157
158
|
signing_key:
|
158
159
|
specification_version: 4
|
159
160
|
summary: Simple and fully customizable alternative to ActiveResource, using faraday
|
@@ -179,3 +180,4 @@ test_files:
|
|
179
180
|
- spec/support/models/user.rb
|
180
181
|
- spec/support/models/user_with_address_and_posts.rb
|
181
182
|
- spec/support/test_server.rb
|
183
|
+
has_rdoc:
|