Class: Purple::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/purple/client.rb

Class Method Summary collapse

Class Method Details

.authorization(type = nil, value = nil, **custom_options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/purple/client.rb', line 21

def authorization(type = nil, value = nil, **custom_options)
  if type.nil? && value.nil? && custom_options.empty?
    @authorization
  else
    @authorization = case type
                     when :bearer
                       Purple::Requests::Authorization.bearer_token(value)
                     when :google_auth
                       Purple::Requests::Authorization.google_auth(**custom_options)
                     when :custom_headers
                       Purple::Requests::Authorization.custom_headers(custom_options)
                     when :custom_query
                       Purple::Requests::Authorization.custom_query(custom_options)
                     end
  end
end

.body(type = nil, **structure, &block) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/purple/client.rb', line 93

def body(type = nil, **structure, &block)
  case type
  when :default
    @current_resp.body = :default
  else
    @current_resp.body = Responses::Body.new(structure:, response: @current_resp, transform: block)
  end
end

.callback(&block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/purple/client.rb', line 38

def callback(&block)
  if block_given?
    @callback = block
  else
    @callback
  end
end

.domain(value = nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/purple/client.rb', line 13

def domain(value = nil)
  if value.nil?
    @domain
  else
    @domain = value
  end
end

.method_missing(method_name, *args) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/purple/client.rb', line 102

def method_missing(method_name, *args, &)
  if @paths&.any? { |path| path.name == method_name }
    @paths.find { |path| path.name == method_name }
  else
    super
  end
end

.params(*args, &block) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/purple/client.rb', line 76

def params(*args, &block)
  @parent_path.request.params = if block_given?
                                  block
                                else
                                  args
                                end
end

.path(name, method: :get) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/purple/client.rb', line 46

def path(name, method: :get)
  path = Path.new(name:, parent: @parent_path, method:, client: self)

  @paths ||= []
  @paths << path

  @parent_path.children << path if @parent_path

  if block_given?
    @parent_path = path
    yield
  end

  @parent_path = path.parent
end

.requestObject



72
73
74
# File 'lib/purple/client.rb', line 72

def request
  yield if block_given?
end

.response(status) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/purple/client.rb', line 84

def response(status)
  resp = Response.new(status:, path: @parent_path)

  @parent_path.responses << resp
  @current_resp = resp

  yield if block_given?
end

.root_method(method_name) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/purple/client.rb', line 62

def root_method(method_name)
  current_path = @parent_path

  define_singleton_method method_name do |**args, &block|
    params = current_path.request.params.call(**args) if current_path.request.params.is_a?(Proc)

    current_path.execute(params, args, &block)
  end
end