Class: Sticapi::SticapiClient
- Inherits:
-
Object
- Object
- Sticapi::SticapiClient
- Includes:
- Singleton
- Defined in:
- lib/sticapi_client.rb
Constant Summary collapse
- MUTEX =
Monitor.new
- OPEN_TIMEOUT =
10- READ_TIMEOUT =
30
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.
38 39 40 41 |
# File 'lib/sticapi_client.rb', line 38 def initialize load_config load_token end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
33 34 35 |
# File 'lib/sticapi_client.rb', line 33 def access_token @access_token end |
#client ⇒ Object
Returns the value of attribute client.
33 34 35 |
# File 'lib/sticapi_client.rb', line 33 def client @client end |
#expiry ⇒ Object
Returns the value of attribute expiry.
33 34 35 |
# File 'lib/sticapi_client.rb', line 33 def expiry @expiry end |
#host ⇒ Object
Returns the value of attribute host.
32 33 34 |
# File 'lib/sticapi_client.rb', line 32 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
32 33 34 |
# File 'lib/sticapi_client.rb', line 32 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
32 33 34 |
# File 'lib/sticapi_client.rb', line 32 def port @port end |
#ssl ⇒ Object
Returns the value of attribute ssl.
32 33 34 |
# File 'lib/sticapi_client.rb', line 32 def ssl @ssl end |
#uid ⇒ Object
Returns the value of attribute uid.
33 34 35 |
# File 'lib/sticapi_client.rb', line 33 def uid @uid end |
#urn ⇒ Object
Returns the value of attribute urn.
32 33 34 |
# File 'lib/sticapi_client.rb', line 32 def urn @urn end |
#user ⇒ Object
Returns the value of attribute user.
32 33 34 |
# File 'lib/sticapi_client.rb', line 32 def user @user end |
Instance Method Details
#expiry_now ⇒ Object
72 73 74 75 76 |
# File 'lib/sticapi_client.rb', line 72 def expiry_now MUTEX.synchronize do @expiry = 0 end end |
#get_token ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sticapi_client.rb', line 47 def get_token return unless token_expired? MUTEX.synchronize do return unless token_expired? sign_out if @access_token.present? uri = URI.parse("#{self.uri}/auth/sign_in") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = @ssl http.open_timeout = OPEN_TIMEOUT http.read_timeout = READ_TIMEOUT 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 end |
#sign_out ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/sticapi_client.rb', line 97 def sign_out MUTEX.synchronize do if @access_token.present? uri = URI.parse("#{self.uri}/auth/sign_out") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = @ssl http.open_timeout = OPEN_TIMEOUT http.read_timeout = READ_TIMEOUT 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 end |
#sticapi_request(route, options = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sticapi_client.rb', line 78 def sticapi_request(route, = {}) MUTEX.synchronize do 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"] parse_response(response) end end |
#uri ⇒ Object
43 44 45 |
# File 'lib/sticapi_client.rb', line 43 def uri "http#{"s" if @ssl}://#{@host}:#{@port}#{"/" if @urn}#{@urn}" end |