Class: ApiWrapperFor8x8::Connection
- Inherits:
-
Object
- Object
- ApiWrapperFor8x8::Connection
show all
- Includes:
- Agents, Channel, HTTParty
- Defined in:
- lib/ApiWrapperFor8x8/connection.rb
Constant Summary
collapse
- RECORDS_LIMIT =
50
- MAX_TRY =
3
- VALID_SEGMENT =
['channels', 'agents', 'statistics']
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Agents
#agent_list, #agents_detail, #filtered_agents
Methods included from Agent
#agent_detail
Methods included from Channel
#channel_details, #channel_list, #channel_sum_x
Constructor Details
#initialize(creds = {}) ⇒ Connection
Returns a new instance of Connection.
15
16
17
18
19
20
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 15
def initialize(creds={})
@configuration = {}
ApiWrapperFor8x8::Connection.api_token_keys.each do |key|
@configuration[key] = creds[key].to_s
end
end
|
Class Method Details
.api_token_keys ⇒ Object
32
33
34
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 32
def self.api_token_keys
[:username, :password].freeze
end
|
Instance Method Details
#api_token_keys_valid? ⇒ Boolean
#apply_filter(list, filtered_options) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 84
def apply_filter(list, filtered_options)
if filtered_options.size == 0
return list
end
list.select do |ele|
flag = true
filtered_options.each do |key, value|
flag = false unless (ele[key] && ele[key] == value)
end
flag
end
end
|
#get(url, params = {}, filtered_opts = {}) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 36
def get(url, params={}, filtered_opts={})
offset = params[:n] || 1
params[:n] = offset unless params[:n]
unless params.empty?
url = "#{url}?#{serialize_param(params)}"
end
tries = 1
resp = []
begin
resp_tmp = get_stat(request(:get, url, {}), url)
resp.concat(apply_filter(resp_tmp, filtered_opts)) if resp_tmp
tries += 1
offset += RECORDS_LIMIT
url.gsub!(/n=[0-9]*/, "n=#{offset}")
end while (size_of(resp_tmp) >= RECORDS_LIMIT && tries <= MAX_TRY)
resp
end
|
#get_stat(resp, url) ⇒ Object
77
78
79
80
81
82
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 77
def get_stat(resp, url)
if root_name = validate_and_extract_url(url)
return [] if (resp && resp.size == 0)
return resp[root_name][root_name[0...-1]]
end
end
|
#request(method, url, options = {}) ⇒ Object
22
23
24
25
26
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 22
def request(method, url, options={})
raise "Please set usranme and password" unless api_token_keys_valid?
options[:basic_auth] = @configuration
self.class.__send__(method, url, options).parsed_response
end
|
#serialize_param(params) ⇒ Object
58
59
60
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 58
def serialize_param(params)
params.sort.map {|key, value| URI.escape("#{key}=#{value}")}.join('&')
end
|
#size_of(details) ⇒ Object
62
63
64
65
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 62
def size_of(details)
details ||= []
details.size
end
|
#validate_and_extract_url(url) ⇒ Object
67
68
69
70
71
72
73
74
75
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 67
def validate_and_extract_url(url)
uri = URI(url)
last_seg = uri.path.split('/').last
if last_seg =~ /(#{VALID_SEGMENT.join('|')})\.json$/
return last_seg.split('.').first
else
raise ApiWrapperFor8x8::ResponseError.new(nil, "URL path is incorrect! please double check your url.")
end
end
|