Class: Sticapi::SticapiClient
- Inherits:
-
Object
- Object
- Sticapi::SticapiClient
- Includes:
- Singleton
- Defined in:
- lib/sticapi_client.rb
Constant Summary collapse
- TOKEN_CACHE_PATH =
"tmp/sticapi_token.yml"
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#client ⇒ Object
Returns the value of attribute client.
-
#expiry ⇒ Object
Returns the value of attribute expiry.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#uid ⇒ Object
Returns the value of attribute uid.
-
#urn ⇒ Object
Returns the value of attribute urn.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #expiry_now ⇒ Object
- #get_token ⇒ Object
-
#initialize ⇒ SticapiClient
constructor
A new instance of SticapiClient.
- #sign_out ⇒ Object
- #sticapi_request(route, options = {}) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize ⇒ SticapiClient
Returns a new instance of SticapiClient.
34 35 36 37 |
# File 'lib/sticapi_client.rb', line 34 def initialize load_config load_token end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
30 31 32 |
# File 'lib/sticapi_client.rb', line 30 def access_token @access_token end |
#client ⇒ Object
Returns the value of attribute client.
30 31 32 |
# File 'lib/sticapi_client.rb', line 30 def client @client end |
#expiry ⇒ Object
Returns the value of attribute expiry.
30 31 32 |
# File 'lib/sticapi_client.rb', line 30 def expiry @expiry end |
#host ⇒ Object
Returns the value of attribute host.
29 30 31 |
# File 'lib/sticapi_client.rb', line 29 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
29 30 31 |
# File 'lib/sticapi_client.rb', line 29 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
29 30 31 |
# File 'lib/sticapi_client.rb', line 29 def port @port end |
#ssl ⇒ Object
Returns the value of attribute ssl.
29 30 31 |
# File 'lib/sticapi_client.rb', line 29 def ssl @ssl end |
#uid ⇒ Object
Returns the value of attribute uid.
30 31 32 |
# File 'lib/sticapi_client.rb', line 30 def uid @uid end |
#urn ⇒ Object
Returns the value of attribute urn.
29 30 31 |
# File 'lib/sticapi_client.rb', line 29 def urn @urn end |
#user ⇒ Object
Returns the value of attribute user.
29 30 31 |
# File 'lib/sticapi_client.rb', line 29 def user @user end |
Instance Method Details
#expiry_now ⇒ Object
64 65 66 |
# File 'lib/sticapi_client.rb', line 64 def expiry_now @expiry = 0 end |
#get_token ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sticapi_client.rb', line 43 def get_token return unless @access_token.blank? || (DateTime.now > Time.at(@expiry.to_i)) if @access_token.present? sign_out end uri = URI.parse("#{self.uri}/auth/sign_in") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = @ssl request = Net::HTTP::Post.new(uri.request_uri) request["Content-Type"] = "application/json" request.body = { email: @user, password: @password }.to_json response = http.request(request) @access_token = response["access-token"] if response["access-token"] @client = response["client"] if response["client"] @uid = response["uid"] if response["uid"] @expiry = response["expiry"] if response["expiry"] save_token end |
#sign_out ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/sticapi_client.rb', line 85 def sign_out if @access_token.present? uri = URI.parse("#{self.uri}/auth/sign_out") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = @ssl request = Net::HTTP::Delete.new(uri.request_uri) request["Content-Type"] = "application/json" request["access-token"] = @access_token request["client"] = @client request["uid"] = @uid request["expiry"] = @expiry http.request(request) end @access_token = "" @client = "" @uid = "" @expiry = "" File.delete(token_cache_path) if File.exist?(token_cache_path) end |
#sticapi_request(route, options = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sticapi_client.rb', line 68 def sticapi_request(route, = {}) get_token response = http_request(route, ) if response.code.to_i == 401 expiry_now get_token response = http_request(route, ) end @access_token = response["access-token"] if response["access-token"] @client = response["client"] if response["client"] @uid = response["uid"] if response["uid"] @expiry = response["expiry"] if response["expiry"] JSON.parse(response.body) end |
#uri ⇒ Object
39 40 41 |
# File 'lib/sticapi_client.rb', line 39 def uri "http#{"s" if @ssl}://#{@host}:#{@port}#{"/" if @urn}#{@urn}" end |