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'
VOYS_DATE_FORMAT =
"%m-%d-%Y"

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# File 'lib/voys_api/client.rb', line 13

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")


123
124
125
126
127
128
129
# File 'lib/voys_api/client.rb', line 123

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/voys_api/client.rb', line 108

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) if not File.exists?(File.join(recording_path, filename))
    return filename
  end
end

#html_export(options = {}) ⇒ Object



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
106
# File 'lib/voys_api/client.rb', line 57

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"
        if not File.exists?(recording_filename)
          FileUtils.mkdir_p(File.dirname(recording_filename))
          get_recording(recording, recording_filename)
        end
      end

      results << result
    end
    page_number += 1
  end until page.at('.pagination a.next').nil?
  return results
end

#logged_in?Boolean

Returns:

  • (Boolean)


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

def logged_in?
  @logged_in || false
end

#loginObject



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

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



131
132
133
# File 'lib/voys_api/client.rb', line 131

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.



48
49
50
51
52
53
54
55
# File 'lib/voys_api/client.rb', line 48

def raw_export(options = {})
   if not logged_in?

  options = convert_options(options)

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