class Purest::Rest
Base class for interacting with PURE storage REST API
Public Class Methods
access_method?(meth_id)
click to toggle source
# File lib/purest/rest.rb, line 55 def access_method?(meth_id) @access_methods.include? meth_id end
method_missing(meth_id, *args)
click to toggle source
Calls superclass method
# File lib/purest/rest.rb, line 59 def method_missing(meth_id, *args) if access_method?(meth_id) new.send(meth_id, *args) else # See https://bugs.ruby-lang.org/issues/10969 begin super rescue NameError => err raise NoMethodError, err end end end
new()
click to toggle source
Initialize the fadaray connection, create session unless one exists
# File lib/purest/rest.rb, line 9 def initialize establish_connection create_session unless authenticated? end
Public Instance Methods
authenticated?()
click to toggle source
Check if session exists, and whether or not it's expired
# File lib/purest/rest.rb, line 15 def authenticated? if defined? @session_expire Time.now.utc < @session_expire else false end end
concat_url(parts)
click to toggle source
Assemble a url from an array of parts. @param parts [Array] the url chunks to be assembled @return [String] the resultant url string
# File lib/purest/rest.rb, line 26 def concat_url(parts) if parts.length > 1 parts.first + '?' + parts.drop(1).join('&') else parts.first end end
logout()
click to toggle source
Logout current session
# File lib/purest/rest.rb, line 47 def logout raw_resp = @conn.delete do |req| req.url "/api/#{Purest.configuration.api_version}/auth/session" end remove_instance_variable(:@session_expire) end
use_named_parameter(name, value)
click to toggle source
Format url parameters into strings correctly @param name [String] the name of the parameter @param value [String] the value of the parameter @return [Array] the resultant parameter string inside an array.
# File lib/purest/rest.rb, line 38 def use_named_parameter(name, value) if value.is_a? Array ["#{name}=#{value.join(',')}"] else value ? ["#{name}=#{value}"] : [] end end
Private Instance Methods
build_connection()
click to toggle source
# File lib/purest/rest.rb, line 108 def build_connection Faraday.new(Purest.configuration.url, Purest.configuration.options) do |faraday| faraday.request :json faraday.use :cookie_jar faraday.adapter Faraday.default_adapter end end
create_session()
click to toggle source
# File lib/purest/rest.rb, line 88 def create_session token = Purest.configuration.api_key || get_token raw_resp = @conn.post do |req| req.url "/api/#{Purest.configuration.api_version}/auth/session" req.params = { "api_token": token } end # Man they really tuck that away, don't they? @session_expire = Time.parse(raw_resp.env.response_headers['set-cookie'].split(';')[1].split(',')[1].strip).utc raw_resp end
establish_connection()
click to toggle source
Build the API Client
# File lib/purest/rest.rb, line 104 def establish_connection @conn = build_connection end
get_token()
click to toggle source
# File lib/purest/rest.rb, line 76 def get_token raw_resp = @conn.post do |req| req.url "/api/#{Purest.configuration.api_version}/auth/apitoken" req.params = { "username": Purest.configuration.username.to_s, "password": Purest.configuration.password.to_s } end JSON.parse(raw_resp.body)['api_token'] end