haveapi-client 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ module HaveAPI::Client
2
+ class Parameters::Typed
3
+ module Boolean
4
+ def self.to_b(str)
5
+ return true if str === true
6
+ return true if str =~ /^(true|t|yes|y|1)$/i
7
+
8
+ return false if str === false
9
+ return false if str =~ /^(false|f|no|n|0)$/i
10
+
11
+ false
12
+ end
13
+ end
14
+
15
+ attr_reader :errors, :value
16
+
17
+ def initialize(params, desc, value)
18
+ @params = params
19
+ @desc = desc
20
+ @value = coerce(value)
21
+ @errors = []
22
+ end
23
+
24
+ def valid?
25
+ ret = Validator.validate(@desc[:validators], @value, @params)
26
+
27
+ @errors.concat(ret) unless ret === true
28
+ ret === true
29
+ end
30
+
31
+ def to_api
32
+ if @desc[:type] == 'Datetime' && value.is_a?(Time)
33
+ value.iso8601
34
+
35
+ else
36
+ value
37
+ end
38
+ end
39
+
40
+ protected
41
+ def coerce(raw)
42
+ type = @desc[:type]
43
+
44
+ val = if type == 'Integer'
45
+ raw.to_i
46
+
47
+ elsif type == 'Float'
48
+ raw.to_f
49
+
50
+ elsif type == 'Boolean'
51
+ Boolean.to_b(raw)
52
+
53
+ elsif type == 'Datetime'
54
+ begin
55
+ Time.iso8601(raw)
56
+
57
+ rescue ArgumentError
58
+ @errors << 'not in ISO 8601 format'
59
+ nil
60
+ end
61
+
62
+ elsif type == 'String'
63
+ raw.to_s
64
+
65
+ else
66
+ raw
67
+ end
68
+
69
+ val
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,72 @@
1
+ module HaveAPI::Client
2
+ module Parameters ; end
3
+
4
+ class Params
5
+ attr_reader :errors, :params
6
+
7
+ def initialize(action, data)
8
+ @action = action
9
+ @data = data
10
+ @params = {}
11
+ @errors = {}
12
+ coerce
13
+ end
14
+
15
+ def coerce
16
+ @action.input_params.each do |name, p|
17
+ next unless @data.has_key?(name)
18
+
19
+ if p[:type] == 'Resource'
20
+ @params[name] = Parameters::Resource.new(self, p, @data[name])
21
+
22
+ else
23
+ @params[name] = Parameters::Typed.new(self, p, @data[name])
24
+ end
25
+ end
26
+ end
27
+
28
+ def valid?
29
+ @action.input_params.each do |name, p|
30
+ next if p[:validators].nil?
31
+
32
+ if p[:validators][:presence] && @params[name].nil?
33
+ error(name, 'required parameter missing')
34
+
35
+ elsif @params[name].nil?
36
+ next
37
+ end
38
+
39
+ if !@params[name].valid?
40
+ error(name, @params[name].errors)
41
+ end
42
+
43
+ end
44
+
45
+ @errors.empty?
46
+ end
47
+
48
+ def to_api
49
+ ret = {}
50
+
51
+ @params.each do |name, p|
52
+ ret[name] = p.to_api
53
+ end
54
+
55
+ ret[:meta] = @data[:meta] if @data.has_key?(:meta)
56
+
57
+ ret
58
+ end
59
+
60
+ protected
61
+ def error(param, msg)
62
+ @errors[param] ||= []
63
+
64
+ if msg.is_a?(::Array)
65
+ @errors[param].concat(msg)
66
+
67
+ else
68
+ @errors[param] << msg
69
+ end
70
+ end
71
+ end
72
+ end
@@ -21,10 +21,12 @@ module HaveAPI::Client
21
21
  if response
22
22
  if response.is_a?(Hash)
23
23
  @params = response
24
+ @prepared_args = response[:_meta][:url_params]
24
25
 
25
26
  else
26
27
  @response = response
27
28
  @params = response.response
29
+ @prepared_args = response.meta[:url_params]
28
30
  end
29
31
 
30
32
  setup_from_clone(resource)
