basic_api_client 1.1.0 → 2.0.0
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/lib/basic_api_client.rb +46 -44
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20e7c36a1f799484ef9491436647986933287950
|
4
|
+
data.tar.gz: a979582b06128b2976b261eb3009dfc15ca52c23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4650dd17e0c88fbc905d4d1f5b615fc28d5908472c7922f8bc267418ecea83bb42b1a59ad614cdc88a59e771acff1d9f5089f0827296decbf29ec2ff10726eea
|
7
|
+
data.tar.gz: 3f56203af9fca4bc82f10c4fa817ee049fc8aaf8cb5bc6e5f85c99af848746369edb14862292ca62ed1af631dc23348894b32800c67c894061fd790ae02fe623
|
data/lib/basic_api_client.rb
CHANGED
@@ -1,77 +1,79 @@
|
|
1
|
-
require '
|
1
|
+
require 'curb'
|
2
2
|
require 'json'
|
3
3
|
|
4
4
|
class BasicApiClient
|
5
5
|
AUTH_HEADERS = %w{ access-token token-type uid expiry client }.sort
|
6
6
|
|
7
7
|
attr_reader :headers
|
8
|
-
attr_accessor :last_response
|
8
|
+
attr_accessor :last_response, :last_return
|
9
9
|
|
10
|
-
def initialize(cn, password, site,
|
10
|
+
def initialize(cn, password, site, auth_path)
|
11
11
|
@cn = cn
|
12
12
|
@password = password
|
13
13
|
@site = site
|
14
|
-
@
|
14
|
+
@site.sub!(/\/+$/, '')
|
15
|
+
@auth_url = url_for(auth_path)
|
15
16
|
end
|
16
17
|
|
17
18
|
def headers
|
18
19
|
@headers ||= {}
|
19
20
|
end
|
20
21
|
|
21
|
-
def
|
22
|
-
@
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
faraday.adapter Faraday.default_adapter
|
27
|
-
end
|
28
|
-
end
|
22
|
+
def authorize
|
23
|
+
@headers = {}
|
24
|
+
params = { cn: @cn, password: @password }
|
25
|
+
Curl.post(@auth_url, params, &method(:setup_request))
|
26
|
+
last_return
|
29
27
|
end
|
30
28
|
|
31
|
-
def
|
32
|
-
|
33
|
-
req.url @auth_url
|
34
|
-
req.params['cn'] = @cn
|
35
|
-
req.params['password'] = @password
|
36
|
-
end
|
29
|
+
def get(path, params = {})
|
30
|
+
authorize unless auth_headers?
|
37
31
|
|
38
|
-
|
32
|
+
url = url_for(path)
|
33
|
+
Curl.get(url, params, &method(:setup_request))
|
34
|
+
last_return
|
39
35
|
end
|
40
36
|
|
41
|
-
def
|
37
|
+
def post(path, params = {})
|
42
38
|
authorize unless auth_headers?
|
43
39
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
40
|
+
url = url_for(path)
|
41
|
+
Curl.post(url, params, &method(:setup_request))
|
42
|
+
last_return
|
43
|
+
end
|
49
44
|
|
50
|
-
|
45
|
+
private
|
46
|
+
def url_for(path)
|
47
|
+
path = path.sub(/^\/+/, '')
|
48
|
+
[@site, path].join('/')
|
51
49
|
end
|
52
50
|
|
53
|
-
def
|
54
|
-
|
51
|
+
def setup_request(req)
|
52
|
+
req.headers = headers
|
53
|
+
req.on_failure &method(:handle_failure)
|
54
|
+
req.on_missing &method(:handle_failure)
|
55
|
+
req.on_success &method(:handle_success)
|
56
|
+
end
|
55
57
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
def handle_success(res)
|
59
|
+
@last_response = res
|
60
|
+
@headers = parse_headers(res).select { |name, _|
|
61
|
+
AUTH_HEADERS.include?(name) }
|
62
|
+
@last_return = JSON.parse(res.body_str)
|
63
|
+
end
|
61
64
|
|
62
|
-
|
65
|
+
def handle_failure(res, code)
|
66
|
+
@last_response = res
|
67
|
+
@headers = {}
|
68
|
+
@last_return = nil
|
63
69
|
end
|
64
70
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
else
|
72
|
-
@headers = {}
|
73
|
-
false
|
74
|
-
end
|
71
|
+
def parse_headers(res)
|
72
|
+
lines = res.header_str.split("\r\n").drop(1)
|
73
|
+
lines.each_with_object({}) { |line, headers|
|
74
|
+
name, value = line.split(": ")
|
75
|
+
name.downcase!
|
76
|
+
headers[name] = value }
|
75
77
|
end
|
76
78
|
|
77
79
|
def auth_headers?
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: basic_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jphager2
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: curb
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
version: '0'
|
68
68
|
requirements: []
|
69
69
|
rubyforge_project:
|
70
|
-
rubygems_version: 2.
|
70
|
+
rubygems_version: 2.5.1
|
71
71
|
signing_key:
|
72
72
|
specification_version: 4
|
73
73
|
summary: Example of a basic api client that keeps access-token headers
|