Module: Nonacat

Defined in:
lib/nonacat.rb,
lib/nonacat/version.rb

Defined Under Namespace

Modules: Github, Link

Constant Summary collapse

GITHUB_API_PATH =
Pathname.new(__dir__).join(-"../github-rest-api-description/api.github.com.oas-#{oas}.json.zz")
GITHUB_API =

A Scorpio::OpenAPI::Document for Github's API

Scorpio.new_document(
  JSON.parse(Zlib.inflate(GITHUB_API_PATH.read)),
  after_initialize: proc do |node|
    # Name schema components like Github::CodeSearchResultItem, Github::Repository, etc.
    if node.jsi_is_schema? && !node.jsi_schema_module_name
      if node.jsi_ptr.parent == JSI::Ptr['components', 'schemas']
        const_name = JSI::Util::Private.const_name_from_parts(node.jsi_ptr.tokens.last.to_s.split(/[_-]/)) # TODO shouldn't use JSI privates
        Github.const_set(const_name, node.jsi_schema_module) if const_name && !Github.constants.include?(const_name.to_sym)
      end
    end

    node_describes_url = node.jsi_is_schema? && (
      node.keyword_value?('format', 'uri') ||
      (node.keyword_value?('type', 'string') && (
        (node.jsi_ptr.tokens.last.respond_to?(:to_str) && node.jsi_ptr.tokens.last =~ /_url\z/) ||
        (node.example.respond_to?(:to_str) && node.example['://']))))
    if node_describes_url
      node.jsi_schema_module.include(Nonacat::Link)
    end
  end,
)
VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.authorizationObject

Authorization params passed to Faraday::Request::Authorization.

Nonacat.authorization = ['Bearer', 'github_pat_2kxqIkfByCRkCGT2...']
Nonacat.authorization = [:basic, 'notEthan', 'p4$$w0rd']


75
76
77
# File 'lib/nonacat.rb', line 75

def authorization
  @authorization
end

Class Method Details

.paginate_items(operation, ratelimit: true, **conf) {|JSI::Base| ... } ⇒ nil, Enumerator

Yields each item in each page of results from the indicated operation.

Parameters:

  • operation (String, Scorpio::OpenAPI::Operation)

    an operationId or an Operation

  • ratelimit (Boolean) (defaults to: true)

    ratelimit each response

Yields:

  • (JSI::Base)

    each item in each page of results

Returns:

  • (nil, Enumerator)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nonacat.rb', line 83

def paginate_items(operation, ratelimit: true, **conf, &block)
  return to_enum(__method__, operation, **conf) unless block_given?
  operation = operation.is_a?(Scorpio::OpenAPI::Operation) ? operation : GITHUB_API.operations[operation]
  operation.each_link_page(**conf) do |page_ur|
    if page_ur.response.body_object.respond_to?(:to_ary)
      page_ur.response.body_object.each(&block)
    elsif page_ur.response.body_object.respond_to?(:to_hash) && page_ur.response.body_object.key?('items') && page_ur.response.body_object['items'].respond_to?(:to_ary)
      page_ur.response.body_object['items'].each(&block)
    else
      raise("pagination not detected in operation response.\noperation: #{operation.pretty_inspect.chomp}\nresponse ur: #{page_ur.pretty_inspect.chomp}")
    end
    Nonacat.ratelimit(page_ur) if ratelimit
  end
end

.ratelimit(ur) ⇒ Object

If the given ur's response indicates insufficent remaining ratelimit, sleep until limit will reset



99
100
101
102
103
104
# File 'lib/nonacat.rb', line 99

def ratelimit(ur)
  if ur.response.headers['x-ratelimit-remaining'] && Float(ur.response.headers['x-ratelimit-remaining']) <= 1 && ur.response.headers['x-ratelimit-reset']
    sleep(1 + (Time.at(Float(ur.response.headers['x-ratelimit-reset'])) - Time.now))
  end
  ur
end