@@ -0,0 +1,55 @@
1
+ module HaveAPI::Client
2
+ module Validators ; end
3
+
4
+ class Validator
5
+ class << self
6
+ def name(v)
7
+ Validator.register(v, self)
8
+ end
9
+
10
+ def register(name, klass)
11
+ @validators ||= {}
12
+ @validators[name] = klass
13
+ end
14
+
15
+ def validate(validators, param, other_params)
16
+ ret = []
17
+
18
+ validators.each do |name, desc|
19
+ fail "unsupported validator '#{name}'" if @validators[name].nil?
20
+
21
+ v = @validators[name].new(desc, param, other_params)
22
+ ret.concat(v.errors) unless v.valid?
23
+ end
24
+
25
+ ret.empty? ? true : ret
26
+ end
27
+ end
28
+
29
+ attr_reader :value, :params
30
+
31
+ def initialize(opts, value, other_params)
32
+ @opts = opts
33
+ @value = value
34
+ @params = other_params.params
35
+ end
36
+
37
+ def errors
38
+ @errors || [opts[:message] % { value: value }]
39
+ end
40
+
41
+ def valid?
42
+ raise NotImplementedError
43
+ end
44
+
45
+ protected
46
+ def opts
47
+ @opts
48
+ end
49
+
50
+ def error(e)
51
+ @errors ||= []
52
+ @errors << e
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,9 @@
1
+ module HaveAPI::Client
2
+ class Validators::Acceptance < Validator
3
+ name :accept
4
+
5
+ def valid?
6
+ value == opts[:value]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module HaveAPI::Client
2
+ class Validators::Confirmation < Validator
3
+ name :confirm
4
+
5
+ def valid?
6
+ other = opts[:parameter].to_sym
7
+
8
+ if opts[:equal]
9
+ return false if params[other].nil?
10
+ value == params[other].value
11
+
12
+ else
13
+ other = params[other] ? params[other].value : nil
14
+ value != other
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module HaveAPI::Client
2
+ class Validators::Custom < Validator
3
+ name :custom
4
+
5
+ def valid?
6
+ true
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module HaveAPI::Client
2
+ class Validators::Exclusion < Validator
3
+ name :exclude
4
+
5
+ def valid?
6
+ !opts[:values].include?(value)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module HaveAPI::Client
2
+ class Validators::Format < Validator
3
+ name :format
4
+
5
+ def valid?
6
+ rx = Regexp.new(opts[:rx])
7
+
8
+ if opts[:match]
9
+ rx.match(value) ? true : false
10
+
11
+ else
12
+ rx.match(value) ? false : true
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module HaveAPI::Client
2
+ class Validators::Inclusion < Validator
3
+ name :include
4
+
5
+ def valid?
6
+ if opts[:values].is_a?(::Hash)
7
+ opts[:values].keys.include?(value)
8
+
9
+ else
10
+ opts[:values].include?(value)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module HaveAPI::Client
2
+ class Validators::Length < Validator
3
+ name :length
4
+
5
+ def valid?
6
+ len = value.length
7
+
8
+ return len == opts[:equals] if opts[:equals]
9
+ return len >= opts[:min] if opts[:min] && !opts[:max]
10
+ return len <= opts[:max] if !opts[:min] && opts[:max]
11
+ len >= opts[:min] && len <= opts[:max]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ module HaveAPI::Client
2
+ class Validators::Numericality < Validator
3
+ name :number
4
+
5
+ def valid?
6
+ if value.is_a?(::String)
7
+ return false if /\A\d+\z/ !~ value
8
+ v = value.to_i
9
+
10
+ else
11
+ v = value
12
+ end
13
+
14
+ ret = true
15
+ ret = false if opts[:min] && v < opts[:min]
16
+ ret = false if opts[:max] && v > opts[:max]
17
+ ret = false if opts[:step] && (v - (opts[:min] || 0)) % opts[:step] != 0
18
+ ret = false if opts[:mod] && v % opts[:mod] != 0
19
+ ret = false if opts[:odd] && v % 2 == 0
20
+ ret = false if opts[:even] && v % 2 > 0
21
+ ret
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module HaveAPI::Client
2
+ class Validators::Presence < Validator
3
+ name :present
4
+
5
+ def valid?
6
+ return false if value.nil?
7
+ return !value.strip.empty? if !opts[:empty] && value.is_a?(::String)
8
+ true
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,6 @@
1
1
  module HaveAPI
