Class: Kerio::Api::Session
- Inherits:
-
Object
- Object
- Kerio::Api::Session
- Defined in:
- lib/kerio-api/session.rb
Instance Attribute Summary collapse
-
#token ⇒ Object
writeonly
Sets the attribute token.
Instance Method Summary collapse
- #download_file(path) ⇒ Object
- #headers ⇒ Object
-
#initialize(url, verify_ssl, debug) ⇒ Session
constructor
A new instance of Session.
- #json_method(name, params) ⇒ Object
- #process_json_response(resp) ⇒ Object
- #upload_file(file) ⇒ Object
Constructor Details
#initialize(url, verify_ssl, debug) ⇒ Session
Returns a new instance of Session.
13 14 15 16 17 18 19 20 |
# File 'lib/kerio-api/session.rb', line 13 def initialize(url, verify_ssl, debug) @url = url @verify_ssl = verify_ssl @debug = debug @token = nil @cookie = nil end |
Instance Attribute Details
#token=(value) ⇒ Object (writeonly)
Sets the attribute token
11 12 13 |
# File 'lib/kerio-api/session.rb', line 11 def token=(value) @token = value end |
Instance Method Details
#download_file(path) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/kerio-api/session.rb', line 84 def download_file(path) h = headers h['Accept'] = '*/*' u = URI.parse("#{@url.scheme}://#{@url.host}:#{@url.port}#{path}") resp = HTTParty.get( u.to_s, headers: h, verify: @verify_ssl, stream_body: true, follow_redirects: true, ) do |fragment| yield fragment end raise Kerio::Api::Error.new(resp) if resp.code != 200 return {"result" => {"code" => resp.code}} end |
#headers ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/kerio-api/session.rb', line 22 def headers headers = {} headers['X-Token'] = @token if not @token.nil? headers['Cookie'] = @cookie if not @cookie.nil? headers end |
#json_method(name, params) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/kerio-api/session.rb', line 36 def json_method(name, params) h = headers h['Accept'] = 'application/json-rpc' body = { jsonrpc: '2.0', id: Kernel.rand(10**12), method: name, params: params, } body['token'] = @token if not @token.nil? PP.pp body if @debug resp = HTTMultiParty.post( @url.to_s, body: JSON.generate(body), headers: h, verify: @verify_ssl, follow_redirects: true, ) PP.pp resp if @debug process_json_response(resp) return resp end |
#process_json_response(resp) ⇒ Object
30 31 32 33 34 |
# File 'lib/kerio-api/session.rb', line 30 def process_json_response(resp) @cookie = resp.headers['Set-Cookie'] if not resp.headers['Set-Cookie'].nil? raise Kerio::Api::Error.new(resp) if ((not resp["error"].nil?) || (resp.code != 200)) end |
#upload_file(file) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/kerio-api/session.rb', line 65 def upload_file(file) h = headers h['Accept'] = '*/*' h['Content-Type'] = 'multipart/form-data'; resp = HTTMultiParty.post( @url.to_s + '/upload', headers: h, verify: @verify_ssl, query: { 'newFile.bin' => file, }, follow_redirects: true, ) process_json_response(resp) return resp end |