Class: Sticapi::SticapiClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSticapiClient

Returns a new instance of SticapiClient.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sticapi_client.rb', line 38

def initialize
  configs = YAML.load_file("#{Rails.root}/config/sticapi.yml")[Rails.env]
  @host = configs["host"]
  @port = configs["port"] || 80
  @user = configs["user"]
  @urn = configs["urn"]
  @ssl = configs["ssl"] || false
  @password = configs["password"]
  @access_token = ""
  @client = ""
  @uid = ""
  @expiry = ""
  get_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.



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

def client
  @client
end

#expiryObject

Returns the value of attribute expiry.



36
37
38
# File 'lib/sticapi_client.rb', line 36

def expiry
  @expiry
end

#hostObject

Returns the value of attribute host.



27
28
29
# File 'lib/sticapi_client.rb', line 27

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.



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

def port
  @port
end

#sslObject

Returns the value of attribute ssl.



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

def ssl
  @ssl
end

#uidObject

Returns the value of attribute uid.



35
36
37
# File 'lib/sticapi_client.rb', line 35

def uid
  @uid
end

#urnObject

Returns the value of attribute urn.



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

def urn
  @urn
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#expiry_nowObject



71
72
73
# File 'lib/sticapi_client.rb', line 71

def expiry_now
  @expiry = 0
end

#get_tokenObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sticapi_client.rb', line 57

def get_token
  if @access_token.blank? || (DateTime.now > Time.at(@expiry.to_i))
    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["email"] = @user
    request["password"] = @password
    response = http.request(request)
    update_token(response)
  end
end

#sign_outObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sticapi_client.rb', line 103

def sign_out
  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
  @access_token = ""
  response = http.request(request)
end

#sticapi_request(route, options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sticapi_client.rb', line 82

def sticapi_request(route, options = {})
  get_token
  kind = options[:kind] || "post"
  uri = URI.parse("#{self.uri}#{route}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = @ssl
  request = nil
  request = Net::HTTP::Post.new(uri.request_uri) if kind == "post"
  request = Net::HTTP::Get.new(uri.request_uri) if kind == "get"
  request["Content-Type"] = "application/json"
  request["access-token"] = @access_token
  request["client"] = @client
  request["uid"] = @uid
  request["expiry"] = @expiry
  request.body = options.except(:kind).to_json
  response = http.request(request)
  update_token(response)
  sign_out
  JSON.parse(response.body)
end

#update_token(response) ⇒ Object



75
76
77
78
79
80
# File 'lib/sticapi_client.rb', line 75

def update_token(response)
  @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"]
end

#uriObject



53
54
55
# File 'lib/sticapi_client.rb', line 53

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