Class: Purple::Client

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

Constant Summary collapse

VERSION =
Purple::VERSION

Class Method Summary collapse

Class Method Details

.additional_callback_arguments(*array) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/purple/client.rb', line 129

def additional_callback_arguments(*array)
  if array.empty?
    @additional_callback_arguments || []
  else
    @additional_callback_arguments = array
  end
end

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



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

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



112
113
114
115
116
117
118
119
# File 'lib/purple/client.rb', line 112

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



48
49
50
51
52
53
54
# File 'lib/purple/client.rb', line 48

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

.domain(value = nil) ⇒ Object



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

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

.method_missing(method_name, *args) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/purple/client.rb', line 121

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



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

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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/purple/client.rb', line 56

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

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

  @parent_path.children << path if @parent_path

  if block_given?
    @parent_path = path
    yield
  end

  @parent_path = path.parent
end

.requestObject



91
92
93
# File 'lib/purple/client.rb', line 91

def request
  yield if block_given?
end

.response(status) ⇒ Object



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

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/purple/client.rb', line 72

def root_method(method_name)
  current_path = @parent_path

  define_singleton_method method_name do |*call_args, **kw_args, &block|
    if current_path.is_param
      value = call_args.first
      current_path.with(value)
    end

    callback_arguments = additional_callback_arguments.map do |arg|
      kw_args.delete(arg)
    end

    params = current_path.request.params.call(**kw_args) if current_path.request.params.is_a?(Proc)

    current_path.execute(params, kw_args, *callback_arguments, &block)
  end
end

.timeout(value = nil) ⇒ Object



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

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