Class: Callrail::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/callrail.rb

Constant Summary collapse

MAX_PAGE_SIZE =
'250'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Api

Returns a new instance of Api.



12
13
14
15
16
17
# File 'lib/callrail.rb', line 12

def initialize(opts = {})
  @api_version = opts[:api_version] || "v3"
  @url = "https://api.callrail.com/#{@api_version}/a"
  @auth = "Token token=" + opts[:key]
  @account_id = opts[:account_id].to_s if opts[:account_id]  
end

Instance Method Details

#create_company(opts = {}) ⇒ Object



154
155
156
157
158
159
# File 'lib/callrail.rb', line 154

def create_company(opts = {}) # http://apidocs.callrail.com/#time-zones
  params = set_params(opts)
  path = "/" + @account_id + "/companies.json"
  response = parse_json(RestClient.post(@url+path, params ,:Authorization => @auth))
  return response   
end

#create_integration(opts = {}) ⇒ Object



248
249
250
251
252
# File 'lib/callrail.rb', line 248

def create_integration(opts ={})
  opts[:path] = "/" + @account_id + "/integrations.json"
  params = set_params(opts)
  return parse_json(RestClient.post(@url+opts[:path], params ,:Authorization => @auth))
end

#create_tracker(opts = {}) ⇒ Object



224
225
226
227
228
# File 'lib/callrail.rb', line 224

def create_tracker(opts={})
  opts[:path] = "/" + @account_id + "/trackers.json"
  params = set_params(opts)
  return parse_json(RestClient.post(@url+opts[:path], params ,:Authorization => @auth))
end

#create_user(opts = {}) ⇒ Object



179
180
181
182
183
# File 'lib/callrail.rb', line 179

def create_user( opts = {})
  params = set_params(opts)
  path = "/" + @account_id + "/users.json"
  return parse_json(RestClient.post(@url+path, params ,:Authorization => @auth))
end

#disable_company(opts = {}) ⇒ Object



167
168
169
170
# File 'lib/callrail.rb', line 167

def disable_company( opts = {})
  path = "/" + @account_id + "/companies/" + opts[:company_id].to_s
  return parse_json(RestClient.delete(@url+path, :Authorization => @auth))
end

#disable_integration(opts = {}) ⇒ Object



260
261
262
263
# File 'lib/callrail.rb', line 260

def disable_integration(opts={})
   path = "/" + @account_id + "/integrations/" + opts[:integration_id].to_s + ".json"
   return parse_json(RestClient.delete(@url+path, :Authorization => @auth))
end

#disable_tracker(opts = {}) ⇒ Object



236
237
238
239
# File 'lib/callrail.rb', line 236

def disable_tracker(opts={})
  path = "/" + @account_id + "/trackers/" + opts[:tracker_id].to_s + ".json"
  return parse_json(RestClient.delete(@url+path, :Authorization => @auth))
end

#get_accounts(opts = {}) ⇒ Object

Account



141
142
143
144
145
# File 'lib/callrail.rb', line 141

def get_accounts(opts = {})
  opts[:path] = (opts[:account_id]) ? "/" + opts[:account_id].to_s + ".json" : ".json"
  opts[:data] = "accounts" unless  opts[:account_id]
  return get_responses(opts)  
end

#get_call_recording(opts = {}) ⇒ Object



209
210
211
212
213
# File 'lib/callrail.rb', line 209

def get_call_recording( opts={} )
  opts[:path] =  "/" + @account_id + "/calls/" + opts[:call_id].to_s + "/recording.json"
  opts[:data] = "url"
  return get_responses(opts).first
end

#get_calls(opts = {}) ⇒ Object

Calls



192
193
194
195
196
# File 'lib/callrail.rb', line 192

def get_calls( opts={} )
  opts[:path] = (opts[:call_id]) ? "/" + @account_id + "/calls/" + opts[:call_id].to_s + ".json" : "/" + @account_id + "/calls.json" 
  opts[:data] = "calls"  unless opts[:call_id]
  return get_responses(opts)
end

#get_calls_summary(opts = {}) ⇒ Object



198
199
200
201
# File 'lib/callrail.rb', line 198

def get_calls_summary( opts={} )
  opts[:path] =  (opts[:time_series] == true) ? "/" + @account_id + "/calls/timeseries.json" : "/" + @account_id + "/calls/summary.json" 
  return get_responses(opts)
end

#get_companies(opts = {}) ⇒ Object

Company



148
149
150
151
152
# File 'lib/callrail.rb', line 148

def get_companies(opts = {}) 
  opts[:path] = (opts[:company_id]) ? "/" + @account_id + "/companies/" + opts[:company_id].to_s + ".json" : "/" + @account_id + "/companies.json"
  opts[:data] = "companies" unless opts[:company_id]
  return get_responses(opts)
end

#get_integrations(opts = {}) ⇒ Object

Integrations



242
243
244
245
246
# File 'lib/callrail.rb', line 242

