alloy-api 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +8 -1
- data/lib/alloy-api.rb +22 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 642721aa9c89391716cea4f0d4ae4a86938b4106de8a02811223ebc3aa373050
|
4
|
+
data.tar.gz: b983d1b9c6a1810e2fa27d4f27a8da3b30e3587615985cc0156eb17be3dca4cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd88a8990194bd30bd75af695c189422bc0728b7114b368fbae6d0f0a3e3be2a80810b9e8a76e958007f94cfa362f7395c472ef21a2300bd8bd8a1f252c7e06a
|
7
|
+
data.tar.gz: 0f210ddce06c592cc5df6a7e93d3eeed891f7ee69b63d835f9ad590f99fa2912817d160b1239ad32f7ae56414bef68ddd9036a409c9f99f3c491612290712e70
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -35,17 +35,24 @@ Alloy::Api.api_secret = 'provided by the workflow'
|
|
35
35
|
|
36
36
|
If using Rails, this can be done in either the environment files or an initializer, but remember not to include the raw keys in repositories!
|
37
37
|
|
38
|
-
Also, if using a sandbox, set the
|
38
|
+
Also, if using a sandbox, set the uri:
|
39
39
|
|
40
40
|
```ruby
|
41
41
|
Alloy::Api.api_uri = 'https://sandbox.alloy.co/' # Be sure to include the trailing slash!
|
42
42
|
```
|
43
43
|
|
44
|
+
The API supports multiple endpoints as well. The default endpoint is called `:main`, but additional endpoints can be added:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
Alloy::Api.set_endpoint(:other_endpoint, '<api_token>', '<api_secret', '<api_uri>(optional - use for sandbox)')
|
48
|
+
```
|
49
|
+
|
44
50
|
The Alloy.co api methods are implemented by the Api class. Most take a set of options, including headers and body:
|
45
51
|
|
46
52
|
```ruby
|
47
53
|
Alloy::Api.parameters(method: 'get') # The default method is 'post' - this method will get required/optional parameters for running evaluations
|
48
54
|
Alloy::Api.evaluations(body: { first_name: 'John', last_name: 'Smith' }, headers: { 'Alloy-Refresh-Cache': 'true' }) # Runs an evaluation. Headers can be set as well, but the Content-Type and Authorization are automatic
|
55
|
+
Alloy::Api.parameters(method: 'get', endpoint: :other_endpoint) # use a different endpoint - allowing for multiple workflows to be used
|
49
56
|
```
|
50
57
|
|
51
58
|
## License
|
data/lib/alloy-api.rb
CHANGED
@@ -4,20 +4,26 @@ require 'json'
|
|
4
4
|
|
5
5
|
module Alloy
|
6
6
|
class Api
|
7
|
-
@@
|
8
|
-
@@api_token = nil
|
9
|
-
@@api_secret = nil
|
7
|
+
@@api_endpoints = { main: { uri: 'https://api.alloy.co/' } }
|
10
8
|
|
11
9
|
def self.api_uri=(value)
|
12
|
-
@@
|
10
|
+
@@api_endpoints[:main][:uri] = value
|
13
11
|
end
|
14
12
|
|
15
13
|
def self.api_token=(value)
|
16
|
-
@@
|
14
|
+
@@api_endpoints[:main][:token] = value
|
17
15
|
end
|
18
16
|
|
19
17
|
def self.api_secret=(value)
|
20
|
-
@@
|
18
|
+
@@api_endpoints[:main][:secret] = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.set_endpoint(name, token, secret, uri = 'https://api.alloy.co/')
|
22
|
+
@@api_endpoints[name] = {
|
23
|
+
uri: uri,
|
24
|
+
token: token,
|
25
|
+
secret: secret
|
26
|
+
}
|
21
27
|
end
|
22
28
|
|
23
29
|
def self.parameters(options={})
|
@@ -38,12 +44,17 @@ module Alloy
|
|
38
44
|
|
39
45
|
def self.perform(path, options={})
|
40
46
|
method = options[:method] || "post"
|
41
|
-
|
47
|
+
endpoint = options[:endpoint] || :main
|
48
|
+
uri = "#{@@api_endpoints[endpoint][:uri]}#{path}"
|
42
49
|
headers = {
|
43
50
|
"Content-Type" => "application/json",
|
44
|
-
"Authorization" => auth_param
|
51
|
+
"Authorization" => auth_param(endpoint)
|
45
52
|
}.merge(options[:headers].to_h)
|
46
|
-
|
53
|
+
if options[:body_stream].present?
|
54
|
+
response = HTTParty.send(method, uri, { headers: headers, body_stream: options[:body_stream] })
|
55
|
+
else
|
56
|
+
response = HTTParty.send(method, uri, {headers: headers, body: (options[:body] || {}).to_json})
|
57
|
+
end
|
47
58
|
return response if options[:raw]
|
48
59
|
JSON.parse(response.body)
|
49
60
|
# TODO: Error handling
|
@@ -51,12 +62,8 @@ module Alloy
|
|
51
62
|
|
52
63
|
private
|
53
64
|
|
54
|
-
def self.auth_param
|
55
|
-
"Basic #{
|
56
|
-
end
|
57
|
-
|
58
|
-
def self.encoded
|
59
|
-
Base64.strict_encode64 "#{@@api_token}:#{@@api_secret}"
|
65
|
+
def self.auth_param(endpoint)
|
66
|
+
"Basic #{Base64.strict_encode64("#{@@api_endpoints[endpoint][:token]}:#{@@api_endpoints[endpoint][:secret]}")}"
|
60
67
|
end
|
61
68
|
end
|
62
69
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alloy-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Schultz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06-
|
11
|
+
date: 2020-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|