voys_api 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -47,3 +47,5 @@ For example:
47
47
  3. Commit your changes (`git commit -am 'Add some feature'`)
48
48
  4. Push to the branch (`git push origin my-new-feature`)
49
49
  5. Create new Pull Request
50
+
51
+ (C)opyright Joopp BV - www.joopp.com
@@ -1,8 +1,14 @@
1
1
  require 'csv'
2
2
  require 'mechanize'
3
+ require 'fileutils'
4
+ require 'active_support/core_ext/object/to_query'
5
+ require 'active_support/core_ext/object/to_param'
6
+ require 'active_support/core_ext/object/try'
3
7
  # VoysApi exports voys.nl call list..
4
8
  class VoysApi::Client
5
9
 
10
+ VOYS_HOST = 'mijn.voys.nl'
11
+
6
12
  def initialize(username, password)
7
13
  @username = username
8
14
  @password = password
@@ -14,7 +20,7 @@ class VoysApi::Client
14
20
 
15
21
  def login
16
22
  return true if @logged_in
17
- page = agent.get('https://client.voys.nl/user/login/')
23
+ page = agent.get("https://#{VOYS_HOST}/user/login/")
18
24
  login_form = page.form
19
25
  login_form.fields.detect {|field| field.name == 'this_is_the_login_form'} || raise(VoysApi::AuthenticationError, "Could not find the login form!")
20
26
  login_form.field_with(:name => "username").value = @username
@@ -41,19 +47,77 @@ class VoysApi::Client
41
47
  def raw_export(options = {})
42
48
  login if not logged_in?
43
49
 
44
- # convert options
45
- options[:period_from] = options[:period_from].strftime("%Y-%m-%d") if options[:period_from].is_a?(Time)
46
- options[:period_to] = options[:period_to].strftime("%Y-%m-%d") if options[:period_to].is_a?(Time)
50
+ options = covert_options(options)
47
51
 
48
52
  result = agent.post('/cdr/export', options)
49
53
  result.body
50
54
  end
51
55
 
56
+ def html_export(options = {})
57
+ login if not logged_in?
58
+
59
+ options = {
60
+ period_from: nil,
61
+ period_to: nil,
62
+ inboundoutbound: 0,
63
+ totals: 0,
64
+ aggregation: 0,
65
+ recordings: 0,
66
+ page: 1
67
+ }.merge(options)
68
+ options = convert_options(options)
69
+
70
+ results = []
71
+ page_number = 1
72
+ begin
73
+ options[:page] = page_number
74
+ puts "Page #{page_number}"
75
+
76
+ page = agent.get("/cdr?#{options.to_param}")
77
+ rows = page.search('table tbody tr')
78
+ rows.each do |row|
79
+ cols = row.search('td')
80
+
81
+ result = {}
82
+ result[:date] = cols[2].inner_text.strip
83
+ result[:inbound__outbound] = cols[3].inner_text.strip
84
+ result[:duration] = cols[5].inner_text
85
+ source = result[:source] = cols[6].inner_text.strip
86
+ destination = result[:destination] = cols[7].inner_text.strip
87
+ recording = result[:recording] = cols[9].at('.jp-embedded').try(:[], 'data-source-wav')
88
+ puts result.inspect
89
+
90
+ # Download all recordings
91
+ if recording
92
+ time = Time.parse(result[:date])
93
+ recording_filename = "recordings/#{time.strftime("%Y%m%d_%H%M")}-#{source}-#{destination}.wav"
94
+ FileUtils.mkdir_p(File.dirname(recording_filename))
95
+ get_recording(recording, recording_filename)
96
+ end
97
+
98
+ results << result
99
+ end
100
+ page_number += 1
101
+ end until page.at('.pagination a.next').nil?
102
+ return results
103
+ end
104
+
105
+ def get_recording(recording_path, filename = nil)
106
+ login if not logged_in?
107
+
108
+ if recording_path =~ /(\d+)\/?$/
109
+ recording_id = $1
110
+ filename ||= "#{recording_id}.wav"
111
+ agent.get(recording_path).save(filename)
112
+ return filename
113
+ end
114
+ end
115
+
116
+ # Returns CSV::Table of calls.
52
117
  # NOTE:
53
118
  # Date and time values are in +0200 timezone but set to UTC
54
119
  # To fix use row[:date].change(:offset => "+0200")
55
120
  def export(options = {}, csv_options = {})
56
-
57
121
  csv_options = {col_sep: ';', converters: [:date_time], headers: :first_row, header_converters: :symbol}.merge(csv_options)
58
122
  export = CSV.parse(raw_export(options), csv_options)
59
123
  return export
@@ -62,13 +126,26 @@ class VoysApi::Client
62
126
  end
63
127
 
64
128
  def logout
65
- agent.get('https://client.voys.nl/user/logout/')
129
+ agent.get("https://#{VOYS_HOST}/user/logout/")
66
130
  end
67
131
 
68
132
  private
69
133
 
134
+ def convert_options(options)
135
+ converted_options = options.clone # deep clone?
136
+
137
+ # convert options
138
+ converted_options[:period_from] = options[:period_from].strftime("%Y-%m-%d") if not options[:period_from].is_a?(String)
139
+ converted_options[:period_to] = options[:period_to].strftime("%Y-%m-%d") if not options[:period_from].is_a?(String)
140
+
141
+ return converted_options
142
+ end
143
+
70
144
  def agent
71
- @agent ||= Mechanize.new
145
+ return @agent if not @agent.nil?
146
+ @agent = Mechanize.new
147
+ @agent.pluggable_parser.default = Mechanize::Download
148
+ return @agent
72
149
  end
73
150
 
74
151
  end
@@ -1,3 +1,3 @@
1
1
  module VoysApi
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/voys_api.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
 
24
24
  spec.add_dependency 'mechanize'
25
+ spec.add_dependency 'active_support'
25
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voys_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-26 00:00:00.000000000 Z
12
+ date: 2014-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: active_support
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  description: Export calls from http://www.voys.nl.
63
79
  email:
64
80
  - joost@joopp.com
@@ -97,9 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
113
  version: '0'
98
114
  requirements: []
99
115
  rubyforge_project:
100
- rubygems_version: 1.8.25
116
+ rubygems_version: 1.8.23
101
117
  signing_key:
102
118
  specification_version: 3
103
119
  summary: Export calls from http://www.voys.nl.
104
120
  test_files: []
105
- has_rdoc: