Class: Sticapi::SticapiClient

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

Constant Summary collapse

TOKEN_CACHE_PATH =
"tmp/sticapi_token.yml"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSticapiClient

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_tokenObject

Returns the value of attribute access_token.



30
31
32
# File 'lib/sticapi_client.rb', line 30

def access_token
  @access_token
end

#clientObject

Returns the value of attribute client.



30
31
32
# File 'lib/sticapi_client.rb', line 30

def client
  @client
end

#expiryObject

Returns the value of attribute expiry.



30
31
32
# File 'lib/sticapi_client.rb', line 30

def expiry
  @expiry
end

#hostObject

Returns the value of attribute host.



29
30
31
# File 'lib/sticapi_client.rb', line 29

def host
  @host
end

#passwordObject

Returns the value of attribute password.



29
30
31
# File 'lib/sticapi_client.rb', line 29

def password
  @password
end

#portObject

Returns the value of attribute port.



29
30
31
# File 'lib/sticapi_client.rb', line 29

def port
  @port
end

#sslObject

Returns the value of attribute ssl.



29
30
31
# File 'lib/sticapi_client.rb', line 29

def ssl
  @ssl
end

#uidObject

Returns the value of attribute uid.



30
31
32
# File 'lib/sticapi_client.rb', line 30

def uid
  @uid
end

#urnObject

Returns the value of attribute urn.



29
30
31
# File 'lib/sticapi_client.rb', line 29

def urn
  @urn
end

#userObject

Returns the value of attribute user.



29
30
31
# File 'lib/sticapi_client.rb', line 29

def user
  @user
end

Instance Method Details

#expiry_nowObject



64
65
66
# File 'lib/sticapi_client.rb', line 64

def expiry_now
  @expiry = 0
end

#get_tokenObject



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_outObject



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, options = {})
  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"]
  JSON.parse(response.body)
end

#uriObject



39
40
41
# File 'lib/sticapi_client.rb', line 39

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