lib/acw/client.rb in acw-1.3.0 vs lib/acw/client.rb in acw-1.3.1
- old
+ new
@@ -1,15 +1,17 @@
# frozen_string_literal: true
require 'excon'
require 'cgi'
require 'json'
+require './lib/acw/helpers'
module Acw
class Client
+ include Acw::Helpers
+
API_VERSION = 3
- Result = Struct.new(:success?, :error, :value)
def initialize(configs = {})
@config = configs
end
@@ -17,121 +19,120 @@
def connection
@connection ||= Excon.new(config[:url])
end
- # CONTACTS
def create_contact(args = {})
safe_http_call do
params = { contact: args }
connection.post(
path: "/api/#{API_VERSION}/contacts",
- headers: headers,
+ headers: headers(config[:token]),
body: params.to_json
)
end
end
- def sync_contact(args = {})
+ def sync_contact(params)
safe_http_call do
- params = { contact: args }
connection.post(
path: "/api/#{API_VERSION}/contact/sync",
- headers: headers,
+ headers: headers(config[:token]),
body: params.to_json
)
end
end
def retrieve_contact(id)
safe_http_call do
- connection.get(path: "/api/#{API_VERSION}/contacts/#{id}", headers: headers)
+ connection.get(
+ path: "/api/#{API_VERSION}/contacts/#{id}",
+ headers: headers(config[:token])
+ )
end
end
def retrieve_contact_by_email(email)
safe_http_call do
uemail = CGI.escape email
- connection.get(path: "/api/#{API_VERSION}/contacts?search=#{uemail}", headers: headers)
+ connection.get(
+ path: "/api/#{API_VERSION}/contacts?search=#{uemail}",
+ headers: headers(config[:token])
+ )
end
end
- # LISTS
def retrieve_lists
safe_http_call do
- connection.get(path: "/api/#{API_VERSION}/lists", headers: headers)
+ connection.get(
+ path: "/api/#{API_VERSION}/lists",
+ headers: headers(config[:token])
+ )
end
end
- # TAGS
def create_tag(args = {})
safe_http_call do
params = { tag: args }
connection.post(
path: "/api/#{API_VERSION}/tags",
- headers: headers,
+ headers: headers(config[:token]),
body: params.to_json
)
end
end
def add_contact_tag(args = {})
safe_http_call do
params = { 'contactTag': args }
connection.post(
path: "/api/#{API_VERSION}/contactTags",
- headers: headers,
+ headers: headers(config[:token]),
body: params.to_json
)
end
end
def remove_contact_tag(id)
safe_http_call do
- connection.delete(path: "/api/#{API_VERSION}/contactTags/#{id}", headers: headers)
+ connection.delete(
+ path: "/api/#{API_VERSION}/contactTags/#{id}",
+ headers: headers(config[:token])
+ )
end
end
- # FIELD_VALUES
def create_field_value(args = {})
safe_http_call do
params = { 'fieldValue': args }
connection.post(
path: "/api/#{API_VERSION}/fieldValues",
- headers: headers,
+ headers: headers(config[:token]),
body: params.to_json
)
end
end
def update_field_value(id, args = {})
safe_http_call do
params = { 'fieldValue': args }
connection.put(
path: "/api/#{API_VERSION}/fieldValues/#{id}",
- headers: headers,
+ headers: headers(config[:token]),
body: params.to_json
)
end
end
private
- def headers
- {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- 'Api-Token': config[:token]
- }
- end
-
def safe_http_call
response = yield
raise response.body unless success_http_status(response.status)
- Result.new(true, nil, JSON.parse(response.body))
+ result(true, nil, JSON.parse(response.body))
rescue StandardError => e
- Result.new(false, e.message, nil)
+ result(false, e.message, nil)
end
def success_http_status(status)
[200, 201, 202].include?(status)
end