Class: VoysApi::Client

Inherits:
Object
  • Object
show all
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

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(options = {}, csv_options = {})
  csv_options = {col_sep: ';', converters: [:date_time], headers: :first_row, header_converters: :symbol}.merge(csv_options)
  export = CSV.parse(raw_export(options), csv_options)
  return export
rescue CSV::MalformedCSVError => exception
  raise exception, "#{exception.message}\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)
   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(options = {})
   if not logged_in?

  options = {
      period_from: nil,
      period_to: nil,
      inboundoutbound: 0,
      totals: 0,
      aggregation: 0,
      recordings: 0,
      page: 1
    }.merge(options)
  options = convert_options(options)

  results = []
  page_number = 1
  begin
    options[:page] = page_number
    puts "Page #{page_number}"

    page = agent.get("/cdr?#{options.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

Returns:

  • (Boolean)


17
18
19
# File 'lib/voys_api/client.rb', line 17

def logged_in?
  @logged_in || false
end

#loginObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/voys_api/client.rb', line 21

def 
  return true if @logged_in
  page = agent.get("https://#{VOYS_HOST}/user/login/")
   = page.form
  .fields.detect {|field| field.name == 'this_is_the_login_form'} || raise(VoysApi::AuthenticationError, "Could not find the login form!")
  .field_with(:name => "username").value = @username
  .field_with(:name => "password").value = @password
   = agent.submit 
  if (.form && .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

#logoutObject



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(options = {})
   if not logged_in?

  options = covert_options(options)

  result = agent.post('/cdr/export', options)
  result.body
end