Class: Lemur::API

Inherits:
Object
  • Object
show all
Defined in:
lib/lemur.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#connectionObject (readonly)

Returns the value of attribute connection.



25
26
27
# File 'lib/lemur.rb', line 25

def connection
  @connection
end

#request_optionsObject (readonly)

Returns the value of attribute request_options.



25
26
27
# File 'lib/lemur.rb', line 25

def request_options
  @request_options
end

#responseObject (readonly)

Returns the value of attribute response.



25
26
27
# File 'lib/lemur.rb', line 25

def response
  @response
end

#security_optionsObject (readonly)

Returns the value of attribute security_options.



25
26
27
# File 'lib/lemur.rb', line 25

def security_options
  @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_tokenObject



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
  faraday_options = {
        :headers['Content-Type'] => 'application/json',
        :url => Lemur::ODNOKLASSNIKI_NEW_TOKEN_URL
      }
  conn = init_faraday(faraday_options)
  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: security_options[:application_key]),
                                           security_options[:access_token])
  end
end