idcf-faraday_middleware 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 274199f523769c56e273e171ac12dd2fc6c3a1d3
4
- data.tar.gz: 43e5945820d09be8cf39ad6d5da27b5f703cc5c4
3
+ metadata.gz: 7b42000179d2f5df6fdd51e6827fb30e8d479b9d
4
+ data.tar.gz: 3a8aa4e79e8c585fef31536417fe2e482059bbc2
5
5
  SHA512:
6
- metadata.gz: 66fddb0a09bc01e956f7333e33ffb8272ff4448d2bc6883d167b43af7d37004efbfd4d6ce761362c78e0b1b37e01e15d63e161939b06dc7eaa521b322f816056
7
- data.tar.gz: 52145a770b275dcef3da51c2aaa04c1b5b6d40f23ab9ff46e06fedaeaeb6241b13d456c4a9634d466aa95c974af010f3d677326ca9f42f99aca1633cd59a0cfb
6
+ metadata.gz: 2cb0ddb8779f8ae3c59b9c0eaca07d758bf0e12c1dc26af2b758c825bb212b776dd15cc8046380f351bbb9b17b6c454ccde7d37c0395f9a2f6c1db0878226b58
7
+ data.tar.gz: 03b00fedfc9bbf1dfb28d8db511a91a5bc7861797148ab7d0d8fc8555f8643532674f29459340f183f1324ca0204689cf30da67f4befe7f3f6d2dfe2564b526e
@@ -15,3 +15,4 @@ ratings:
15
15
  exclude_paths:
16
16
  - spec/
17
17
  - 'vendor/**/*'
18
+ - .idea
data/README.md CHANGED
@@ -27,8 +27,6 @@ Or install it yourself as:
27
27
  ### Computing
28
28
 
29
29
  ```ruby
30
- require 'faraday'
31
- require 'faraday_middleware'
32
30
  require 'idcf/faraday_middleware'
33
31
 
34
32
  Faraday::Request.register_middleware(
@@ -37,10 +35,11 @@ Faraday::Request.register_middleware(
37
35
 
38
36
  @connection ||=
39
37
  Faraday.new(url: 'https://compute.jp-east.idcfcloud.com/') do |faraday|
40
- faraday.request :json
38
+ faraday.request :url_encoded
41
39
  faraday.request :signature, api_key: api_key, secret_key: secret_key
42
40
  faraday.response :json
43
41
  faraday.adapter Faraday.default_adapter
42
+ faraday.options.params_encoder = Faraday::FlatParamsEncoder
44
43
  end
45
44
  @connection.get '/client/api?command=listZones'
46
45
  ```
