telerivet 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+
2
+ module Telerivet
3
+
4
+ #
5
+ # Represents a row in a custom data table.
6
+ #
7
+ # For example, each response to a poll is stored as one row in a data table.
8
+ # If a poll has a question with ID 'q1', the verbatim response to that question would be
9
+ # stored in row.vars.q1, and the response code would be stored in row.vars.q1_code.
10
+ #
11
+ # Each custom variable name within a data row corresponds to a different
12
+ # column/field of the data table.
13
+ #
14
+ # Fields:
15
+ #
16
+ # - id (string, max 34 characters)
17
+ # * ID of the data row
18
+ # * Read-only
19
+ #
20
+ # - contact_id
21
+ # * ID of the contact this row is associated with (or null if not associated with any
22
+ # contact)
23
+ # * Updatable via API
24
+ #
25
+ # - from_number (string)
26
+ # * Phone number that this row is associated with (or null if not associated with any
27
+ # phone number)
28
+ # * Updatable via API
29
+ #
30
+ # - vars (Hash)
31
+ # * Custom variables stored for this data row
32
+ # * Updatable via API
33
+ #
34
+ # - table_id
35
+ # * ID of the table this data row belongs to
36
+ # * Read-only
37
+ #
38
+ # - project_id
39
+ # * ID of the project this data row belongs to
40
+ # * Read-only
41
+ #
42
+ class DataRow < Entity
43
+ #
44
+ # Saves any fields or custom variables that have changed for this data row.
45
+ #
46
+ def save()
47
+ super
48
+ end
49
+
50
+ #
51
+ # Deletes this data row.
52
+ #
53
+ def delete()
54
+ @api.do_request("DELETE", get_base_api_path())
55
+ end
56
+
57
+ def id
58
+ get('id')
59
+ end
60
+
61
+ def contact_id
62
+ get('contact_id')
63
+ end
64
+
65
+ def contact_id=(value)
66
+ set('contact_id', value)
67
+ end
68
+
69
+ def from_number
70
+ get('from_number')
71
+ end
72
+
73
+ def from_number=(value)
74
+ set('from_number', value)
75
+ end
76
+
77
+ def table_id
78
+ get('table_id')
79
+ end
80
+
81
+ def project_id
82
+ get('project_id')
83
+ end
84
+
85
+ def get_base_api_path()
86
+ "/projects/#{get('project_id')}/tables/#{get('table_id')}/rows/#{get('id')}"
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,191 @@
1
+
2
+ module Telerivet
3
+
4
+ #
5
+ # Represents a custom data table that can store arbitrary rows.
6
+ #
7
+ # For example, poll services use data tables to store a row for each response.
8
+ #
9
+ #
10
+ # DataTables are schemaless -- each row simply stores custom variables. Each
11
+ # variable name is equivalent to a different "column" of the data table.
12
+ # Telerivet refers to these variables/columns as "fields", and automatically
13
+ # creates a new field for each variable name used in a row of the table.
14
+ #
15
+ # Fields:
16
+ #
17
+ # - id (string, max 34 characters)
18
+ # * ID of the data table
19
+ # * Read-only
20
+ #
21
+ # - name
22
+ # * Name of the data table
23
+ # * Updatable via API
24
+ #
25
+ # - num_rows (int)
26
+ # * Number of rows in the table
27
+ # * Read-only
28
+ #
29
+ # - vars (Hash)
30
+ # * Custom variables stored for this data table
31
+ # * Updatable via API
32
+ #
33
+ # - project_id
34
+ # * ID of the project this data table belongs to
35
+ # * Read-only
36
+ #
37
+ class DataTable < Entity
38
+ #
39
+ # Queries rows in this data table.
40
+ #
41
+ # Arguments:
42
+ # - options (Hash)
43
+ #
44
+ # - time_created (UNIX timestamp)
45
+ # * Filter data rows by the time they were created
46
+ # * Allowed modifiers: time_created[ne], time_created[min], time_created[max]
47
+ #
48
+ # - vars (Hash)
49
+ # * Filter data rows by value of a custom variable (e.g. vars[q1], vars[foo], etc.)
50
+ # * Allowed modifiers: vars[foo][exists], vars[foo][ne], vars[foo][prefix],
51
+ # vars[foo][not_prefix], vars[foo][gte], vars[foo][gt], vars[foo][lt], vars[foo][lte],
52
+ # vars[foo][min], vars[foo][max]
53
+ #
54
+ # - sort
55
+ # * Sort the results based on a field
56
+ # * Allowed values: default
57
+ # * Default: default
58
+ #
59
+ # - sort_dir
60
+ # * Sort the results in ascending or descending order
61
+ # * Allowed values: asc, desc
62
+ # * Default: asc
63
+ #
64
+ # - page_size (int)
65
+ # * Number of results returned per page (max 200)
66
+ # * Default: 50
67
+ #
68
+ # - offset (int)
69
+ # * Number of items to skip from beginning of result set
70
+ # * Default: 0
71
+ #
72
+ # Returns:
73
+ # Telerivet::APICursor (of Telerivet::DataRow)
74
+ #
75
+ def query_rows(options = nil)
76
+ require_relative 'datarow'
77
+ @api.cursor(DataRow, get_base_api_path() + "/rows", options)
78
+ end
79
+
80
+ #
81
+ # Adds a new row to this data table.
82
+ #
83
+ # Arguments:
84
+ # - options (Hash)
85
+ #
86
+ # - contact_id
87
+ # * ID of the contact that this row is associated with (if applicable)
88
+ #
89
+ # - from_number (string)
90
+ # * Phone number that this row is associated with (if applicable)
91
+ #
92
+ # - vars
93
+ # * Custom variables and values to set for this data row
94
+ #
95
+ # Returns:
96
+ # Telerivet::DataRow
97
+ #
98
+ def create_row(options = nil)
99
+ require_relative 'datarow'
100
+ DataRow.new(@api, @api.do_request("POST", get_base_api_path() + "/rows", options))
101
+ end
102
+
103
+ #
104
+ # Retrieves the row in the given table with the given ID.
105
+ #
106
+ # Note: This does not make any API requests until you access a property of the DataRow.
107
+ #
108
+ # Arguments:
109
+ # - id
110
+ # * ID of the row
111
+ # * Required
112
+ #
113
+ # Returns:
114
+ # Telerivet::DataRow
115
+ #
116
+ def get_row_by_id(id)
117
+ require_relative 'datarow'
118
+ return DataRow.new(@api, {'project_id' => self.project_id, 'table_id' => self.id, 'id' => id}, false)
119
+ end
120
+
121
+ #
122
+ # Gets a list of all fields (columns) defined for this data table. The return value is an
123
+ # array of objects with the properties 'name' and 'variable'. (Fields are automatically
124
+ # created any time a DataRow's 'vars' property is updated.)
125
+ #
126
+ # Returns:
127
+ # array
128
+ #
129
+ def get_fields()
130
+ return @api.do_request("GET", get_base_api_path() + "/fields")
131
+ end
132
+
133
+ #
134
+ # Returns the number of rows for each value of a given variable. This can be used to get the
135
+ # total number of responses for each choice in a poll, without making a separate query for
136
+ # each response choice. The return value is an object mapping values to row counts, e.g.
137
+ # `{"yes":7,"no":3}`
138
+ #
139
+ # Arguments:
140
+ # - variable
141
+ # * Variable of field to count by.
142
+ # * Required
143
+ #
144
+ # Returns:
145
+ # object
146
+ #
147
+ def count_rows_by_value(variable)
148
+ return @api.do_request("GET", get_base_api_path() + "/count_rows_by_value", {'variable' => variable})
149
+ end
150
+
151
+ #
152
+ # Saves any fields that have changed for this data table.
153
+ #
154
+ def save()
155
+ super
156
+ end
157
+
158
+ #
159
+ # Permanently deletes the given data table, including all its rows
160
+ #
161
+ def delete()
162
+ @api.do_request("DELETE", get_base_api_path())
163
+ end
164
+
165
+ def id
166
+ get('id')
167
+ end
168
+
169
+ def name
170
+ get('name')
171
+ end
172
+
173
+ def name=(value)
174
+ set('name', value)
175
+ end
176
+
177
+ def num_rows
178
+ get('num_rows')
179
+ end
180
+
181
+ def project_id
182
+ get('project_id')
183
+ end
184
+
185
+ def get_base_api_path()
186
+ "/projects/#{get('project_id')}/tables/#{get('id')}"
187
+ end
188
+
189
+ end
190
+
191
+ end
@@ -0,0 +1,124 @@
1
+ module Telerivet
2
+
3
+ class Entity
4
+
5
+ def initialize(api, data, is_loaded = true)
6
+ @api = api
7
+ @vars = nil
8
+ @dirty = {}
9
+ @data = {}
10
+ set_data(data)
11
+ @is_loaded = is_loaded
12
+ end
13
+
14
+ def set_data(data)
15
+ @data = data
16
+
17
+ if data.has_key?('vars')
18
+ @vars = CustomVars.new(data['vars'])
19
+ else
20
+ @vars = CustomVars.new({})
21
+ end
22
+ end
23
+
24
+ def load_data()
25
+ if !@is_loaded
26
+ @is_loaded = true
27
+ set_data(@api.do_request('GET', get_base_api_path()))
28
+ end
29
+ end
30
+
31
+ def vars
32
+ @vars
33
+ end
34
+
35
+ def get(name)
36
+ if @data.has_key?(name)
37
+ return @data[name]
38
+ elsif @is_loaded
39
+ return nil
40
+ end
41
+
42
+ load_data()
43
+
44
+ return @data[name]
45
+ end
46
+
47
+ def set(name, value)
48
+ if !@is_loaded
49
+ loadData()
50
+ end
51
+
52
+ @data[name] = value
53
+ @dirty[name] = value
54
+ end
55
+
56
+ def save()
57
+ dirty_props = @dirty
58
+
59
+ if @vars != nil
60
+ dirty_vars = @vars.get_dirty_variables()
61
+ @dirty['vars'] = dirty_vars if dirty_vars.length() > 0
62
+ end
63
+
64
+ @api.do_request('POST', get_base_api_path(), @dirty)
65
+ @dirty = {}
66
+
67
+ @vars.clear_dirty_variables() if @vars != nil
68
+ end
69
+
70
+ def to_s()
71
+ res = self.class.name
72
+
73
+ if not @is_loaded
74
+ res += " (not loaded)"
75
+ end
76
+
77
+ res += " JSON: " + JSON.dump(@data)
78
+
79
+ return res
80
+ end
81
+
82
+ def get_base_api_path()
83
+ abstract
84
+ end
85
+ end
86
+
87
+ class CustomVars
88
+ def initialize(vars)
89
+ @vars = vars
90
+ @dirty = {}
91
+ end
92
+
93
+ def all()
94
+ @vars
95
+ end
96
+
97
+ def get_dirty_variables()
98
+ @dirty
99
+ end
100
+
101
+ def clear_dirty_variables()
102
+ @dirty = {}
103
+ end
104
+
105
+ def get(name)
106
+ @vars[name]
107
+ end
108
+
109
+ def set(name, value)
110
+ @vars[name] = value
111
+ @dirty[name] = value
112
+ end
113
+
114
+ def method_missing(m, *args)
115
+ name = m.to_s
116
+ if name.end_with?('=')
117
+ set(name.chop, args[0])
118
+ else
119
+ get(name)
120
+ end
121
+ end
122
+ end
123
+
124
+ end
@@ -0,0 +1,181 @@
1
+
2
+ module Telerivet
3
+
4
+ #
5
+ # Represents a group used to organize contacts within Telerivet.
6
+ #
7
+ # Fields:
8
+ #
9
+ # - id (string, max 34 characters)
10
+ # * ID of the group
11
+ # * Read-only
12
+ #
13
+ # - name
14
+ # * Name of the group
15
+ # * Updatable via API
16
+ #
17
+ # - num_members (int)
18
+ # * Number of contacts in the group
19
+ # * Read-only
20
+ #
21
+ # - time_created (UNIX timestamp)
22
+ # * Time the group was created in Telerivet
23
+ # * Read-only
24
+ #
25
+ # - vars (Hash)
26
+ # * Custom variables stored for this group
27
+ # * Updatable via API
28
+ #
29
+ # - project_id
30
+ # * ID of the project this group belongs to
31
+ # * Read-only
32
+ #
33
+ class Group < Entity
34
+ #
35
+ # Queries contacts that are members of the given group.
36
+ #
37
+ # Arguments:
38
+ # - options (Hash)
39
+ #
40
+ # - name
41
+ # * Filter contacts by name
42
+ # * Allowed modifiers: name[ne], name[prefix], name[not_prefix], name[gte], name[gt],
43
+ # name[lt], name[lte]
44
+ #
45
+ # - phone_number
46
+ # * Filter contacts by phone number
47
+ # * Allowed modifiers: phone_number[ne], phone_number[prefix],
48
+ # phone_number[not_prefix], phone_number[gte], phone_number[gt], phone_number[lt],
49
+ # phone_number[lte]
50
+ #
51
+ # - time_created (UNIX timestamp)
52
+ # * Filter contacts by time created
53
+ # * Allowed modifiers: time_created[ne], time_created[min], time_created[max]
54
+ #
55
+ # - last_message_time (UNIX timestamp)
56
+ # * Filter contacts by last time a message was sent or received
57
+ # * Allowed modifiers: last_message_time[exists], last_message_time[ne],
58
+ # last_message_time[min], last_message_time[max]
59
+ #
60
+ # - vars (Hash)
61
+ # * Filter contacts by value of a custom variable (e.g. vars[email], vars[foo], etc.)
62
+ # * Allowed modifiers: vars[foo][exists], vars[foo][ne], vars[foo][prefix],
63
+ # vars[foo][not_prefix], vars[foo][gte], vars[foo][gt], vars[foo][lt], vars[foo][lte],
64
+ # vars[foo][min], vars[foo][max]
65
+ #
66
+ # - sort
67
+ # * Sort the results based on a field
68
+ # * Allowed values: default, name, phone_number, last_message_time
69
+ # * Default: default
70
+ #
71
+ # - sort_dir
72
+ # * Sort the results in ascending or descending order
73
+ # * Allowed values: asc, desc
74
+ # * Default: asc
75
+ #
76
+ # - page_size (int)
77
+ # * Number of results returned per page (max 200)
78
+ # * Default: 50
79
+ #
80
+ # - offset (int)
81
+ # * Number of items to skip from beginning of result set
82
+ # * Default: 0
83
+ #
84
+ # Returns:
85
+ # Telerivet::APICursor (of Telerivet::Contact)
86
+ #
87
+ def query_contacts(options = nil)
88
+ require_relative 'contact'
89
+ @api.cursor(Contact, get_base_api_path() + "/contacts", options)
90
+ end
91
+
92
+ #
93
+ # Queries scheduled messages to the given group.
94
+ #
95
+ # Arguments:
96
+ # - options (Hash)
97
+ #
98
+ # - message_type
99
+ # * Filter scheduled messages by message_type
100
+ # * Allowed values: sms, mms, ussd, call
101
+ #
102
+ # - time_created (UNIX timestamp)
103
+ # * Filter scheduled messages by time_created
104
+ # * Allowed modifiers: time_created[ne], time_created[min], time_created[max]
105
+ #
106
+ # - next_time (UNIX timestamp)
107
+ # * Filter scheduled messages by next_time
108
+ # * Allowed modifiers: next_time[exists], next_time[ne], next_time[min],
109
+ # next_time[max]
110
+ #
111
+ # - sort
112
+ # * Sort the results based on a field
113
+ # * Allowed values: default, name
114
+ # * Default: default
115
+ #
116
+ # - sort_dir
117
+ # * Sort the results in ascending or descending order
118
+ # * Allowed values: asc, desc
119
+ # * Default: asc
120
+ #
121
+ # - page_size (int)
122
+ # * Number of results returned per page (max 200)
123
+ # * Default: 50
124
+ #
125
+ # - offset (int)
126
+ # * Number of items to skip from beginning of result set
127
+ # * Default: 0
128
+ #
129
+ # Returns:
130
+ # Telerivet::APICursor (of Telerivet::ScheduledMessage)
131
+ #
132
+ def query_scheduled_messages(options = nil)
133
+ require_relative 'scheduledmessage'
134
+ @api.cursor(ScheduledMessage, get_base_api_path() + "/scheduled", options)
135
+ end
136
+
137
+ #
138
+ # Saves any fields that have changed for this group.
139
+ #
140
+ def save()
141
+ super
142
+ end
143
+
144
+ #
145
+ # Deletes this group (Note: no contacts are deleted.)
146
+ #
147
+ def delete()
148
+ @api.do_request("DELETE", get_base_api_path())
149
+ end
150
+
151
+ def id
152
+ get('id')
153
+ end
154
+
155
+ def name
156
+ get('name')
157
+ end
158
+
159
+ def name=(value)
160
+ set('name', value)
161
+ end
162
+
163
+ def num_members
164
+ get('num_members')
165
+ end
166
+
167
+ def time_created
168
+ get('time_created')
169
+ end
170
+
171
+ def project_id
172
+ get('project_id')
173
+ end
174
+
175
+ def get_base_api_path()
176
+ "/projects/#{get('project_id')}/groups/#{get('id')}"
177
+ end
178
+
179
+ end
180
+
181
+ end
@@ -0,0 +1,134 @@
1
+
2
+ module Telerivet
3
+
4
+ #
5
+ # Represents a label used to organize messages within Telerivet.
6
+ #
7
+ # Fields:
8
+ #
9
+ # - id (string, max 34 characters)
10
+ # * ID of the label
11
+ # * Read-only
12
+ #
13
+ # - name
14
+ # * Name of the label
15
+ # * Updatable via API
16
+ #
17
+ # - time_created (UNIX timestamp)
18
+ # * Time the label was created in Telerivet
19
+ # * Read-only
20
+ #
21
+ # - vars (Hash)
22
+ # * Custom variables stored for this label
23
+ # * Updatable via API
24
+ #
25
+ # - project_id
26
+ # * ID of the project this label belongs to
27
+ # * Read-only
28
+ #
29
+ class Label < Entity
30
+ #
31
+ # Queries messages with the given label.
32
+ #
33
+ # Arguments:
34
+ # - options (Hash)
35
+ #
36
+ # - direction
37
+ # * Filter messages by direction
38
+ # * Allowed values: incoming, outgoing
39
+ #
40
+ # - message_type
41
+ # * Filter messages by message_type
42
+ # * Allowed values: sms, mms, ussd, call
43
+ #
44
+ # - source
45
+ # * Filter messages by source
46
+ # * Allowed values: phone, provider, web, api, service, webhook, scheduled
47
+ #
48
+ # - starred (bool)
49
+ # * Filter messages by starred/unstarred
50
+ #
51
+ # - status
52
+ # * Filter messages by status
53
+ # * Allowed values: ignored, processing, received, sent, queued, failed,
54
+ # failed_queued, cancelled, delivered, not_delivered
55
+ #
56
+ # - time_created[min] (UNIX timestamp)
57
+ # * Filter messages created on or after a particular time
58
+ #
59
+ # - time_created[max] (UNIX timestamp)
60
+ # * Filter messages created before a particular time
61
+ #
62
+ # - contact_id
63
+ # * ID of the contact who sent/received the message
64
+ #
65
+ # - phone_id
66
+ # * ID of the phone that sent/received the message
67
+ #
68
+ # - sort
69
+ # * Sort the results based on a field
70
+ # * Allowed values: default
71
+ # * Default: default
72
+ #
73
+ # - sort_dir
74
+ # * Sort the results in ascending or descending order
75
+ # * Allowed values: asc, desc
76
+ # * Default: asc
77
+ #
78
+ # - page_size (int)
79
+ # * Number of results returned per page (max 200)
80
+ # * Default: 50
81
+ #
82
+ # - offset (int)
83
+ # * Number of items to skip from beginning of result set
84
+ # * Default: 0
85
+ #
86
+ # Returns:
87
+ # Telerivet::APICursor (of Telerivet::Message)
88
+ #
89
+ def query_messages(options = nil)
90
+ require_relative 'message'
91
+ @api.cursor(Message, get_base_api_path() + "/messages", options)
92
+ end
93
+
94
+ #
95
+ # Saves any fields that have changed for the label.
96
+ #
97
+ def save()
98
+ super
99
+ end
100
+
101
+ #
102
+ # Deletes the given label (Note: no messages are deleted.)
103
+ #
104
+ def delete()
105
+ @api.do_request("DELETE", get_base_api_path())
106
+ end
107
+
108
+ def id
109
+ get('id')
110
+ end
111
+
112
+ def name
113
+ get('name')
114
+ end
115
+
116
+ def name=(value)
117
+ set('name', value)
118
+ end
119
+
120
+ def time_created
121
+ get('time_created')
122
+ end
123
+
124
+ def project_id
125
+ get('project_id')
126
+ end
127
+
128
+ def get_base_api_path()
129
+ "/projects/#{get('project_id')}/labels/#{get('id')}"
130
+ end
131
+
132
+ end
133
+
134
+ end