diff --git a/.gitignore b/.gitignore index 34ac40c..16a444c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ ontraport.sublime-project ontraport.sublime-workspace *.gem -/doc/* \ No newline at end of file +/doc/* +.idea +.ruby-version \ No newline at end of file diff --git a/.yardoc/checksums b/.yardoc/checksums index 381a7f7..61b18cc 100644 --- a/.yardoc/checksums +++ b/.yardoc/checksums @@ -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 diff --git a/.yardoc/object_types b/.yardoc/object_types index c2a3c53..f9d1146 100644 Binary files a/.yardoc/object_types and b/.yardoc/object_types differ diff --git a/.yardoc/objects/root.dat b/.yardoc/objects/root.dat index 3af0c01..a9a2a11 100644 Binary files a/.yardoc/objects/root.dat and b/.yardoc/objects/root.dat differ diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ef68db..2f9e8f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/ontraport.rb b/lib/ontraport.rb index 5ffeb96..234f84e 100644 --- a/lib/ontraport.rb +++ b/lib/ontraport.rb @@ -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 } + # #=> # + # + # @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 } + # #=> # + # + # @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 @@ -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 diff --git a/lib/ontraport/configuration.rb b/lib/ontraport/configuration.rb index bde6ab1..a37fe37 100644 --- a/lib/ontraport/configuration.rb +++ b/lib/ontraport/configuration.rb @@ -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] @@ -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 \ No newline at end of file diff --git a/lib/ontraport/response.rb b/lib/ontraport/response.rb index 275270d..092fa3c 100644 --- a/lib/ontraport/response.rb +++ b/lib/ontraport/response.rb @@ -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| diff --git a/lib/ontraport/version.rb b/lib/ontraport/version.rb index 0c98a2f..78498ae 100644 --- a/lib/ontraport/version.rb +++ b/lib/ontraport/version.rb @@ -1,3 +1,3 @@ module Ontraport - VERSION = '0.1.21' + VERSION = '0.1.3' end