Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ontraport.sublime-project
ontraport.sublime-workspace
*.gem
/doc/*
/doc/*
.idea
.ruby-version
8 changes: 4 additions & 4 deletions .yardoc/checksums
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lib/ontraport.rb b594b805dde3c592fd7cbad36b2c4669b73b26e7
lib/ontraport/version.rb 91989823b1c7e27b077df5cbfa78e912fd7e9400
lib/ontraport/response.rb a6560cd7c27414b42d2070db67f466f8e1be050e
lib/ontraport.rb 306376bf02a3e2270837fc52572b60bf15ea4405
lib/ontraport/version.rb 13bacfd933f7b6f9cd998cabbcaba4ce15fd9ea2
lib/ontraport/response.rb 29aed67b1e4f4f54a179c11680e19df0cbf4de77
lib/ontraport/exceptions.rb 2847613063c8f69f5c22a2a19b8e445b66075729
lib/ontraport/configuration.rb c0cecf8a66acf72e84d7c97e89f602a92a6f8f21
lib/ontraport/configuration.rb 3c1a8c931b393a146d44360862182f69173c282b
Binary file modified .yardoc/object_types
Binary file not shown.
Binary file modified .yardoc/objects/root.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

- Added new method `add_subscription` for objects
- Added new configuration option `debug_mode`
adds value `original_request` to response data

# 0.1.21 - September 6, 2017

- require HTTParty in main module (fixes #1)
Expand Down
99 changes: 78 additions & 21 deletions lib/ontraport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,54 @@ def self.untag_objects object_type, object:, tag:
objects_call :delete, object_type, endpoint: '/objects/tag', data: params
end

# Add a subscription to an object.
#
# @example
# Ontraport.add_subscription :contact, 12345, [150,200], "Campaign", { range: 5 }
# #=> #<Ontraport::Response @data=...>
#
# @see https://api.ontraport.com/live/#!/objects/addSubscription API docs
#
# @param object_type [Symbol] the type of object
# @param ids [Array, Integer] id or array of ids of objects to subscribe
# @param add_list [Array, Integer] id or array of ids of Campaigns or Sequences to subscribe the object to
# @param sub_type [String, nil] possible values are "Sequence" or "Campaign" defaults to "Campaign"
# @param params [Hash, nil] extra stuff to add to request data. Use +.describe+ to get a list of available fields.
# @return [Response]

def self.add_subscription object_type, ids, add_list, sub_type = 'Campaign', params = {}
objects_call :put, object_type, endpoint: '/objects/subscribe',
data: params.update(
ids: Array(ids).join(','),
sub_type: sub_type,
add_list: Array(add_list).join(',')
)
end

# Remove a subscription from an object.
#
# @example
# Ontraport.remove_subscription :contact, 12345, [150,200], "Campaign", { range: 5 }
# #=> #<Ontraport::Response @data=...>
#
# @see https://api.ontraport.com/live/#!/objects/addSubscription API docs
#
# @param object_type [Symbol] the type of object
# @param ids [Array, Integer] id or array of ids of objects to subscribe
# @param remove_list [Array, Integer] id or array of ids of Campaigns or Sequences to unsubscribe the object to
# @param sub_type [String, nil] possible values are "Sequence" or "Campaign" defaults to "Campaign"
# @param params [Hash, nil] extra stuff to add to request data. Use +.describe+ to get a list of available fields.
# @return [Response]

def self.remove_subscription object_type, ids, remove_list, sub_type = 'Campaign', params = {}
objects_call :delete, object_type, endpoint: '/objects/subscribe',
data: params.update(
ids: Array(ids).join(','),
sub_type: sub_type,
remove_list: Array(remove_list).join(',')
)
end

# @!endgroup
# @!group "Transactions" Methods

Expand All @@ -167,35 +215,44 @@ def self.get_order order_id
end

# @!endgroup
#

private
def self.request_with_authentication method, endpoint:, data: nil
data_param = method.eql?(:get) ? :query : :body

args = [method, "#{BASE_URL}#{API_VERSION}#{endpoint}"]
kwargs = {
:headers => { 'Api-Appid' => @configuration.api_id, 'Api-Key' => @configuration.api_key },
data_param => data
}
def self.request_with_authentication method, endpoint:, data: nil
data_param = method.eql?(:get) ? :query : :body
request_content = data_param.eql?(:body) ? data.to_json : data

response = HTTParty.send *args, **kwargs
args = [method, "#{BASE_URL}#{API_VERSION}#{endpoint}"]
kwargs = {
:headers => { 'Api-Appid' => @configuration.api_id,
'Api-Key' => @configuration.api_key,
'Content-Type' => 'application/json' },
data_param => request_content
}

unless response.code.eql? 200
error = "#{response.code} #{response.msg}"
raise APIError.new(response.body.present? ? "#{error} - #{response.body}" : error)
end
response = HTTParty.send *args, **kwargs

Response.new **response.parsed_response.symbolize_keys
unless response.code.eql? 200
error = "#{response.code} #{response.msg}"
raise APIError.new(response.body.present? ? "#{error} - #{response.body}" : error)
end

def self.objects_call method, object_type, endpoint:, data: {}
metadata = describe object_type
data.update 'objectID' => metadata['schema_object_id']
parsed_response = response.parsed_response

request_with_authentication method, endpoint: endpoint, data: data
end
@configuration.debug_mode ? parsed_response.update(original_request: response.request) : nil

def self.objects_meta
@objects_meta_cache ||= request_with_authentication :get, endpoint: '/objects/meta'
end
Response.new **parsed_response.symbolize_keys
end

def self.objects_call method, object_type, endpoint:, data: {}
metadata = describe object_type
data.update 'objectID' => metadata['schema_object_id']

request_with_authentication method, endpoint: endpoint, data: data
end

def self.objects_meta
@objects_meta_cache ||= request_with_authentication :get, endpoint: '/objects/meta'
end
end
8 changes: 7 additions & 1 deletion lib/ontraport/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def self.configuration
# Ontraport.configure do |config|
# config.api_id = 'foo'
# config.api_key = 'bar'
# if Rails.env.development?
# config.debug_mode = true
# end
# end
#
# @return [nil]
Expand All @@ -23,6 +26,9 @@ def self.configure

# The Gem's configuration
class Configuration
attr_accessor :api_id, :api_key
attr_accessor :api_id, :api_key, :debug_mode
def initialize
@debug_mode ||= false
end
end
end
7 changes: 6 additions & 1 deletion lib/ontraport/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ class Response
# Numeric object identifier for a particular object in the schema.
#
# @return [String]
attr_reader :code, :data, :updates, :notifications, :account_id, :misc, :name, :fields, :schema_object_id
#
# @!attribute [r] original_request
# Original HTTParty request only present when debug mode is enabled in config
#
# @return [HTTParty::Request, nil] the original request for debugging
attr_reader :code, :data, :updates, :notifications, :account_id, :misc, :name, :fields, :schema_object_id, :original_request

def initialize args
args.each do |key,val|
Expand Down
2 changes: 1 addition & 1 deletion lib/ontraport/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ontraport
VERSION = '0.1.21'
VERSION = '0.1.3'
end