-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdecision.rb
More file actions
67 lines (53 loc) · 1.62 KB
/
decision.rb
File metadata and controls
67 lines (53 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require "cgi"
require_relative "../router"
require_relative "../validate/decision"
require_relative "../utils/hash_getter"
require_relative "./decision/apply_to"
module Sift
class Client
class Decision
FILTER_PARAMS = %w{ limit entity_type abuse_types from }
attr_reader :account_id, :api_key, :client_class
def initialize(api_key, account_id, client_class = Sift::Client)
@account_id = account_id
@api_key = api_key
@client_class = client_class
end
def list(options = {})
getter = Utils::HashGetter.new(options)
if path = getter.get(:next_ref)
request_next_page(path)
else
Router.get(index_path, {
query: build_query(getter),
headers: auth_header,
client_class: client_class
})
end
end
def build_query(getter)
FILTER_PARAMS.inject({}) do |result, filter|
if value = getter.get(filter)
result[filter] = value.is_a?(Array) ? value.join(",") : value
end
result
end
end
def apply_to(configs = {})
getter = Utils::HashGetter.new(configs)
configs[:account_id] = account_id
ApplyTo.new(api_key, getter.get(:decision_id), configs, client_class).run
end
def index_path
"#{Client::API3_ENDPOINT}/v3/accounts/#{account_id}/decisions"
end
private
def request_next_page(path)
Router.get(path, headers: auth_header, client_class: client_class)
end
def auth_header
Client.build_auth_header(api_key)
end
end
end
end