Class: Sticapi::SticapiClient
- Inherits:
-
Object
- Object
- Sticapi::SticapiClient
- Includes:
- Singleton
- Defined in:
- lib/sticapi_client.rb
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.
-
#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
- #get_token ⇒ Object
-
#initialize ⇒ SticapiClient
constructor
A new instance of SticapiClient.
- #sticapi_request(route, options = {}) ⇒ Object
- #update_token(response) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize ⇒ SticapiClient
Returns a new instance of SticapiClient.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sticapi_client.rb', line 30 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 = '' get_token end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
25 26 27 |
# File 'lib/sticapi_client.rb', line 25 def access_token @access_token end |
#client ⇒ Object
Returns the value of attribute client.
26 27 28 |
# File 'lib/sticapi_client.rb', line 26 def client @client end |
#expiry ⇒ Object
Returns the value of attribute expiry.
28 29 30 |
# File 'lib/sticapi_client.rb', line 28 def expiry @expiry end |
#host ⇒ Object
Returns the value of attribute host.
20 21 22 |
# File 'lib/sticapi_client.rb', line 20 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
24 25 26 |
# File 'lib/sticapi_client.rb', line 24 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
22 23 24 |
# File 'lib/sticapi_client.rb', line 22 def port @port end |
#uid ⇒ Object
Returns the value of attribute uid.
27 28 29 |
# File 'lib/sticapi_client.rb', line 27 def uid @uid end |
#urn ⇒ Object
Returns the value of attribute urn.
21 22 23 |
# File 'lib/sticapi_client.rb', line 21 def urn @urn end |
#user ⇒ Object
Returns the value of attribute user.
23 24 25 |
# File 'lib/sticapi_client.rb', line 23 def user @user end |
Instance Method Details
#get_token ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sticapi_client.rb', line 49 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 |
#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, = {}) kind = [:kind] || 'post' uri = URI.parse("#{self.uri}#{route}") http = Net::HTTP.new(uri.host, uri.port) 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.body = .except(:kind).to_json response = http.request(request) update_token(response) JSON.parse(response.body) end |
#update_token(response) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/sticapi_client.rb', line 61 def update_token(response) @access_token = response['access-token'] @client = response['client'] @uid = response['uid'] @expiry = response['expiry'] end |
#uri ⇒ Object
45 46 47 |
# File 'lib/sticapi_client.rb', line 45 def uri "http://#{@host}:#{@port}#{'/' if @urn}#{@urn}" end |