@@ -48,8 +47,6 @@ Faraday::Request.register_middleware(
48
47
  ## ILB
49
48
 
50
49
  ```ruby
51
- require 'faraday'
52
- require 'faraday_middleware'
53
50
  require 'idcf/faraday_middleware'
54
51
 
55
52
  Faraday::Request.register_middleware(
@@ -69,8 +66,6 @@ Faraday::Request.register_middleware(
69
66
  ## DNS
70
67
 
71
68
  ```ruby
72
- require 'faraday'
73
- require 'faraday_middleware'
74
69
  require 'idcf/faraday_middleware'
75
70
 
76
71
  Faraday::Request.register_middleware(
@@ -90,8 +85,6 @@ Faraday::Request.register_middleware(
90
85
  ### Billing
91
86
 
92
87
  ```ruby
93
- require 'faraday'
94
- require 'faraday_middleware'
95
88
  require 'idcf/faraday_middleware'
96
89
 
97
90
  Faraday::Request.register_middleware(
@@ -111,8 +104,6 @@ Faraday::Request.register_middleware(
111
104
  ### Content Cache
112
105
 
113
106
  ```ruby
114
- require 'faraday'
115
- require 'faraday_middleware'
116
107
  require 'idcf/faraday_middleware'
117
108
 
118
109
  Faraday::Request.register_middleware(
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_runtime_dependency 'activesupport', '>= 4.2'
25
25
  spec.add_runtime_dependency 'activemodel', '>= 4.2'
26
26
  spec.add_runtime_dependency 'faraday'
27
+ spec.add_runtime_dependency 'faraday_middleware'
27
28
 
28
29
  spec.add_development_dependency 'bundler', '~> 1.14'
29
30
  spec.add_development_dependency 'yard'
@@ -32,7 +33,6 @@ Gem::Specification.new do |spec|
32
33
  spec.add_development_dependency 'simplecov'
33
34
  spec.add_development_dependency 'pry'
34
35
  spec.add_development_dependency 'awesome_print'
35
- spec.add_development_dependency 'faraday_middleware'
36
36
 
37
37
  spec.required_ruby_version = '>= 1.9.3'
38
38
  end
@@ -1,5 +1,6 @@
1
1
  require 'base64'
2
2
  require 'faraday'
3
+ require 'faraday_middleware'
3
4
  require 'active_support'
4
5
  require 'active_support/core_ext/class/attribute'
5
6
  require 'active_support/dependencies/autoload'
@@ -16,45 +16,70 @@ module Idcf
16
16
  def call(env)
17
17
  raise InvalidKeys, errors.messages.to_s if invalid?
18
18
 
19
- query = env.url.query
20
- query = add_query_param query, 'apikey', api_key
21
- query = add_query_param query, 'response', 'json'
22
- query = add_query_param query, 'signature', signature(env)
23
- env.url.query = query
24
-
19
+ if env.body.nil?
20
+ env.url.query = make_get_params(env)
21
+ else
22
+ env.body = make_params_body(env)
23
+ end
25
24
  @app.call env
26
25
  end
27
26
 
28
27
  private
29
28
 
29
+ def make_params_body(env)
30
+ params = URI.decode_www_form(env.url.query)
31
+ params.concat(URI.decode_www_form(env.body))
32
+ params << ['apikey', api_key]
33
+ params << %w(response json)
34
+ params << ['signature', signature(URI.encode_www_form(params))]
35
+ URI.encode_www_form(params)
36
+ end
37
+
38
+ def make_get_params(env)
39
+ query = env.url.query
40
+ query = add_query_param query, 'apikey', api_key
41
+ query = add_query_param query, 'response', 'json'
42
+ add_query_param query, 'signature', signature(query)
43
+ end
44
+
30
45
  def add_query_param(query, key, value)
31
46
  query = query.to_s
32
47
  query << '&' unless query.empty?
33
48
  query << "#{Faraday::Utils.escape key}=#{Faraday::Utils.escape value}"
34
49
  end
35
50
 
36
- def signature(env)
51
+ def signature(query)
37
52
  Base64.strict_encode64(
38
53
  OpenSSL::HMAC.digest(
39
54
  OpenSSL::Digest::SHA1.new,
40
55
  secret_key,
41
- signature_seed(env)
56
+ signature_seed(query)
42
57
  )
43
58
  )
44
59
  end
45
60
 
46
- def signature_seed(env)
47
- query = env.url.query&.downcase
61
+ def signature_seed(query)
48
62
  raise InvalidParameter, 'Must be set command=...' if query.nil?
49
- query = URI.decode_www_form(query)
63
+ query = URI.decode_www_form(query.downcase)
50
64
  command_check query
51
- query.sort! { |x, y| x[0] <=> y [0] }
52
- URI.encode_www_form(query).gsub('+', '%20').downcase
65
+ query = query.sort do |x, y|
66
+ x[0] <=> y[0]
67
+ end
68
+ signeture_seed_part_decode(URI.encode_www_form(query)).downcase
69
+ end
70
+
71
+ def signeture_seed_part_decode(query)
72
+ PARTIALLY_DECODE.each do |list|
73
+ query = query.gsub(*list)
74
+ end
75
+ query
53
76
  end
54
77
 
55
78
  def command_check(query)
56
- raise InvalidParameter, 'Must be set command=...' if
57
- query.find { |x| x[0] == 'command' && x[1].present? }.nil?
79
+ list = query.find do |x|
80
+ x[0] == 'command' && x[1].present?
81
+ end
82
+ raise InvalidParameter, 'Must be set command=...' if list.nil?
58
83
  end
59
84
  end
60
85
  end
@@ -8,6 +8,12 @@ module Idcf
8
8
  EXPIRES = 'Expired'.freeze
9
9
  SIGNATURE = 'Signature'.freeze
10
10
  SIGNATURE_TTL = 600
11
+ PARTIALLY_DECODE = [
12
+ %w(+ %20),
13
+ %w(%2A *),
14
+ %w(%5B [),
15
+ %w(%5D ])
16
+ ].freeze
11
17
  end
12
18
  end
13
19
  end
@@ -1,5 +1,5 @@
1
1
  module Idcf
2
2
  module FaradayMiddleware
3
- VERSION = '0.0.1'.freeze
3
+ VERSION = '0.0.2'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: idcf-faraday_middleware
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - IDC Frontier Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-31 00:00:00.000000000 Z
11
+ date: 2017-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -150,20 +164,6 @@ dependencies:
150
164
  - - ">="
151
165
  - !ruby/object:Gem::Version
152
166
  version: '0'
153
- - !ruby/object:Gem::Dependency
154
- name: faraday_middleware
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- version: '0'
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- version: '0'
167
167
  description:
168
168
  email:
169
169
  executables: []