virtuous 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.reek.yml +2 -2
- data/CHANGELOG.md +4 -0
- data/lib/virtuous/client/project.rb +58 -0
- data/lib/virtuous/client.rb +2 -1
- data/lib/virtuous/error.rb +35 -34
- data/lib/virtuous/parse_oj.rb +18 -16
- data/lib/virtuous/version.rb +1 -1
- data/spec/support/fixtures/project_query_options.json +2701 -0
- data/spec/support/fixtures/projects.json +93 -0
- data/spec/support/virtuous_mock.rb +3 -1
- data/spec/virtuous/resources/project_spec.rb +70 -0
- metadata +10 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dde636ea3b588b861eb1571600585b5a92f221c427b1de5d0dd5da6019f19906
|
4
|
+
data.tar.gz: c82140d46fe427a713015074ae28c81fa2ef7494cc9a527cd1dac9f129525b69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 408bdd4c97d63495fca22af299e1878ef70ca601428bde15ecc684c59f8d01cb0d3340389de1b4adb77958d6fc9972f2864338d31b729ef1e8db2a3951b3c180
|
7
|
+
data.tar.gz: a917e9353ad30d8ac5f4626dedf67890fa5cbc3123d9559aa0a61475034e7e98faf2e0e45644428326326529392c215c1fb0a91beb3955cdac3ef0ff46c33179
|
data/.reek.yml
CHANGED
@@ -8,7 +8,7 @@ detectors:
|
|
8
8
|
- "Virtuous::Client#connection"
|
9
9
|
- "Virtuous::Client#unauthorized_connection"
|
10
10
|
- "Virtuous::HashHelper#self.deep_transform_keys"
|
11
|
-
- "FaradayMiddleware::ParseOj#on_complete"
|
11
|
+
- "Virtuous::FaradayMiddleware::ParseOj#on_complete"
|
12
12
|
NilCheck:
|
13
13
|
exclude:
|
14
14
|
- "Virtuous::Client#initialize"
|
@@ -22,7 +22,7 @@ detectors:
|
|
22
22
|
- "Virtuous::HashHelper#self.deep_transform_keys"
|
23
23
|
- "Virtuous::Client#connection"
|
24
24
|
- "Virtuous::Client#unauthorized_connection"
|
25
|
-
- "FaradayMiddleware::VirtuousErrorHandler#on_complete"
|
25
|
+
- "Virtuous::FaradayMiddleware::VirtuousErrorHandler#on_complete"
|
26
26
|
ControlParameter:
|
27
27
|
exclude:
|
28
28
|
- "Virtuous::Client#initialize"
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,58 @@
|
|
1
|
+
module Virtuous
|
2
|
+
class Client
|
3
|
+
module Project
|
4
|
+
##
|
5
|
+
# Queries a list of available project query options.
|
6
|
+
# @return [Hash] A list of available project query options.
|
7
|
+
def project_query_options
|
8
|
+
parse(get('api/Project/QueryOptions'))
|
9
|
+
end
|
10
|
+
|
11
|
+
##
|
12
|
+
# Queries projects.
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# client.query_projects(
|
16
|
+
# take: 5, skip: 0, sort_by: 'Id',
|
17
|
+
# conditions: [{ parameter: 'Project Code', operator: 'Is', value: 102 }]
|
18
|
+
# )
|
19
|
+
#
|
20
|
+
# @option options [Integer] :take The maximum amount of projects to query. Default: 10.
|
21
|
+
# Max is 1000.
|
22
|
+
# @option options [Integer] :skip The number of projects to skip. Default: 0.
|
23
|
+
# @option options [String] :sort_by The value to sort records by.
|
24
|
+
# @option options [Boolean] :descending If true the records will be sorted in descending
|
25
|
+
# order.
|
26
|
+
# @option options [Array] :conditions An array of conditions to filter the project.
|
27
|
+
# Use {project_query_options} to see
|
28
|
+
# a full list of available conditions.
|
29
|
+
#
|
30
|
+
# @return [Hash] A hash with a list and the total amount of projects that satisfy
|
31
|
+
# the conditions.
|
32
|
+
# @example Output
|
33
|
+
# { list: [...], total: n }
|
34
|
+
#
|
35
|
+
def query_projects(**options)
|
36
|
+
uri = URI('api/Project/Query')
|
37
|
+
query_params = options.slice(:take, :skip)
|
38
|
+
uri.query = URI.encode_www_form(query_params) unless query_params.empty?
|
39
|
+
|
40
|
+
parse(post(uri, format(query_projects_body(options))))
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def query_projects_body(options)
|
46
|
+
conditions = options[:conditions]
|
47
|
+
body = options.slice(:sort_by, :descending)
|
48
|
+
return body if conditions.nil?
|
49
|
+
|
50
|
+
body.merge({
|
51
|
+
groups: [{
|
52
|
+
conditions: conditions
|
53
|
+
}]
|
54
|
+
})
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/virtuous/client.rb
CHANGED
@@ -75,6 +75,7 @@ module Virtuous
|
|
75
75
|
include Gift
|
76
76
|
include RecurringGift
|
77
77
|
include GiftDesignation
|
78
|
+
include Project
|
78
79
|
|
79
80
|
##
|
80
81
|
# Access token used for OAuth authentication.
|
@@ -263,7 +264,7 @@ module Virtuous
|
|
263
264
|
conn.request :json
|
264
265
|
conn.response :oj
|
265
266
|
conn.response :logger, @logger if @logger
|
266
|
-
conn.use FaradayMiddleware::VirtuousErrorHandler
|
267
|
+
conn.use Virtuous::FaradayMiddleware::VirtuousErrorHandler
|
267
268
|
conn.adapter @adapter
|
268
269
|
yield(conn) if block_given?
|
269
270
|
end
|
data/lib/virtuous/error.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
1
3
|
module Virtuous # :nodoc: all
|
2
4
|
class Error < StandardError; end
|
3
5
|
class BadGateway < Error; end
|
@@ -9,46 +11,45 @@ module Virtuous # :nodoc: all
|
|
9
11
|
class NotFound < Error; end
|
10
12
|
class ServiceUnavailable < Error; end
|
11
13
|
class Unauthorized < Error; end
|
12
|
-
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
ERROR_STATUSES = (400..600).freeze
|
15
|
+
module FaradayMiddleware
|
16
|
+
class VirtuousErrorHandler < Faraday::Middleware
|
17
|
+
ERROR_STATUSES = (400..600).freeze
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
##
|
20
|
+
# Throws an exception for responses with an HTTP error code.
|
21
|
+
def on_complete(env)
|
22
|
+
message = error_message(env)
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
24
|
+
case env[:status]
|
25
|
+
when 400
|
26
|
+
raise Virtuous::BadRequest, message
|
27
|
+
when 401
|
28
|
+
raise Virtuous::Unauthorized, message
|
29
|
+
when 403
|
30
|
+
raise Virtuous::Forbidden, message
|
31
|
+
when 404
|
32
|
+
raise Virtuous::NotFound, message
|
33
|
+
when 500
|
34
|
+
raise Virtuous::InternalServerError, message
|
35
|
+
when 502
|
36
|
+
raise Virtuous::BadGateway, message
|
37
|
+
when 503
|
38
|
+
raise Virtuous::ServiceUnavailable, message
|
39
|
+
when 504
|
40
|
+
raise Virtuous::GatewayTimeout, message
|
41
|
+
when 520
|
42
|
+
raise Virtuous::CloudflareError, message
|
43
|
+
when ERROR_STATUSES
|
44
|
+
raise Virtuous::Error, message
|
45
|
+
end
|
45
46
|
end
|
46
|
-
end
|
47
47
|
|
48
|
-
|
48
|
+
private
|
49
49
|
|
50
|
-
|
51
|
-
|
50
|
+
def error_message(env)
|
51
|
+
"#{env[:status]}: #{env[:url]} #{env[:body]}"
|
52
|
+
end
|
52
53
|
end
|
53
54
|
end
|
54
55
|
end
|
data/lib/virtuous/parse_oj.rb
CHANGED
@@ -1,24 +1,26 @@
|
|
1
1
|
require 'oj'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
3
|
+
module Virtuous
|
4
|
+
module FaradayMiddleware
|
5
|
+
class ParseOj < Faraday::Middleware
|
6
|
+
##
|
7
|
+
# Parses JSON responses.
|
8
|
+
def on_complete(env)
|
9
|
+
body = env[:body]
|
10
|
+
env[:body] = if empty_body?(body.strip)
|
11
|
+
nil
|
12
|
+
else
|
13
|
+
Oj.load(body, mode: :compat)
|
14
|
+
end
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
+
private
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
def empty_body?(body)
|
20
|
+
body.empty? && body == ''
|
21
|
+
end
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
Faraday::Response.register_middleware(oj: FaradayMiddleware::ParseOj)
|
26
|
+
Faraday::Response.register_middleware(oj: Virtuous::FaradayMiddleware::ParseOj)
|
data/lib/virtuous/version.rb
CHANGED