Class: SticapiClient::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.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sticapi_client.rb', line 23

def initialize
  configs = YAML.load_file("#{Rails.root}/config/sticapi.yml")[Rails.env]
  # configs = YAML.load_file("/home/ricardo/dev/sticapi_client/lib/generators/sticapi_client/templates/sticapi.yml")[Rails.env]
  @host = configs['host']
  @port = configs['port'] || 80
  @user = configs['user']
  @urn = configs['urn']
  @password = configs['password']
  @access_token = ''
  @client = ''
  @uid = ''
  @expiry = ''
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



18
19
20
# File 'lib/sticapi_client.rb', line 18

def access_token
  @access_token
end

#clientObject

Returns the value of attribute client.



19
20
21
# File 'lib/sticapi_client.rb', line 19

def client
  @client
end

#expiryObject

Returns the value of attribute expiry.



21
22
23
# File 'lib/sticapi_client.rb', line 21

def expiry
  @expiry
end

#hostObject

Returns the value of attribute host.



13
14
15
# File 'lib/sticapi_client.rb', line 13

def host
  @host
end

#passwordObject

Returns the value of attribute password.



17
18
19
# File 'lib/sticapi_client.rb', line 17

def password
  @password
end

#portObject

Returns the value of attribute port.



15
16
17
# File 'lib/sticapi_client.rb', line 15

def port
  @port
end

#uidObject

Returns the value of attribute uid.



20
21
22
# File 'lib/sticapi_client.rb', line 20

def uid
  @uid
end

#urnObject

Returns the value of attribute urn.



14
15
16
# File 'lib/sticapi_client.rb', line 14

def urn
  @urn
end

#userObject

Returns the value of attribute user.



16
17
18
# File 'lib/sticapi_client.rb', line 16

def user
  @user
end

Instance Method Details

#get_tokenObject



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

def get_token
  if @access_token.blank?
    uri = URI.parse("#{self.uri}/auth/sign_in")
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.request_uri)
    request['email'] = @user
    request['password'] = @password
    response = http.request(request)
    update_token(response)
  end
end

#update_token(response) ⇒ Object



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

def update_token(response)
  @access_token = response['access-token']
  @client = response['client']
  @uid = response['uid']
  @expiry = response['expiry']
end

#uriObject



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

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