def get_integrations(opts ={})
  opts[:path] = (opts[:integration_id]) ? "/" + @account_id + "/integrations/" + opts[:integration_id].to_s + ".json" : "/" + @account_id + "/integrations.json"
  opts[:data] = "integrations" unless opts[:integration_id]
  return get_responses(opts)
end

#get_responses(opts = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/callrail.rb', line 123

def get_responses(opts = {})
  responses = []
  params = set_params(opts)
  response = parse_json(RestClient.get(@url+params[:path], params: params,:Authorization => @auth)).body
  total_pages = response["total_pages"] || 1
  total_records = response["total_records"] || 1              

  while total_pages > 0
    response = (opts[:data]) ? response[opts[:data]] : response 
    responses.push(response)
    params[:page] += 1 unless opts[:page]
    total_pages -= 1     
    response = parse_json(RestClient.get(@url+params[:path], params: params,:Authorization => @auth)).body unless total_pages < 1            
  end
  return responses.flatten! || responses
end

#get_trackers(opts = {}) ⇒ Object

Tracker



218
219
220
221
222
# File 'lib/callrail.rb', line 218

def get_trackers(opts={})
  opts[:path] = (opts[:tracker_id]) ? "/" + @account_id + "/trackers/" + opts[:tracker_id].to_s + ".json" : "/" + @account_id + "/trackers.json"
  opts[:data] = "trackers" unless opts[:tracker_id]
  return get_responses(opts)
end

#get_users(opts = {}) ⇒ Object

User



173
174
175
176
177
# File 'lib/callrail.rb', line 173

def get_users( opts={} )
  opts[:path] = (opts[:user_id]) ? "/" + @account_id + "/users/" + opts[:user_id].to_s + ".json" : "/" + @account_id + "/users.json"
  opts[:data] = "users" unless opts[:user_id]
  return get_responses(opts)
end

#parse_json(response) ⇒ Object



23
24
25
26
# File 'lib/callrail.rb', line 23

def parse_json(response)
  body = JSON.parse(response.to_str) if response.code == 200 || response.code == 201
  OpenStruct.new(code: response.code, body: body)
end

#set_account_id(opts = {}) ⇒ Object



19
20
21
# File 'lib/callrail.rb', line 19

def (opts = {})
  @account_id = opts[:account_id].to_s
end

#set_params(opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/callrail.rb', line 28

