dialog-api 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/dialog-api/api/conversation.rb +19 -0
- data/lib/dialog-api/api/interlocutor.rb +26 -0
- data/lib/dialog-api/api/message.rb +21 -0
- data/lib/dialog-api/api/track.rb +29 -0
- data/lib/dialog-api/client.rb +34 -0
- data/lib/dialog-api/configuration.rb +36 -0
- data/lib/dialog-api/request.rb +55 -0
- data/lib/dialog-api/version.rb +3 -0
- data/lib/dialog-api.rb +14 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8e372a4430bbd304f323b1bc031d73f16a566ca5
|
4
|
+
data.tar.gz: b6d19f20daac02a080787b8253587ec2dcca9235
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ae426620a85e5b85b96156797de093f379a10a264619245a5b5568023fa151ec073e53b1bbe621b7a79a0e146f328d520bfcf196af4baea2f2ea54f19c84ae9f
|
7
|
+
data.tar.gz: 1c5ea3c74da2304f4d0e7671bf30f10e6695fe0c3438642907812d322ef5e7509911629de132aba3176d9fb2a80a3df92b592e102c13f41d8a4909a25ee7c9a1
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Dialog
|
2
|
+
module API
|
3
|
+
module Conversation
|
4
|
+
|
5
|
+
# Lists all conversations
|
6
|
+
# @return [Array]
|
7
|
+
def conversations
|
8
|
+
get("b/#{bot_id}/conversations")
|
9
|
+
end
|
10
|
+
|
11
|
+
# Retrieves a conversation
|
12
|
+
# @param id [String]
|
13
|
+
# @return [Hash]
|
14
|
+
def conversation(id)
|
15
|
+
get("b/#{bot_id}/conversations/#{id}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Dialog
|
2
|
+
module API
|
3
|
+
module Interlocutor
|
4
|
+
|
5
|
+
# Lists all interlocutors
|
6
|
+
# @return [Array]
|
7
|
+
def interlocutors
|
8
|
+
get("b/#{bot_id}/interlocutors")
|
9
|
+
end
|
10
|
+
|
11
|
+
# Retrieves an interlocutor
|
12
|
+
# @param id [String] Interlocutor Id
|
13
|
+
# @return [Hash]
|
14
|
+
def interlocutor(id)
|
15
|
+
get("b/#{bot_id}/interlocutors#{id}")
|
16
|
+
end
|
17
|
+
|
18
|
+
# Creates an interlocutor
|
19
|
+
# @param attributes [Hash]
|
20
|
+
# @return [Hash]
|
21
|
+
def create_interlocutor(attributes)
|
22
|
+
post("b/#{bot_id}/interlocutors", body: { interlocutor: attributes })
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Dialog
|
2
|
+
module API
|
3
|
+
module Message
|
4
|
+
|
5
|
+
# Lists all messages associated to a conversation
|
6
|
+
# @param conversation_id [String]
|
7
|
+
# @return
|
8
|
+
def messages(conversation_id)
|
9
|
+
get("b/#{bot_id}/conversations/#{conversation_id}/messages")
|
10
|
+
end
|
11
|
+
|
12
|
+
# Retrieves a message
|
13
|
+
# @param id [String]
|
14
|
+
# @param conversation_id [String]
|
15
|
+
# @return
|
16
|
+
def message(id, conversation_id)
|
17
|
+
get("b/#{bot_id}/conversations/#{conversation_id}/messages/#{id}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Dialog
|
2
|
+
module API
|
3
|
+
module Track
|
4
|
+
|
5
|
+
# Tracks a message
|
6
|
+
# @param payload [Hash]
|
7
|
+
# @return
|
8
|
+
def track(payload)
|
9
|
+
body = {
|
10
|
+
message: {
|
11
|
+
conversation_distinct_id: payload[:conversation_distinct_id],
|
12
|
+
creator_distinct_id: payload[:creator_distinct_id],
|
13
|
+
creator_type: payload[:creator_type],
|
14
|
+
distinct_id: payload[:distinct_id],
|
15
|
+
platform: payload[:platform],
|
16
|
+
provider: payload[:provider],
|
17
|
+
mtype: payload[:mtype],
|
18
|
+
sent_at: payload[:sent_at],
|
19
|
+
properties: {
|
20
|
+
text: payload[:text]
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
post("b/#{bot_id}/track", body: body)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'dialog-api/request'
|
2
|
+
require 'dialog-api/api/conversation'
|
3
|
+
require 'dialog-api/api/interlocutor'
|
4
|
+
require 'dialog-api/api/message'
|
5
|
+
require 'dialog-api/api/track'
|
6
|
+
|
7
|
+
module Dialog
|
8
|
+
class Client
|
9
|
+
include Dialog::Request
|
10
|
+
include Dialog::API::Conversation
|
11
|
+
include Dialog::API::Interlocutor
|
12
|
+
include Dialog::API::Message
|
13
|
+
include Dialog::API::Track
|
14
|
+
|
15
|
+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
|
16
|
+
|
17
|
+
# @param options [Hash]
|
18
|
+
def initialize(options = {})
|
19
|
+
options = Dialog.options.merge(options)
|
20
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
21
|
+
send("#{key}=", options[key])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Wraps an url into a trackable Dialog url
|
26
|
+
# @param id [String] A conversation distinct Id provided by the platform or the provider
|
27
|
+
# @param url [String]
|
28
|
+
# @return [String]
|
29
|
+
def link(id, url)
|
30
|
+
escaped = URI.escape(url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
31
|
+
URI.join(api_endpoint, "v1/click/", id, "?url=#{escaped}").to_s
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'dialog-api/version'
|
2
|
+
|
3
|
+
module Dialog
|
4
|
+
module Configuration
|
5
|
+
VALID_OPTIONS_KEYS = [:api_endpoint, :api_token, :user_agent, :bot_id, :on_error].freeze
|
6
|
+
|
7
|
+
DEFAULT_API_ENDPOINT = "https://api.dialoganalytics.com/v1/"
|
8
|
+
DEFAULT_API_TOKEN = nil
|
9
|
+
DEFAULT_USER_AGENT = "Dialog Ruby Gem #{Dialog::VERSION}".freeze
|
10
|
+
|
11
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
12
|
+
|
13
|
+
def self.extended(base)
|
14
|
+
base.reset!
|
15
|
+
end
|
16
|
+
|
17
|
+
# Convenience method to allow configuration options to be set in a block
|
18
|
+
def configure
|
19
|
+
yield self
|
20
|
+
end
|
21
|
+
|
22
|
+
def options
|
23
|
+
Hash[ * VALID_OPTIONS_KEYS.map { |key| [key, send(key)] }.flatten ]
|
24
|
+
end
|
25
|
+
|
26
|
+
def reset!
|
27
|
+
self.api_endpoint = DEFAULT_API_ENDPOINT
|
28
|
+
self.api_token = DEFAULT_API_TOKEN
|
29
|
+
self.user_agent = DEFAULT_USER_AGENT
|
30
|
+
self.on_error = Proc.new {}
|
31
|
+
self.bot_id = nil
|
32
|
+
|
33
|
+
return true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'http'
|
2
|
+
|
3
|
+
module Dialog
|
4
|
+
module Request
|
5
|
+
|
6
|
+
# Performs a HTTP Get request
|
7
|
+
#
|
8
|
+
# @param path [String]
|
9
|
+
# @param params [Hash]
|
10
|
+
def get(path, params: {})
|
11
|
+
request(:get, URI.parse(api_endpoint).merge(path), params: params)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Performs a HTTP Post request
|
15
|
+
#
|
16
|
+
# @param path [String]
|
17
|
+
# @param params [Hash]
|
18
|
+
# @param body [Hash]
|
19
|
+
def post(path, params: {}, body: {})
|
20
|
+
request(:post, URI.parse(api_endpoint).merge(path), params: params, body: body)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# @return [HTTP::Client]
|
27
|
+
# @raise [ArgumentError]
|
28
|
+
def request(method, path, params: {}, body: {})
|
29
|
+
raise ArgumentError, ("Please configure Dialog.api_token first") unless api_token
|
30
|
+
raise ArgumentError, ("Please configure Dialog.bot_id first") unless bot_id
|
31
|
+
|
32
|
+
headers = {
|
33
|
+
'accept' => "application/json",
|
34
|
+
'User-Agent' => Dialog.user_agent,
|
35
|
+
'Authorization' => "Api-Key #{api_token}"
|
36
|
+
}
|
37
|
+
|
38
|
+
response = Http.headers(headers).send(method, path, params: params, json: body)
|
39
|
+
|
40
|
+
# Pass on errors when HTTP status included in 400 to 599
|
41
|
+
if (400..599).include?(response.code)
|
42
|
+
begin
|
43
|
+
body = response.parse['error']
|
44
|
+
rescue HTTP::Error
|
45
|
+
body = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
on_error.call(response.code, response.reason, body)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Return parsed json body
|
52
|
+
response.parse
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/dialog-api.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'dialog-api/configuration'
|
2
|
+
require 'dialog-api/client'
|
3
|
+
|
4
|
+
module Dialog
|
5
|
+
extend Configuration
|
6
|
+
|
7
|
+
# Alias for Dialog::Client.new
|
8
|
+
#
|
9
|
+
# @return [Dialog::Client]
|
10
|
+
def new(options = {})
|
11
|
+
Dialog::Client.new(options)
|
12
|
+
end
|
13
|
+
module_function :new
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dialog-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Philippe Dionne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
description: Dialog is a conversational analytics platform. See https://dialoganalytics.com
|
28
|
+
for details.
|
29
|
+
email:
|
30
|
+
- p@dialoganalytics.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/dialog-api.rb
|
36
|
+
- lib/dialog-api/api/conversation.rb
|
37
|
+
- lib/dialog-api/api/interlocutor.rb
|
38
|
+
- lib/dialog-api/api/message.rb
|
39
|
+
- lib/dialog-api/api/track.rb
|
40
|
+
- lib/dialog-api/client.rb
|
41
|
+
- lib/dialog-api/configuration.rb
|
42
|
+
- lib/dialog-api/request.rb
|
43
|
+
- lib/dialog-api/version.rb
|
44
|
+
homepage: https://github.com/dialoganalytics/dialog-ruby
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 2.6.6
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: A ruby client for the Dialog API.
|
68
|
+
test_files: []
|