File: gh.rb

package info (click to toggle)
ruby-gh 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,644 kB
  • sloc: ruby: 1,793; makefile: 4
file content (84 lines) | stat: -rw-r--r-- 2,036 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

require 'gh/version'
require 'backports' if RUBY_VERSION < '2.1'
require 'forwardable'

module GH
  autoload :Cache, 'gh/cache'
  autoload :Case, 'gh/case'
  autoload :CustomLimit, 'gh/custom_limit'
  autoload :Error, 'gh/error'
  autoload :Instrumentation, 'gh/instrumentation'
  autoload :LazyLoader, 'gh/lazy_loader'
  autoload :LinkFollower, 'gh/link_follower'
  autoload :MergeCommit, 'gh/merge_commit'
  autoload :Normalizer, 'gh/normalizer'
  autoload :Pagination, 'gh/pagination'
  autoload :Parallel, 'gh/parallel'
  autoload :Remote, 'gh/remote'
  autoload :Response, 'gh/response'
  autoload :ResponseXHeaderFormatter, 'gh/response_x_header_formatter'
  autoload :ResponseWrapper, 'gh/response_wrapper'
  autoload :Stack, 'gh/stack'
  autoload :TokenCheck, 'gh/token_check'
  autoload :Wrapper, 'gh/wrapper'

  def self.with(backend)
    if backend.is_a?(Hash)
      @options ||= {}
      options = @options
      @options = @options.merge(backend)
      backend = DefaultStack.build(@options)
    end

    if block_given?
      was = current
      self.current = backend
      yield
    else
      backend
    end
  ensure
    @options = options if options
    self.current = was if was
  end

  def self.set(options)
    Thread.current[:GH] = nil
    DefaultStack.options.merge! options
  end

  def self.current
    Thread.current[:GH] ||= DefaultStack.new
  end

  def self.current=(backend)
    Thread.current[:GH] = backend
  end

  extend SingleForwardable
  def_delegators :current, :api_host, :[], :reset, :load, :post, :delete, :patch, :put, :in_parallel, :in_parallel?,
                 :options, :head

  def self.method_missing(*args, &block)
    current.public_send(*args, &block)
  end

  def self.respond_to_missing?(method, *)
    super
  end

  DefaultStack = Stack.new do
    use Instrumentation
    use Parallel
    use TokenCheck
    use Pagination
    use LinkFollower
    use MergeCommit
    use LazyLoader
    use Normalizer
    use CustomLimit
    use Remote
  end
end