Class: AnalyticsPSW::Api
- Inherits:
-
Object
- Object
- AnalyticsPSW::Api
- Defined in:
- lib/analytics-psw/api.rb
Class Method Summary collapse
Instance Method Summary collapse
- #access_token_hash ⇒ Object
- #connection ⇒ Object
-
#initialize(server_url, client_id, client_secret, proxy = nil) ⇒ Api
constructor
A new instance of Api.
Constructor Details
#initialize(server_url, client_id, client_secret, proxy = nil) ⇒ Api
Returns a new instance of Api.
3 4 5 6 7 8 |
# File 'lib/analytics-psw/api.rb', line 3 def initialize(server_url, client_id, client_secret, proxy = nil) @server_url = server_url @client_id = client_id @client_secret = client_secret @proxy = proxy end |
Class Method Details
.parse_json_response(json_response) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/analytics-psw/api.rb', line 30 def self.parse_json_response(json_response) parsed_response = {} parsed_response['status'] = json_response.status if json_response.status.present? if [200, 201, 202].include?(json_response.status) parsed_response['body'] = JSON.parse(json_response.body) if json_response.body.present? parsed_response['location'] = json_response.headers['location'] if json_response.headers.present? && json_response.headers['location'] else parsed_response['body'] = {} parsed_response['error_message'] = json_response.body end parsed_response end |
Instance Method Details
#access_token_hash ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/analytics-psw/api.rb', line 16 def access_token_hash response_hash = {} response = connection.post do |request| request.url "#{@server_url}/oauth/token?client_id=#{@client_id}&client_secret=#{@client_secret}" request.headers['Content-Type'] = 'application/json' end parsed_response = self.class.parse_json_response(response) if parsed_response['body'].present? response_hash[:token] = parsed_response['body']['access_token'] response_hash[:expires] = Time.now + parsed_response['body']['expires_in'] end response_hash end |
#connection ⇒ Object
10 11 12 13 14 |
# File 'lib/analytics-psw/api.rb', line 10 def connection opts = {} opts[:proxy] = { uri: @proxy } if @proxy Faraday.new(@server_url, opts) end |