Class: Lemur::API
- Inherits:
-
Object
- Object
- Lemur::API
- Defined in:
- lib/lemur.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#request_options ⇒ Object
readonly
Returns the value of attribute request_options.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#security_options ⇒ Object
readonly
Returns the value of attribute security_options.
Instance Method Summary collapse
- #get(request_params) ⇒ Object
- #get_new_token ⇒ Object
- #get_request(request_params) ⇒ Object
-
#initialize(application_secret_key, application_key, access_token, application_id = nil, refresh_token = nil) ⇒ API
constructor
A new instance of API.
Constructor Details
#initialize(application_secret_key, application_key, access_token, application_id = nil, refresh_token = nil) ⇒ API
Returns a new instance of API.
16 17 18 19 20 21 22 23 |
# File 'lib/lemur.rb', line 16 def initialize(application_secret_key, application_key, access_token, application_id = nil, refresh_token = nil) start_faraday @security_options = { application_secret_key: application_secret_key, application_key: application_key, access_token: access_token, application_id: application_id, refresh_token: refresh_token } end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
25 26 27 |
# File 'lib/lemur.rb', line 25 def connection @connection end |
#request_options ⇒ Object (readonly)
Returns the value of attribute request_options.
25 26 27 |
# File 'lib/lemur.rb', line 25 def @request_options end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
25 26 27 |
# File 'lib/lemur.rb', line 25 def response @response end |
#security_options ⇒ Object (readonly)
Returns the value of attribute security_options.
25 26 27 |
# File 'lib/lemur.rb', line 25 def @security_options end |
Instance Method Details
#get(request_params) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/lemur.rb', line 56 def get(request_params) @response = get_request(request_params) json_data = JSON.parse(response.body) if json_data.is_a? Hash if json_data['error_code'] raise ApiError, json_data end end json_data end |
#get_new_token ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lemur.rb', line 27 def get_new_token if @security_options[:application_id].blank? || @security_options[:refresh_token].blank? raise ArgumentError, 'wrong number of arguments' end = { :headers['Content-Type'] => 'application/json', :url => Lemur::ODNOKLASSNIKI_NEW_TOKEN_URL } conn = init_faraday() new_token_response = conn.post do |req| req.params = { refresh_token: @security_options[:refresh_token], grant_type: 'refresh_token', client_id: @security_options[:application_id], client_secret: @security_options[:application_secret_key] } end new_token_response = JSON.parse(new_token_response.body) @security_options[:access_token] = new_token_response['access_token'] new_token_response['access_token'] end |
#get_request(request_params) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/lemur.rb', line 48 def get_request(request_params) @request_options = request_params @response = @connection.get do |request| request.params = final_request_params(@request_options.merge(application_key: [:application_key]), [:access_token]) end end |