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
# File 'lib/callrail.rb', line 12

def initialize(opts = {})
  @url = 'https://api.callrail.com/v2/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



138
139
140
141
142
143
# File 'lib/callrail.rb', line 138

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



206
207
208
209
210
# File 'lib/callrail.rb', line 206

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



182
183
184
185
186
# File 'lib/callrail.rb', line 182

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



163
164
165
166
167
# File 'lib/callrail.rb', line 163

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



151
152
153
154
# File 'lib/callrail.rb', line 151

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



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

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



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

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

#get_accounts(opts = {}) ⇒ Object

Account Calls



125
126
127
128
129
# File 'lib/callrail.rb', line 125

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_companies(opts = {}) ⇒ Object

Company Calls



132
133
134
135
136
# File 'lib/callrail.rb', line 132

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



200
201
202
203
204
# File 'lib/callrail.rb', line 200

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/callrail.rb', line 107

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 Calls



176
177
178
179
180
# File 'lib/callrail.rb', line 176

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 Calls



157
158
159
160
161
# File 'lib/callrail.rb', line 157

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



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

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



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

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

#set_params(opts = {}) ⇒ Object



27
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
# File 'lib/callrail.rb', line 27

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]
  #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
  #Text Message Params
    # Filtering: date_range
    # Searching: customer_phone_number, customer_name
  #pagination

  return params
end

#update_company(opts = {}) ⇒ Object



145
146
147
148
149
# File 'lib/callrail.rb', line 145

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



212
213
214
215
216
# File 'lib/callrail.rb', line 212

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



188
189
190
191
192
# File 'lib/callrail.rb', line 188

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

#update_user(opts = {}) ⇒ Object



169
170
171
172
173
# File 'lib/callrail.rb', line 169

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