Class: TalonOne::Integration::Client
- Inherits:
-
Object
- Object
- TalonOne::Integration::Client
- Defined in:
- lib/integration/client.rb
Overview
Basic REST client for the TalonOne integration API
Instance Method Summary collapse
- #close_customer_session(session_id) ⇒ Object
- #create_referral_code(campaign_id, advocate_profile_id, friend_id: "", start: nil, expire: nil) ⇒ Object
-
#initialize(config = {}) ⇒ Client
constructor
A new instance of Client.
- #request(method, path, payload = nil, result = TalonOne::Integration::RuleEngineResult) ⇒ Object
- #search_profiles_by_attributes(profileAttr) ⇒ Object
- #track_event(session_id, event_type, value) ⇒ Object
- #update_customer_profile(profile_id, data) ⇒ Object
- #update_customer_session(session_id, data) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/integration/client.rb', line 13 def initialize(config = {}) @endpoint = URI( config[:endpoint] || ENV["TALONONE_ENDPOINT"] || "https://example.talon.one" ) @endpoint.path = @endpoint.path.sub(/\/+$/, '') @http = Net::HTTP.new(@endpoint.host, @endpoint.port) @http.use_ssl = @endpoint.scheme == "https" @application_id = config[:application_id] || ENV["TALONONE_APP_ID"] @application_key = [config[:application_key] || ENV["TALONONE_APP_KEY"]].pack('H*') end |
Instance Method Details
#close_customer_session(session_id) ⇒ Object
54 55 56 |
# File 'lib/integration/client.rb', line 54 def close_customer_session(session_id) update_customer_session session_id, { state: "closed" } end |
#create_referral_code(campaign_id, advocate_profile_id, friend_id: "", start: nil, expire: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/integration/client.rb', line 58 def create_referral_code(campaign_id, advocate_profile_id, friend_id: "", start: nil, expire: nil) newReferral = { campaignId: campaign_id, advocateProfileIntegrationId: advocate_profile_id, friendProfileIntegrationId: friend_id, } if start newReferral[:startDate] = start end if expire newReferral[:expiryDate] = expire end request "Post", "/v1/referrals", newReferral, TalonOne::Integration::ReferralCode end |
#request(method, path, payload = nil, result = TalonOne::Integration::RuleEngineResult) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/integration/client.rb', line 25 def request(method, path, payload = nil, result = TalonOne::Integration::RuleEngineResult) req = Net::HTTP.const_get(method).new(@endpoint.path + path) req.body = Oj.dump payload, (:custom) signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('md5'), @application_key, req.body) req['Content-Type'] = 'application/json' req['Content-Signature'] = "signer=#{@application_id}; signature=#{signature}" res = @http.request(req) if res.code[0] == '2' result.new(Oj.load(res.body, (:strict))) else raise TalonOne::Integration::ClientError.new("#{method.upcase} #{path} -> #{res.code} #{res.body}") end end |
#search_profiles_by_attributes(profileAttr) ⇒ Object
73 74 75 |
# File 'lib/integration/client.rb', line 73 def search_profiles_by_attributes(profileAttr) request "Post", "/v1/customer_profiles_search", { attributes: profileAttr }, TalonOne::Integration::SearchProfilesResult end |
#track_event(session_id, event_type, value) ⇒ Object
42 43 44 |
# File 'lib/integration/client.rb', line 42 def track_event(session_id, event_type, value) request "Post", "/v1/events", { sessionId: session_id, type: event_type, attributes: value } end |
#update_customer_profile(profile_id, data) ⇒ Object
50 51 52 |
# File 'lib/integration/client.rb', line 50 def update_customer_profile(profile_id, data) request "Put", "/v1/customer_profiles/#{profile_id}", data end |
#update_customer_session(session_id, data) ⇒ Object
46 47 48 |
# File 'lib/integration/client.rb', line 46 def update_customer_session(session_id, data) request "Put", "/v1/customer_sessions/#{session_id}", data end |