2
2
  module Client
3
- VERSION = '0.3.0'
3
+ PROTOCOL_VERSION = '1.0'
4
+ VERSION = '0.4.0'
4
5
  end
5
- end
6
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haveapi-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Skokan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-24 00:00:00.000000000 Z
11
+ date: 2016-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,28 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - ~>
46
46
  - !ruby/object:Gem::Version
47
- version: 4.1.0
47
+ version: 4.1.14
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: 4.1.0
54
+ version: 4.1.14
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: require_all
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 1.3.0
61
+ version: 1.3.3
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: 1.3.0
68
+ version: 1.3.3
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rest_client
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -86,42 +86,28 @@ dependencies:
86
86
  requirements:
87
87
  - - ~>
88
88
  - !ruby/object:Gem::Version
89
- version: 1.8.1
89
+ version: 1.8.3
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ~>
95
95
  - !ruby/object:Gem::Version
96
- version: 1.8.1
96
+ version: 1.8.3
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: highline
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ~>
102
102
  - !ruby/object:Gem::Version
103
- version: 1.6.21
103
+ version: 1.7.8
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ~>
109
109
  - !ruby/object:Gem::Version
110
- version: 1.6.21
111
- - !ruby/object:Gem::Dependency
112
- name: table_print
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ~>
116
- - !ruby/object:Gem::Version
117
- version: 1.5.3
118
- type: :runtime
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ~>
123
- - !ruby/object:Gem::Version
124
- version: 1.5.3
110
+ version: 1.7.8
125
111
  description: Ruby API and CLI for HaveAPI
126
112
  email:
127
113
  - jakub.skokan@vpsfree.cz
@@ -130,6 +116,7 @@ executables:
130
116
  extensions: []
131
117
  extra_rdoc_files: []
132
118
  files:
119
+ - CHANGELOG
133
120
  - Gemfile
134
121
  - LICENSE.txt
135
122
  - README.md
@@ -141,7 +128,9 @@ files:
141
128
  - lib/haveapi/cli/authentication/basic.rb
142
129
  - lib/haveapi/cli/authentication/token.rb
143
130
  - lib/haveapi/cli/cli.rb
131
+ - lib/haveapi/cli/command.rb
144
132
  - lib/haveapi/cli/example_formatter.rb
133
+ - lib/haveapi/cli/output_formatter.rb
145
134
  - lib/haveapi/client.rb
146
135
  - lib/haveapi/client/action.rb
147
136
  - lib/haveapi/client/authentication/base.rb
@@ -152,10 +141,23 @@ files:
152
141
  - lib/haveapi/client/communicator.rb
153
142
  - lib/haveapi/client/exceptions.rb
154
143
  - lib/haveapi/client/inflections.rb
144
+ - lib/haveapi/client/parameters/resource.rb
145
+ - lib/haveapi/client/parameters/typed.rb
146
+ - lib/haveapi/client/params.rb
155
147
  - lib/haveapi/client/resource.rb
156
148
  - lib/haveapi/client/resource_instance.rb
157
149
  - lib/haveapi/client/resource_instance_list.rb
158
150
  - lib/haveapi/client/response.rb
151
+ - lib/haveapi/client/validator.rb
152
+ - lib/haveapi/client/validators/acceptance.rb
153
+ - lib/haveapi/client/validators/confirmation.rb
154
+ - lib/haveapi/client/validators/custom.rb
155
+ - lib/haveapi/client/validators/exclusion.rb
156
+ - lib/haveapi/client/validators/format.rb
157
+ - lib/haveapi/client/validators/inclusion.rb
158
+ - lib/haveapi/client/validators/length.rb
159
+ - lib/haveapi/client/validators/numericality.rb
160
+ - lib/haveapi/client/validators/presence.rb
159
161
  - lib/haveapi/client/version.rb
160
162
  - lib/restclient_ext/request.rb
161
163
  - lib/restclient_ext/resource.rb
@@ -179,8 +181,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
181
  version: '0'
180
182
  requirements: []
181
183
  rubyforge_project:
182
- rubygems_version: 2.2.2
184
+ rubygems_version: 2.2.5
183
185
  signing_key:
184
186
  specification_version: 4
185
187
  summary: Ruby API and CLI for HaveAPI
186
188
  test_files: []
189
+ has_rdoc: