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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +8 -1
  4. data/lib/alloy-api.rb +22 -15
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a08b628f971e1a9f8f86238fc13f71c88bf592e402bdafa7b5044fef8c822bf
4
- data.tar.gz: 0ace081b0bec3227c5dc3f925f126eee963fbdd0a15c4e7ee966559aeeb9b206
3
+ metadata.gz: 642721aa9c89391716cea4f0d4ae4a86938b4106de8a02811223ebc3aa373050
4
+ data.tar.gz: b983d1b9c6a1810e2fa27d4f27a8da3b30e3587615985cc0156eb17be3dca4cf
5
5
  SHA512:
6
- metadata.gz: fecd7e60e97d858b2bcceee8afe3203d397fd55e58e6daeee2913b2f84e081605632a713311b33ef6e6ea9a311326527de9d563c36fbc2c949ea1f10cc0c5979
7
- data.tar.gz: 5d569b724c31aeeb7868868613ab3b7854176ba0d814085d3cf3b105112882533de95e52bad98a95c993b1481485430a0a60040f88e83ff36d255bf7483a6981
6
+ metadata.gz: cd88a8990194bd30bd75af695c189422bc0728b7114b368fbae6d0f0a3e3be2a80810b9e8a76e958007f94cfa362f7395c472ef21a2300bd8bd8a1f252c7e06a
7
+ data.tar.gz: 0f210ddce06c592cc5df6a7e93d3eeed891f7ee69b63d835f9ad590f99fa2912817d160b1239ad32f7ae56414bef68ddd9036a409c9f99f3c491612290712e70
@@ -1,3 +1,8 @@
1
+ ## 0.0.6 / 2020-06-18
2
+
3
+ * Allow body stream
4
+ * Allow multiple endpoints
5
+
1
6
  ## 0.0.5 / 2020-06-11
2
7
 
3
8
  * Method corrections
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 endpoint:
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
@@ -4,20 +4,26 @@ require 'json'
4
4
 
5
5
  module Alloy
6
6
  class Api
7
- @@api_uri = 'https://api.alloy.co/'
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
- @@api_uri = value
10
+ @@api_endpoints[:main][:uri] = value
13
11
  end
14
12
 
15
13
  def self.api_token=(value)
16
- @@api_token = value
14
+ @@api_endpoints[:main][:token] = value
17
15
  end
18
16
 
19
17
  def self.api_secret=(value)
20
- @@api_secret = value
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
- uri = "#{@@api_uri}#{path}"
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
- response = HTTParty.send(method, uri, {headers: headers, body: (options[:body] || {}).to_json})
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 #{encoded}"
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.5
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 00:00:00.000000000 Z
11
+ date: 2020-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty