Class: ApiWrapperFor8x8::Connection
- Inherits:
-
Object
- Object
- ApiWrapperFor8x8::Connection
show all
- Includes:
- Agents, Channel, Stats, HTTParty
- Defined in:
- lib/ApiWrapperFor8x8/connection.rb
Constant Summary
collapse
- RECORDS_LIMIT =
50
- MAX_TRY =
3
- VALID_SEGMENT =
['channels', 'agents', 'statistics']
Constants included
from Stats
Stats::DEFAULT_STAT_ATTR
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Stats
#beginning_of_hour, #end_of_hour, #stats_per_hour
Methods included from Agents
#agent_list, #agents_detail
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.
16
17
18
19
20
21
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 16
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
35
36
37
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 35
def self.api_token_keys
[:username, :password].freeze
end
|
Instance Method Details
#api_token_keys_valid? ⇒ Boolean
#apply_filter(list, filtered_options) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 87
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 39
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
80
81
82
83
84
85
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 80
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
|
#parsed_response(response) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 100
def parsed_response(response)
if response.is_a? Net::HTTPResponse
unless response.is_a? Net::HTTPSuccess
raise ApiWrapperFor8x8::ResponseError.new(response)
end
JSON.parse(response.body)
else
unless response.success?
raise ApiWrapperFor8x8::ResponseError.new(response)
end
response.parsed_response
end
end
|
#request(method, url, options = {}) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 23
def request(method, url, options={})
unless api_token_keys_valid?
raise ApiWrapperFor8x8::ResponseError.new(nil, "Please set username and password correctly")
end
options[:basic_auth] = @configuration
parsed_response(self.class.__send__(method, url, options))
end
|
#serialize_param(params) ⇒ Object
61
62
63
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 61
def serialize_param(params)
params.sort.map {|key, value| URI.escape("#{key}=#{value}")}.join('&')
end
|
#size_of(details) ⇒ Object
65
66
67
68
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 65
def size_of(details)
details ||= []
details.size
end
|
#validate_and_extract_url(url) ⇒ Object
70
71
72
73
74
75
76
77
78
|
# File 'lib/ApiWrapperFor8x8/connection.rb', line 70
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
|