Class: Konzertmeister::Session
- Inherits:
-
Object
- Object
- Konzertmeister::Session
- Defined in:
- lib/konzertmeister/session.rb
Instance Method Summary collapse
- #_request_params(extra = {}) ⇒ Object
- #_url(path = nil) ⇒ Object
- #get(path, headers = {}, &block) ⇒ Object
-
#initialize(hostname: 'konzertmeister', port: 443, ssl_client_cert: nil, ssl_client_key: nil) ⇒ Session
constructor
A new instance of Session.
- #post(path, params, headers = {}, &block) ⇒ Object
- #put(path, params, headers = {}, &block) ⇒ Object
Constructor Details
#initialize(hostname: 'konzertmeister', port: 443, ssl_client_cert: nil, ssl_client_key: nil) ⇒ Session
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/konzertmeister/session.rb', line 6 def initialize( hostname: 'konzertmeister', port: 443, ssl_client_cert: nil, ssl_client_key: nil ) @ssl_client_cert = OpenSSL::X509::Certificate.new(File.read(ssl_client_cert)) unless ssl_client_cert.nil? @ssl_client_key = OpenSSL::PKey::RSA.new(File.read(ssl_client_key)) unless ssl_client_key.nil? @hostname = hostname @port = port end |
Instance Method Details
#_request_params(extra = {}) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/konzertmeister/session.rb', line 86 def _request_params(extra = {}) base = {} base[:ssl_client_cert] = @ssl_client_cert if @ssl_client_cert base[:ssl_client_key] = @ssl_client_key if @ssl_client_key base.merge(extra) end |
#_url(path = nil) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/konzertmeister/session.rb', line 94 def _url(path = nil) scheme = (@port == 443 ? "https" : "http") [ "#{scheme}://#{@hostname}:#{@port}", path ].compact.join("") end |
#get(path, headers = {}, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/konzertmeister/session.rb', line 18 def get(path, headers = {}, &block) url = _url(path) params = _request_params( :method => :get, :url => url, :headers => headers, :timeout => nil ) begin json = RestClient::Request.execute(params, &block) JSON.parse(json) rescue => e puts "ERROR" p e false end end |
#post(path, params, headers = {}, &block) ⇒ 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 |
# File 'lib/konzertmeister/session.rb', line 36 def post(path, params, headers = {}, &block) json = JSON.generate(params) url = _url(path) request_params = _request_params({ :method => :post, :url => url, :payload => json, :headers => headers, :timeout => nil }) begin response = RestClient::Request.execute(request_params, &block) if response.empty? {} else JSON.parse(response) end rescue RestClient::Exception => e puts puts "ERROR making request to #{path}:" p e false end end |
#put(path, params, headers = {}, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/konzertmeister/session.rb', line 61 def put(path, params, headers = {}, &block) json = JSON.generate(params) url = _url(path) request_params = _request_params({ :method => :put, :url => url, :payload => json, :headers => headers, :timeout => nil }) begin response = RestClient::Request.execute(request_params, &block) if response.empty? {} else JSON.parse(response) end rescue RestClient::Exception => e puts puts "ERROR making request to #{path}:" p e false end end |