service_api 0.0.6 → 0.0.7

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: 26e77d025918be1d42d11b3ef4d7924f88061655
4
- data.tar.gz: 6b742c8dcc51a5b87bccb29a83d4d2e59dc93dea
3
+ metadata.gz: a233ba5aaad956201a6d780ea2ad8cbea68665ae
4
+ data.tar.gz: f2cf1eca512dec7924ec6adec10f7fe8a1730132
5
5
  SHA512:
6
- metadata.gz: 2136fca2b43b7777dd615c0c954973fc8ed4421b8814174e40aac0a5ba2ce28573132ac3bbdffbaa06de28fed4f2a7fba4fdbed1000e629391e7c465552aaa38
7
- data.tar.gz: d6b8aed9836e1e49c4b7412faa4605a4ec51e53199cad53d2173072a961cd3b79a26e6cd81f867b994f37e1484f08c36ff52e70c44b1631a67e519f690cc6f01
6
+ metadata.gz: 8dbeb8061d5fdf16f337ee22e79555a844b9626df5775ebbb022f659f2809e5ca135ba059047734d8305c406730bc2b293335f4e4a8fadbff404f444681f0866
7
+ data.tar.gz: a5f96bc72486937e3d72bf7ecfb4565187043546d0d0d9e11db32b9dacd20d092caec937fc26764f59304ede81ee5c0627aab548adb34f02173cf68724a66d83
data/lib/service_api.rb CHANGED
@@ -2,4 +2,5 @@ module ServiceApi; end
2
2
 
3
3
  require 'service_api/version'
4
4
  require 'service_api/base_faraday'
5
+ require 'service_api/normalize_options'
5
6
  require 'service_api/uri_tokens'
@@ -65,6 +65,12 @@ module ServiceApi
65
65
  "#{base_url}#{uri}#{query_params_formatted}"
66
66
  end
67
67
 
68
+ # Mapping arguments passing to method, convert normal arguments to hash, and mapping hash keys
69
+ #
70
+ def normalize_options(mapper, *options)
71
+ NormalizeOptions.new(mapper, options).call
72
+ end
73
+
68
74
  private
69
75
 
70
76
  def connection
