1
+ # "THE BEER-WARE LICENSE"
2
+ # As long as you retain this notice you can do whatever you want with this
3
+ # stuff. If you meet an employee from Windward some day, and you think this
4
+ # stuff is worth it, you can buy them a beer in return. Windward Studios
5
+
6
+ require "uri"
7
+ require "net/http"
8
+ require "rexml/document"
9
+ require "base64"
10
+ #require 'nokogiri'
11
+ require_relative 'Client'
12
+ require_relative 'Version'
13
+ require 'json/pure'
14
+ include REXML
15
+
16
+ class Report
17
+ @baseUri
18
+ @template
19
+ @report
20
+ @guid
21
+
22
+ attr_accessor :Description
23
+ attr_accessor :Title
24
+ attr_accessor :Subject
25
+ attr_accessor :Keywords
26
+ attr_accessor :Locale
27
+ attr_accessor :Timeout
28
+
29
+ attr_accessor :Hyphenate
30
+ attr_accessor :TrackImports
31
+ attr_accessor :RemoveUnusedFormats
32
+ attr_accessor :CopyMetadata
33
+
34
+ attr_reader :OutputFormat
35
+
36
+ def initialize ( baseUri , template , report = nil )
37
+ ctor ( baseUri )
38
+ @template = template
39
+ @report = report
40
+ end
41
+ def ctor ( baseUri )
42
+ url = baseUri . to_s
43
+ if !url . end_with? "/"
44
+ url . concat ( "/" )
45
+ end
46
+ @baseUri = URI ( url )
47
+ @Timeout = 0
48
+ @Hyphenate = :template
49
+ @TrackImports = false
50
+ @RemoveUnusedFormats = true
51
+ @CopyMetadata = :nodatasource
52
+ end
53
+
54
+ def self . GetVersion ( baseUri )
55
+ uri = URI . join ( baseUri , "v1/version" )
56
+ response = Client . get ( uri )
57
+ status = response . message
58
+ body = response . body
59
+ p_body = JSON . parse ( body )
60
+ if ( status == 'OK' )
61
+ ev = p_body [ "EngineVersion" ] . to_s
62
+ sv = p_body [ "ServiceVersion" ] . to_s
63
+ v = Version . new ( sv , ev )
64
+ else
65
+ return nil
66
+ end
67
+ return v
68
+ end
69
+
70
+ def ProcessDataSources ( dataSources )
71
+ xml = CreateXmlDocument ( )
72
+ ApplyDataSources xml , dataSources
73
+ ProcessXml xml
74
+ end
75
+
76
+ def Process ( )
77
+ xml = CreateXmlDocument ( )
78
+ ProcessXml xml
79
+ end
80
+
81
+ def ProcessXml ( xml )
82
+ SetReportOption ( xml , "Description" , @Description )
83
+ SetReportOption ( xml , "Title" , @Title )
84
+ SetReportOption ( xml , "Subject" , @Subject )
85
+ SetReportOption ( xml , "Keywords" , @Keywords )
86
+ SetReportOption ( xml , "Description" , @Description )
87
+ SetReportOption ( xml , "Locale" , @Locale )
88
+
89
+ root = xml . root
90
+ e = root . add_element "Timeout"
91
+ e . add_text @Timeout . to_s
92
+
93
+ e = root . add_element "Hyphenate"
94
+ e . add_text @Hyphenate . to_s
95
+
96
+ if ( @TrackImports )
97
+ _track_imports = "true"
98
+ else
99
+ _track_imports = "false"
100
+ end
101
+
102
+ e = root . add_element "TrackImports"
103
+ e . add_text _track_imports
104
+
105
+ if ( @RemoveUnusedFormats )
106
+ _remove_form = "true"
107
+ else
108
+ _remove_form = "false"
109
+ end
110
+
111
+ e = root . add_element "RemoveUnusedFormats"
112
+ e . add_text _remove_form
113
+
114
+ e = root . add_element "CopyMetadata"
115
+ e . add_text @CopyMetadata . to_s
116
+
117
+ if ( @report == nil )
118
+ e = root . add_element "Async"
119
+ e . add_text "true"
120
+ end
121
+ result = Client . post ( URI . join ( @baseUri , "v1/reports" ) , xml )
122
+ status = result . message
123
+ body = result . body
124
+ if ( status == 'OK' )
125
+ if ( @report != nil )
126
+ ReadReport ( body )
127
+ else
128
+ ReadGuid ( body )
129
+ end
130
+ else
131
+ raise body . to_s
132
+ end
133
+ end
134
+
135
+ def GetReport
136
+ uri = URI . join ( @baseUri , "v1/reports" )
137
+ uri = URI . join ( uri , @guid )
138
+
139
+ result = Client . Get ( uri )
140
+ status = result . message
141
+ body = result . body
142
+ if ( status == 'OK' )
143
+ return Base64 . decode64 ( body . root . get_elements ( "Data" ) [ 0 ] . get_text . value )
144
+ end
145
+ return null
146
+ end
147
+
148
+ def Delete
149
+ uri = URI . join ( @baseUri , v1 /reports )
150
+ uri = URI . join ( uri , @guid )
151
+ result = Client . Delete ( uri )
152
+ end
153
+
154
+ def GetStatus
155
+ uri = URI . join ( @baseUri , "v1/reports/" )
156
+ uri = URI . join ( uri , @guid )
157
+ uri = URI . join ( uri , "/status" )
158
+ result = Client . get ( uri )
159
+ status = result . code
160
+
161
+ case status
162
+ when 200
163
+ return :Ready
164
+ when 202
165
+ return :Working
166
+ when 500
167
+ return :Error
168
+ else
169
+ return :NotFound
170
+ end
171
+ end
172
+
173
+ def ReadReport result
174
+ p_result = JSON . parse ( result )
175
+ data = Base64 . decode64 ( p_result [ "Data" ] )
176
+ @report . write ( data )
177
+ #File.open(@report, 'wb') do |output|
178
+ # output.puts data
179
+ #end
180
+ end
181
+
182
+ def ReadGuid body
183
+ p_body = JSON . parse ( body )
184
+ @guid = body [ "Data" ] [ 0 ]
185
+ end
186
+
187
+ def SetReportOption xml , name , option
188
+ if ( option != nil )
189
+ e = xml . root . add_element name
190
+ e . add_text option . to_s
191
+ end
192
+ end
193
+
194
+ def CreateXmlDocument
195
+ bytes = File . binread ( @template )
196
+ encodedTemplate = Base64 . encode64 ( bytes )
197
+ xml = Document . new
198
+ e = xml . add_element "Template"
199
+ e1 = e . add_element "Data"
200
+ e1 . add_text encodedTemplate
201
+ e1 = e . add_element "OutputFormat"
202
+ e1 . add_text @OutputFormat . to_s
203
+ return xml
204
+ end
205
+
206
+ def ApplyDataSources xml , dataSources
207
+ if ( dataSources . length > 0 )
208
+ xmlDatasources = xml . root . add_element "Datasources"
209
+ dataSources . each do |key , value |
210
+ e = xmlDatasources . add_element ( value . GetXml ( key ) . root )
211
+ end
212
+ end
213
+ end
214
+ end
0 commit comments