Class: BungieSdk::ApiAgent
  
  
  
  
  
    - Inherits:
 
    - 
      Object
      
        
          - Object
 
          
            - BungieSdk::ApiAgent
 
          
        
        show all
      
     
  
  
  
  
  
      - Extended by:
 
      - T::Sig
 
  
  
  
  
  
  
  
  
    - Defined in:
 
    - lib/bungie_sdk/agent.rb
 
  
  
 
Overview
  
    
Base class for all BungieSdk resources. Handles creating and running Typhoeus requests, authentication and auth token management, and basic methods for building endpoints.
   
 
  
  
    
      Constant Summary
      collapse
    
    
      
        - BASE_URI =
          
        
 
        'https://www.bungie.net'.freeze
 
      
    
  
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
      
        - 
  
    
      #delete(path, params: nil, body: nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #get(path, params: nil, body: nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #initialize(api_data = nil)  ⇒ ApiAgent 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of ApiAgent.
 
  
 
      
        - 
  
    
      #post(path, params: nil, body: nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #put(path, params: nil, body: nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #request(method, path, params: nil, body: nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #run(request)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
    
  
  
  Constructor Details
  
    
  
  
    #initialize(api_data = nil)  ⇒ ApiAgent 
  
  
  
  
    
Returns a new instance of ApiAgent.
   
 
  
  
    
      
24
25
26 
     | 
    
      # File 'lib/bungie_sdk/agent.rb', line 24
def initialize(api_data=nil)
  @data = api_data
end 
     | 
  
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #data  ⇒ Object 
  
  
  
  
    
Returns the value of attribute data.
   
 
  
  
    
      
19
20
21 
     | 
    
      # File 'lib/bungie_sdk/agent.rb', line 19
def data
  @data
end 
     | 
  
 
    
   
  
    Instance Method Details
    
      
  
  
    #delete(path, params: nil, body: nil)  ⇒ Object 
  
  
  
  
    
      
100
101
102 
     | 
    
      # File 'lib/bungie_sdk/agent.rb', line 100
def delete(path, params: nil, body: nil)
  request(:delete, path, params: params, body: body)
end 
     | 
  
 
    
      
  
  
    #get(path, params: nil, body: nil)  ⇒ Object 
  
  
  
  
    
      
73
74
75 
     | 
    
      # File 'lib/bungie_sdk/agent.rb', line 73
def get(path, params: nil, body: nil)
  request(:get, path, params: params, body: body)
end 
     | 
  
 
    
      
  
  
    #post(path, params: nil, body: nil)  ⇒ Object 
  
  
  
  
    
      
91
92
93 
     | 
    
      # File 'lib/bungie_sdk/agent.rb', line 91
def post(path, params: nil, body: nil)
  request(:post, path, params: params, body: body)
end 
     | 
  
 
    
      
  
  
    #put(path, params: nil, body: nil)  ⇒ Object 
  
  
  
  
    
      
82
83
84 
     | 
    
      # File 'lib/bungie_sdk/agent.rb', line 82
def put(path, params: nil, body: nil)
  request(:put, path, params: params, body: body)
end 
     | 
  
 
    
      
  
  
    #request(method, path, params: nil, body: nil)  ⇒ Object 
  
  
  
  
    
      
112
113
114
115
116
117
118
119
120
121 
     | 
    
      # File 'lib/bungie_sdk/agent.rb', line 112
def request(method, path, params: nil, body: nil)
  Typhoeus::Request.new(
    "#{BASE_URI}#{path}",
    followlocation: true,
    method:         method.to_sym,
    params:         params,
    body:           body,
    headers:        
  )
end
     | 
  
 
    
      
  
  
    #run(request)  ⇒ Object 
  
  
  
  
    
      
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 
     | 
    
      # File 'lib/bungie_sdk/agent.rb', line 32
def run(request)
  retries = 0
  begin
    request.run if request.response.nil? || retries == 1
    api_response = process_response(request.response)
    @body        = api_response.body
    @headers     = api_response.
    @code        = api_response.status
    if @body['ErrorStatus'] == 'WebAuthRequired'
      TokenManager.instance.web_auth
      throw TokenException
    elsif request.response.options[:return_code] == :couldnt_connect
      TokenManager.instance.refresh_token
      throw TokenException
    end
    api_response
  rescue TokenException
    if retries.zero?
      retries += 1
      options           = request.original_options
      options[:headers] = 
      request           = Typhoeus::Request.new(
        request.base_url,
        **options
      )
      retry
    else
      throw TokenException.new('Invalid OAuth token')
    end
  end
end
     |