Class: Purple::Client
- Inherits:
-
Object
- Object
- Purple::Client
- Defined in:
- lib/purple/client.rb,
lib/purple/client/version.rb
Constant Summary collapse
- VERSION =
Purple::VERSION
Class Method Summary collapse
- .additional_callback_arguments(*array) ⇒ Object
- .authorization(type = nil, value = nil, **custom_options) ⇒ Object
- .body(type = nil, **structure, &block) ⇒ Object
- .callback(&block) ⇒ Object
- .domain(value = nil) ⇒ Object
- .draw(file_path) ⇒ Object
- .method_missing(method_name, *args) ⇒ Object
- .params(*args, &block) ⇒ Object
- .path(name, method: :get, is_param: false) ⇒ Object
- .request ⇒ Object
- .response(status) ⇒ Object
- .root_method(method_name) ⇒ Object
- .timeout(value = nil) ⇒ Object
Class Method Details
.additional_callback_arguments(*array) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/purple/client.rb', line 142 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 (type = nil, value = nil, **) if type.nil? && value.nil? && .empty? else = case type when :bearer Purple::Requests::Authorization.bearer_token(value) when :google_auth Purple::Requests::Authorization.google_auth(**) when :custom_headers Purple::Requests::Authorization.custom_headers() when :custom_query Purple::Requests::Authorization.custom_query() end end end |
.body(type = nil, **structure, &block) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/purple/client.rb', line 125 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 |
.draw(file_path) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/purple/client.rb', line 72 def draw(file_path) caller_path = caller_locations(1, 1).first.absolute_path base_dir = File.dirname(caller_path) full_path = if File.extname(file_path).empty? File.("#{file_path}.rb", base_dir) else File.(file_path, base_dir) end class_eval(File.read(full_path), full_path) end |
.method_missing(method_name, *args) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/purple/client.rb', line 134 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
108 109 110 111 112 113 114 |
# File 'lib/purple/client.rb', line 108 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 |
.request ⇒ Object
104 105 106 |
# File 'lib/purple/client.rb', line 104 def request yield if block_given? end |
.response(status) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/purple/client.rb', line 116 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/purple/client.rb', line 85 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 |