def set_params(opts = {})
  params = {}
  #Result params
    params[:date_range] = opts[:date_range] if opts[:date_range]
      # Values: recent, today, yesterday, last_7_days, last_30_days, this_month, last_month, all_time
    params[:start_date] = opts[:start_date] if opts[:start_date]
      # ex: “2015-09-05” for all calls after and including September 9, 2015 - “2015-09-05T10:00” for all calls after 10 AM September 5, 2015
    params[:end_date] = opts[:end_date] if opts[:end_date]
      # ex: “2015-10-05” for all calls before and including October 9, 2015 - “2015-09-05T10:00” for all calls before 10 AM September 5, 2015
    params[:sort] = opts[:sort] if opts[:sort]
      # ex: /users.json?sort=email Will return a list of user objects for the target account, sorted by email address in alphabetical order.
    params[:search] = opts[:search] if opts[:search]
      # ex: users.json?search=belcher Will return a list of user objects in the target account that match the name given.
    params[:fields] = opts[:fields] if opts[:fields]
      # ex: calls/444941612.json?fields=company_id,company_name Will return the additional user requested fields for the target call.
    params[:page] = opts[:page] || 1
    params[:per_page] = opts[:per_page] || MAX_PAGE_SIZE
    params[:path] = opts[:path] if opts[:path]
  #Filters
    if opts[:filtering]
      opts[:filtering].each do |filter|
        params[filter[:field].to_sym] = filter[:value]            
      end
    end
  #Shared Params
    params[:name] = opts[:name] if opts[:name]
  #Account Params
    # Sorting: id, name
  #Company Params
    # Sorting: id, name
    # Filtering: status
    # Searching: name
    params[:callscore_enabled] = opts[:callscore_enabled] if opts[:callscore_enabled]
    params[:keyword_spotting_enabled] = opts[:keyword_spotting_enabled] if opts[:keyword_spotting_enabled]
    params[:callscribe_enabled] = opts[:callscribe_enabled] if opts[:callscribe_enabled]
    params[:time_zone] = opts[:time_zone] if opts[:time_zone]
      # USA Values: America/New_York (Eastern Time Zone), America/Indiana/Indianapolis (Indiana Time Zone), America/Chicago (Central Time Zone), 
      #             America/Denver (Mountain Time Zone), America/Phoenix (Arizona Time Zone), America/Los_Angeles (Pacific Time Zone)
      #             Full List: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
    params[:swap_exclude_jquery] = opts[:swap_exclude_jquery] if opts[:swap_exclude_jquery]
    params[:swap_ppc_override] = opts[:swap_ppc_override] if opts[:swap_ppc_override]
    params[:swap_landing_override] = opts[:swap_landing_override] if opts[:swap_landing_override]
    params[:swap_cookie_duration] = opts[:swap_cookie_duration] if opts[:swap_cookie_duration]
  #User Params
    # Sorting: id, email, created_at
    # Searching: first_name, last_name, email
    params[:first_name] = opts[:first_name] if opts[:first_name]
    params[:last_name] = opts[:last_name] if opts[:last_name]
    params[:email] = opts[:email] if opts[:email]
    params[:role] = opts[:role] if opts[:role]      
    params[:password] = opts[:password] if opts[:password]
    params[:companies] = opts[:companies] if opts[:companies]
    params[:company_ids] = opts[:company_ids] if opts[:company_ids] #Callrail disabled
  #Tracker Params
    # Filtering: type, status
    params[:type] = opts[:type] if opts[:type]
    params[:company_id] = opts[:company_id] if opts[:company_id]
    params[:call_flow] = opts[:call_flow] if opts[:call_flow]
    params[:pool_size] = opts[:pool_size] if opts[:pool_size]
    params[:pool_numbers] = opts[:pool_numbers] if opts[:pool_numbers]
    params[:source] = opts[:source] if opts[:source]
    params[:swap_targets] = opts[:swap_targets] if opts[:swap_targets]
    params[:whisper_message] = opts[:whisper_message] if opts[:whisper_message]
    params[:sms_enabled] = opts[:sms_enabled] if opts[:sms_enabled]
    params[:tracking_number] = opts[:tracking_number] if opts[:tracking_number]
  #Integration Params
    params[:config] = opts[:config] if opts[:config] 
    params[:state] = opts[:state] if opts[:state] 
  #Call Params
    # Sorting: customer_name, customer_phone_number, duration, start_time, source
    # Filtering: date_range, answer_status, device, direction, lead_status
    # Searching: caller_name, note, source, dialed_number, caller_number, outgoing_number
    # Summary Grouping: source, keywords, campaign, referrer, landing_page, or company
    # Summary Fields: total_calls, missed_calls, answered_calls, first_time_callers, average_duration, formatted_average_duration, leads. Defaults to total_calls
    params[:tags] = opts[:tags] if opts[:tags]
    params[:note] = opts[:note] if opts[:note]
    params[:value] = opts[:value] if opts[:value]
    params[:lead_status] = opts[:lead_status] if opts[:lead_status]
    params[:group_by] = opts[:group_by] if opts[:group_by]
    params[:device] = opts[:device] if opts[:device]
    params[:min_duration] = opts[:min_duration] if opts[:min_duration]
    params[:max_duration] = opts[:max_duration] if opts[:max_duration]
    params[:tracker_ids] = opts[:tracker_ids] if opts[:tracker_ids]
    params[:direction] = opts[:direction] if opts[:direction]
    params[:answer_status] = opts[:answer_status] if opts[:answer_status]
    params[:first_time_callers] = opts[:first_time_callers] if opts[:first_time_callers]
    params[:agent] = opts[:agent] if opts[:agent]
  #Text Message Params
    # Filtering: date_range
    # Searching: customer_phone_number, customer_name
  #pagination

  return params
end

#update_call(opts = {}) ⇒ Object



203
204
205
206
207
# File 'lib/callrail.rb', line 203

def update_call(opts = {})
  params = set_params(opts) 
  path =  "/" + @account_id + "/calls/" + opts[:call_id].to_s + ".json"
  return parse_json(RestClient.put(@url+path, params, :Authorization => @auth))      
end

#update_company(opts = {}) ⇒ Object



161
162
163
164
165
# File 'lib/callrail.rb', line 161

def update_company(opts = {})
  params = set_params(opts) 
  path = "/" + @account_id + "/companies/" + opts[:company_id].to_s + ".json"
  return parse_json(RestClient.put(@url+path, params, :Authorization => @auth)).body      
end

#update_integration(opts = {}) ⇒ Object



254
255
256
257
258
# File 'lib/callrail.rb', line 254

def update_integration(opts = {})
   params = set_params(opts) 
   path = "/" + @account_id + "/integrations/" + opts[:integration_id].to_s + ".json"
   return parse_json(RestClient.put(@url+path, params, :Authorization => @auth))      
end

#update_tracker(opts = {}) ⇒ Object



230
231
232
233
234
# File 'lib/callrail.rb', line 230

def update_tracker(opts={})
  path = "/" + @account_id + "/trackers/" + opts[:tracker_id].to_s + ".json"
  params = set_params(opts)
  return parse_json(RestClient.put(@url+path, params, :Authorization => @auth))  
end

#update_user(opts = {}) ⇒ Object



185
186
187
188
189
# File 'lib/callrail.rb', line 185

def update_user(opts = {})
  params = set_params(opts) 
  path =  "/" + @account_id + "/users/" + opts[:user_id].to_s + ".json"
  return parse_json(RestClient.put(@url+path, params, :Authorization => @auth))      
end