@@ -0,0 +1,75 @@
1
+ require 'service_api/normalize_options/base'
2
+ require 'service_api/normalize_options/hash_with_mapper_hash'
3
+ require 'service_api/normalize_options/normal_with_mapper_array'
4
+ require 'service_api/normalize_options/normal_with_mapper_hash'
5
+
6
+ class NormalizeOptions
7
+ attr_reader :mapper, :options
8
+
9
+ def initialize(mapper, *options)
10
+ @mapper = mapper
11
+ @options = options
12
+ end
13
+
14
+ def parse
15
+ if hash_options?
16
+ parse_hash_options
17
+ elsif arguments_count_corrent?
18
+ parse_normal_options
19
+ else
20
+ raise ArgumentError
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def parse_hash_options
27
+ if mapper_hash?
28
+ parse_hash_options_with_mapper_hash
29
+ else
30
+ first_options
31
+ end
32
+ end
33
+
34
+ def parse_normal_options
35
+ if mapper_hash?
36
+ parse_normal_options_with_mapper_hash
37
+ elsif mapper_array?
38
+ parse_normal_options_with_mapper_array
39
+ else
40
+ {}
41
+ end
42
+ end
43
+
44
+ def parse_hash_options_with_mapper_hash
45
+ NormalizeOptions::HashWithMapperHash.new(first_options, mapper).call
46
+ end
47
+
48
+ def parse_normal_options_with_mapper_hash
49
+ NormalizeOptions::NormalWithMapperHash.new(options, mapper).call
50
+ end
51
+
52
+ def parse_normal_options_with_mapper_array
53
+ NormalizeOptions::NormalWithMapperArray.new(options, mapper).call
54
+ end
55
+
56
+ def mapper_hash?
57
+ mapper.class == Hash
58
+ end
59
+
60
+ def mapper_array?
61
+ mapper.class == Array
62
+ end
63
+
64
+ def first_options
65
+ @first_options ||= options.first
66
+ end
67
+
68
+ def hash_options?
69
+ options.count == 1 && first_options.class == Hash
70
+ end
71
+
72
+ def arguments_count_corrent?
73
+ options.count == mapper.count
74
+ end
75
+ end
@@ -0,0 +1,11 @@
1
+ class NormalizeOptions
2
+ class Base
3
+ attr_reader :options, :mapper, :new_options
4
+
5
+ def initialize(options, mapper)
6
+ @options = options
7
+ @mapper = mapper
8
+ @new_options = {}
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class NormalizeOptions
2
+ class HashWithMapperHash < NormalizeOptions::Base
3
+ def call
4
+ mapper.each do |from, to|
5
+ new_options[to.to_sym] = options[from.to_sym]
6
+ end
7
+
8
+ new_options
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class NormalizeOptions
2
+ class NormalWithMapperArray < NormalizeOptions::Base
3
+ def call
4
+ options.each_with_index do |option, index|
5
+ new_options[mapper[index]] = option
6
+ end
7
+
8
+ new_options
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ class NormalizeOptions
2
+ class NormalWithMapperHash < NormalizeOptions::Base
3
+ def call
4
+ options.each_with_index do |option, index|
5
+ new_options[mapper[mapper_key[index]]] = option
6
+ end
7
+
8
+ new_options
9
+ end
10
+
11
+ private
12
+
13
+ def mapper_key
14
+ mapper.keys
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module ServiceApi
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe NormalizeOptions do
4
+ describe 'hash options' do
5
+ it 'return hash with mapped keys' do
6
+ expect(NormalizeOptions.new({ e1: :o1, e2: :o2 }, e1: 'test1', e2: 'test2').parse).to eq(o1: 'test1', o2: 'test2')
7
+ end
8
+
9
+ it 'return original hash' do
10
+ expect(NormalizeOptions.new([:e1, :e2], e1: 'test1', e2: 'test2').parse).to eq(e1: 'test1', e2: 'test2')
11
+ end
12
+ end
13
+
14
+ describe 'normal options' do
15
+ it 'return hash with mapped keys' do
16
+ expect(NormalizeOptions.new({ e1: :o1, e2: :o2 }, 'test1', 'test2').parse).to eq(o1: 'test1', o2: 'test2')
17
+ end
18
+
19
+ it 'return correct hash' do
20
+ expect(NormalizeOptions.new([:e1, :e2], 'test1', 'test2').parse).to eq(e1: 'test1', e2: 'test2')
21
+ end
22
+ end
23
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krzysztof Wawer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-03 00:00:00.000000000 Z
11
+ date: 2014-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -136,10 +136,16 @@ files:
136
136
  - Rakefile
137
137
  - lib/service_api.rb
138
138
  - lib/service_api/base_faraday.rb
139
+ - lib/service_api/normalize_options.rb
140
+ - lib/service_api/normalize_options/base.rb
141
+ - lib/service_api/normalize_options/hash_with_mapper_hash.rb
142
+ - lib/service_api/normalize_options/normal_with_mapper_array.rb
143
+ - lib/service_api/normalize_options/normal_with_mapper_hash.rb
139
144
  - lib/service_api/uri_tokens.rb
140
145
  - lib/service_api/version.rb
141
146
  - service_api.gemspec
142
147
  - spec/functionals/base_faraday_spec.rb
148
+ - spec/functionals/normalize_options.rb
143
149
  - spec/functionals/uri_tokens_spec.rb
144
150
  - spec/spec_helper.rb
145
151
  homepage: http://github.com/wafcio/service_api
@@ -168,5 +174,6 @@ specification_version: 4
168
174
  summary: Set of useful modules, classes for api gems
169
175
  test_files:
170
176
  - spec/functionals/base_faraday_spec.rb
177
+ - spec/functionals/normalize_options.rb
171
178
  - spec/functionals/uri_tokens_spec.rb
172
179
  - spec/spec_helper.rb