Class: VoysApi::Client
- Inherits:
-
Object
- Object
- VoysApi::Client
- Defined in:
- lib/voys_api/client.rb
Overview
VoysApi exports voys.nl call list..
Constant Summary collapse
- VOYS_HOST =
'mijn.voys.nl'
Instance Method Summary collapse
-
#export(options = {}, csv_options = {}) ⇒ Object
Returns CSV::Table of calls.
- #get_recording(recording_path, filename = nil) ⇒ Object
- #html_export(options = {}) ⇒ Object
-
#initialize(username, password) ⇒ Client
constructor
A new instance of Client.
- #logged_in? ⇒ Boolean
- #login ⇒ Object
- #logout ⇒ Object
-
#raw_export(options = {}) ⇒ Object
Options: period_from: ‘2013-01-01’ (you can also pass a Time object) period_to: ‘2013-01-18’ (you can also pass a Time object) inboundoutbound: 0 totals: 0 aggregation: 0 search_query: nil reset_filter: false page_number: nil.
Constructor Details
#initialize(username, password) ⇒ Client
Returns a new instance of Client.
12 13 14 15 |
# File 'lib/voys_api/client.rb', line 12 def initialize(username, password) @username = username @password = password end |
Instance Method Details
#export(options = {}, csv_options = {}) ⇒ Object
Returns CSV::Table of calls. NOTE:
Date and time values are in +0200 timezone but set to UTC
To fix use row[:date].change(:offset => "+0200")
120 121 122 123 124 125 126 |
# File 'lib/voys_api/client.rb', line 120 def export( = {}, = {}) = {col_sep: ';', converters: [:date_time], headers: :first_row, header_converters: :symbol}.merge() export = CSV.parse(raw_export(), ) return export rescue CSV::MalformedCSVError => exception raise exception, "#{exception.}\nCSV:\n#{raw_export}" end |
#get_recording(recording_path, filename = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/voys_api/client.rb', line 105 def get_recording(recording_path, filename = nil) login if not logged_in? if recording_path =~ /(\d+)\/?$/ recording_id = $1 filename ||= "#{recording_id}.wav" agent.get(recording_path).save(filename) return filename end end |
#html_export(options = {}) ⇒ Object
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 |
# File 'lib/voys_api/client.rb', line 56 def html_export( = {}) login if not logged_in? = { period_from: nil, period_to: nil, inboundoutbound: 0, totals: 0, aggregation: 0, recordings: 0, page: 1 }.merge() = () results = [] page_number = 1 begin [:page] = page_number puts "Page #{page_number}" page = agent.get("/cdr?#{.to_param}") rows = page.search('table tbody tr') rows.each do |row| cols = row.search('td') result = {} result[:date] = cols[2].inner_text.strip result[:inbound__outbound] = cols[3].inner_text.strip result[:duration] = cols[5].inner_text source = result[:source] = cols[6].inner_text.strip destination = result[:destination] = cols[7].inner_text.strip recording = result[:recording] = cols[9].at('.jp-embedded').try(:[], 'data-source-wav') puts result.inspect # Download all recordings if recording time = Time.parse(result[:date]) recording_filename = "recordings/#{time.strftime("%Y%m%d_%H%M")}-#{source}-#{destination}.wav" FileUtils.mkdir_p(File.dirname(recording_filename)) get_recording(recording, recording_filename) end results << result end page_number += 1 end until page.at('.pagination a.next').nil? return results end |
#logged_in? ⇒ Boolean
17 18 19 |
# File 'lib/voys_api/client.rb', line 17 def logged_in? @logged_in || false end |
#login ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/voys_api/client.rb', line 21 def login return true if @logged_in page = agent.get("https://#{VOYS_HOST}/user/login/") login_form = page.form login_form.fields.detect {|field| field.name == 'this_is_the_login_form'} || raise(VoysApi::AuthenticationError, "Could not find the login form!") login_form.field_with(:name => "username").value = @username login_form.field_with(:name => "password").value = @password login_result = agent.submit login_form if (login_result.form && login_result.form.fields.detect {|field| field.name == 'this_is_the_login_form'}) # We're still on the login page! raise(VoysApi::AuthenticationError, "Error logging in!") end @logged_in = true end |
#logout ⇒ Object
128 129 130 |
# File 'lib/voys_api/client.rb', line 128 def logout agent.get("https://#{VOYS_HOST}/user/logout/") end |
#raw_export(options = {}) ⇒ Object
Options:
period_from: '2013-01-01' (you can also pass a Time object)
period_to: '2013-01-18' (you can also pass a Time object)
inboundoutbound: 0
totals: 0
aggregation: 0
search_query: nil
reset_filter: false
page_number: nil
Empty options returns everything.
47 48 49 50 51 52 53 54 |
# File 'lib/voys_api/client.rb', line 47 def raw_export( = {}) login if not logged_in? = () result = agent.post('/cdr/export', ) result.body end |