Class: Bearcat::Client

Inherits:
Footrest::Client
  • Object
show all
Defined in:
lib/bearcat/client.rb,
lib/bearcat/client/tabs.rb,
lib/bearcat/client/files.rb,
lib/bearcat/client/pages.rb,
lib/bearcat/client/roles.rb,
lib/bearcat/client/users.rb,
lib/bearcat/client/groups.rb,
lib/bearcat/client/rubric.rb,
lib/bearcat/client/search.rb,
lib/bearcat/client/courses.rb,
lib/bearcat/client/folders.rb,
lib/bearcat/client/modules.rb,
lib/bearcat/client/o_auth2.rb,
lib/bearcat/client/quizzes.rb,
lib/bearcat/client/reports.rb,
lib/bearcat/client/accounts.rb,
lib/bearcat/client/graph_ql.rb,
lib/bearcat/client/outcomes.rb,
lib/bearcat/client/sections.rb,
lib/bearcat/client/analytics.rb,
lib/bearcat/client/progresses.rb,
lib/bearcat/client/assignments.rb,
lib/bearcat/client/conferences.rb,
lib/bearcat/client/discussions.rb,
lib/bearcat/client/enrollments.rb,
lib/bearcat/client/file_helper.rb,
lib/bearcat/client/submissions.rb,
lib/bearcat/client/canvas_files.rb,
lib/bearcat/client/module_items.rb,
lib/bearcat/client/conversations.rb,
lib/bearcat/client/external_tools.rb,
lib/bearcat/client/outcome_groups.rb,
lib/bearcat/client/account_reports.rb,
lib/bearcat/client/calendar_events.rb,
lib/bearcat/client/content_exports.rb,
lib/bearcat/client/outcome_imports.rb,
lib/bearcat/client/group_categories.rb,
lib/bearcat/client/assignment_groups.rb,
lib/bearcat/client/blueprint_courses.rb,
lib/bearcat/client/group_memberships.rb,
lib/bearcat/client/learning_outcomes.rb,
lib/bearcat/client/rubric_assessment.rb,
lib/bearcat/client/content_migrations.rb,
lib/bearcat/client/rubric_association.rb,
lib/bearcat/client/custom_gradebook_columns.rb

Defined Under Namespace

Modules: AccountReports, Accounts, Analytics, AssignmentGroups, Assignments, BlueprintCourses, CalendarEvents, CanvasFiles, Conferences, ContentExports, ContentMigrations, Conversations, Courses, CustomGradebookColumns, Discussions, Enrollments, ExternalTools, FileHelper, Files, Folders, GraphQL, GroupCategories, GroupMemberships, Groups, LearningOutcomes, ModuleItems, Modules, OAuth2, OutcomeGroups, OutcomeImports, Outcomes, Pages, Progresses, Quizzes, Reports, Roles, Rubric, RubricAssessment, RubricAssociation, Search, Sections, Submissions, Tabs, Users

Instance Method Summary collapse

Instance Method Details

#apply_rate_limits(limit) ⇒ Object



41
42
43
44
# File 'lib/bearcat/client.rb', line 41

def apply_rate_limits(limit)
  return if limit.nil?
  self.limit_remaining = limit.to_i
end

#enforce_rate_limitsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bearcat/client.rb', line 24

def enforce_rate_limits
  return unless limit_remaining.present?
  return unless limit_remaining < Bearcat.rate_limit_min

  tts = ((Bearcat.rate_limit_min - limit_remaining) / 5).ceil
  tts = Bearcat.min_sleep_seconds if tts < Bearcat.min_sleep_seconds
  tts = Bearcat.max_sleep_seconds if tts > Bearcat.max_sleep_seconds


  message = "Canvas API rate limit minimum #{Bearcat.rate_limit_min} reached. "\
    "Sleeping for #{tts} second(s) to catch up ~zzZZ~. "\
    "Limit Remaining: #{limit_remaining}"
  Bearcat.logger.debug(message)

  sleep(tts)
end

#limit_remainingObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bearcat/client.rb', line 50

def limit_remaining
  if using_master_rate_limit?
    Bearcat.master_mutex.synchronize do
      limit = PaulWalker::RateLimit.get(config[:token], config[:token])
      if limit.nil?
        PaulWalker::RateLimit.add(config[:token], config[:token], 0, Bearcat::rate_limit_min)
        limit = { current: 0 }.with_indifferent_access
      end
      Bearcat.logger.debug limit['current'].to_s
      limit['current']
    end
  else
    Bearcat.rate_limits[config[:token]]
  end
end

#limit_remaining=(value) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/bearcat/client.rb', line 66

def limit_remaining=(value)
  if using_master_rate_limit?
    Bearcat.master_mutex.synchronize do
      PaulWalker::RateLimit.add(config[:token], config[:token], value, Bearcat::rate_limit_min)
    end
  else
    Bearcat.rate_limits[config[:token]] = value
  end
end

#request(method, &block) ⇒ Object

Override Footrest request for ApiArray support



17
18
19
20
21
22
# File 'lib/bearcat/client.rb', line 17

def request(method, &block)
  enforce_rate_limits
  response = connection.send(method, &block)
  apply_rate_limits(response.headers['x-rate-limit-remaining'])
  ApiArray.process_response(response, self)
end

#using_master_rate_limit?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/bearcat/client.rb', line 46

def using_master_rate_limit?
  config[:master_rate_limit].present? || Bearcat.master_rate_limit.present?
end