Class: Piwigo::Session
- Inherits:
-
Object
- Object
- Piwigo::Session
- Defined in:
- lib/piwigo/session.rb
Overview
Class to hold the Piwigo session the rest of the API needs to pass in order to access the API
Instance Attribute Summary collapse
-
#id ⇒ String
Piwigo session cookie.
-
#pwg_token ⇒ String
Token required for admin methods.
-
#uri ⇒ URI::HTTP
Piwigo host associated with this session.
-
#username ⇒ String
Token required for admin methods.
Class Method Summary collapse
-
.login(host, username, password, https: true, port: nil, logger: nil) ⇒ Object
Log into the piwigo API and grab the session id for subsequent calls.
Instance Method Summary collapse
-
#initialize(id, uri) ⇒ Session
constructor
A new instance of Session.
-
#logout(logger: nil) ⇒ Object
Logout of the current session.
-
#status ⇒ <Type>
Gets information about the current session.
Constructor Details
#initialize(id, uri) ⇒ Session
Returns a new instance of Session.
21 22 23 24 25 26 |
# File 'lib/piwigo/session.rb', line 21 def initialize(id, uri) self.id = id self.uri = uri status end |
Instance Attribute Details
#id ⇒ String
Returns Piwigo session cookie.
10 11 12 |
# File 'lib/piwigo/session.rb', line 10 def id @id end |
#pwg_token ⇒ String
Returns token required for admin methods.
16 17 18 |
# File 'lib/piwigo/session.rb', line 16 def pwg_token @pwg_token end |
#uri ⇒ URI::HTTP
Returns Piwigo host associated with this session.
13 14 15 |
# File 'lib/piwigo/session.rb', line 13 def uri @uri end |
#username ⇒ String
Returns token required for admin methods.
19 20 21 |
# File 'lib/piwigo/session.rb', line 19 def username @username end |
Class Method Details
.login(host, username, password, https: true, port: nil, logger: nil) ⇒ Object
Log into the piwigo API and grab the session id for subsequent calls.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/piwigo/session.rb', line 84 def self.login(host, username, password, https: true, port: nil, logger: nil) raise 'host should not be nil' if host.nil? raise 'username should not be nil' if username.nil? logger ||= Logger.new($stdout) begin uri = if https URI::HTTPS.build(host: host, port: port.nil? ? 443 : port, path: '/ws.php', query: 'format=json') else URI::HTTP.build(host: host, port: port.nil? ? 80 : port, path: '/ws.php', query: 'format=json') end # Create the HTTP objects http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri) form = { method: 'pwg.session.login', username: username, password: password } request.set_form_data(form) # Send the request response = http.request(request) if response.code == '200' logger.info "Login succeeded: #{response.body}" pwg_id = response.response['set-cookie'].split(/[;,\,]/).select { |i| i.strip.start_with? 'pwg_id' }.first return Session.new(pwg_id, uri) else logger.error "Login failed: #{response.body}" end rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e logger.error "Login failed: #{e.messages}" end nil end |
Instance Method Details
#logout(logger: nil) ⇒ Object
Logout of the current session
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/piwigo/session.rb', line 60 def logout(logger: nil) raise 'This session has already been logged out' if uri.nil? logger ||= Logger.new($stdout) # Create the HTTP objects http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new("#{uri.request_uri}&method=pwg.session.logout") request['Cookie'] = id # Send the request response = http.request(request) logger.info "Logout succeeded: #{response.body}" if response.code == '200' self.id = nil self.uri = nil end |
#status ⇒ <Type>
Gets information about the current session. Also provides a token useable with admin methods.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/piwigo/session.rb', line 30 def status logger ||= Logger.new($stdout) begin # Create the HTTP objects http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri) form = { method: 'pwg.session.getStatus' } request.set_form_data(form) request['Cookie'] = id # Send the request response = http.request(request) if response.code == '200' logger.info 'Status succeeded' data = JSON.parse(response.body) self.pwg_token = data['result']['pwg_token'] self.username = data['result']['username'] end rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e logger.error "Unexpected failed: #{e.messages}" end nil end |