Class: AutoPilot::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = AutoPilot.configuration.user_id, options = {}) ⇒ API

Returns a new instance of API.



6
7
8
9
10
11
# File 'lib/auto_pilot/api.rb', line 6

def initialize(user = AutoPilot.configuration.user_id, options = {})
  @user    = user
  @options = options
  @answers = []
  add_config_client_key
end

Instance Attribute Details

#answersObject (readonly)

Returns the value of attribute answers.



4
5
6
# File 'lib/auto_pilot/api.rb', line 4

def answers
  @answers
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/auto_pilot/api.rb', line 4

def options
  @options
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'lib/auto_pilot/api.rb', line 4

def user
  @user
end

Instance Method Details

#get_answersObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/auto_pilot/api.rb', line 13

def get_answers
  Log.green "fetching information for id #{AutoPilot.configuration.user_id} via stackoverflow api"
  pages.each do |page|
    begin
      Log.green "fetching answers for page #{page}"
      response = answer_response(page)
      answers << response.data.first.answers
    rescue => e
      Log.red "An error occured: #{e}"
      Log.red '- AutoPilot will continue downloading your answers'
      break
    end
    break unless response.has_more
  end
  filtered(answers)
end

#pagesObject



30
31
32
# File 'lib/auto_pilot/api.rb', line 30

def pages
  Array(1..(AutoPilot.configuration.max_pages || 3))
end

#throttleObject

api.stackexchange.com/docs/throttle NOTE: While not strictly a throttle, the Stack Exchange API employs heavy caching and as such no application should make semantically identical requests more than once a minute.



36
37
38
39
# File 'lib/auto_pilot/api.rb', line 36

def throttle
  sleep(AutoPilot.configuration.throttle || 3)
  yield if block_given?
end