Class: Sticapi::SticapiClient

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sticapi_client.rb

Constant Summary collapse

MUTEX =
Monitor.new
OPEN_TIMEOUT =
10
READ_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSticapiClient

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_tokenObject

Returns the value of attribute access_token.



33
34
35
# File 'lib/sticapi_client.rb', line 33

def access_token
  @access_token
end

#clientObject

Returns the value of attribute client.



33
34
35
# File 'lib/sticapi_client.rb', line 33

def client
  @client
end

#expiryObject

Returns the value of attribute expiry.



33
34
35
# File 'lib/sticapi_client.rb', line 33

def expiry
  @expiry
end

#hostObject

Returns the value of attribute host.



32
33
34
# File 'lib/sticapi_client.rb', line 32

def host
  @host
end

#passwordObject

Returns the value of attribute password.



32
33
34
# File 'lib/sticapi_client.rb', line 32

def password
  @password
end

#portObject

Returns the value of attribute port.



32
33
34
# File 'lib/sticapi_client.rb', line 32

def port
  @port
end

#sslObject

Returns the value of attribute ssl.



32
33
34
# File 'lib/sticapi_client.rb', line 32

def ssl
  @ssl
end

#uidObject

Returns the value of attribute uid.



33
34
35
# File 'lib/sticapi_client.rb', line 33

def uid
  @uid
end

#urnObject

Returns the value of attribute urn.



32
33
34
# File 'lib/sticapi_client.rb', line 32

def urn
  @urn
end

#userObject

Returns the value of attribute user.



32
33
34
# File 'lib/sticapi_client.rb', line 32

def user
  @user
end

Instance Method Details

#expiry_nowObject



72
73
74
75
76
# File 'lib/sticapi_client.rb', line 72

def expiry_now
  MUTEX.synchronize do
    @expiry = 0
  end
end

#get_tokenObject



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_outObject



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, options = {})
  MUTEX.synchronize do
    get_token
    response = http_request(route, options)

    if response.code.to_i == 401
      expiry_now
      get_token
      response = http_request(route, options)
    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

#uriObject



43
44
45
# File 'lib/sticapi_client.rb', line 43

def uri
  "http#{"s" if @ssl}://#{@host}:#{@port}#{"/" if @urn}#{@urn}"
end