Class: WebFunction::Endpoint
- Inherits:
-
Object
- Object
- WebFunction::Endpoint
- Defined in:
- lib/web_function/endpoint.rb
Class Method Summary collapse
Instance Method Summary collapse
- #arguments ⇒ Object
- #attributes ⇒ Object
- #docs ⇒ Object
- #errors ⇒ Object
- #flags ⇒ Object
- #group ⇒ Object
-
#initialize(endpoint) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #name ⇒ Object
- #returns ⇒ Object
Constructor Details
#initialize(endpoint) ⇒ Endpoint
Returns a new instance of Endpoint.
5 6 7 |
# File 'lib/web_function/endpoint.rb', line 5 def initialize(endpoint) @endpoint = endpoint end |
Class Method Details
.invoke(url, bearer_auth: nil, args: {}) ⇒ Object
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 |
# File 'lib/web_function/endpoint.rb', line 9 def self.invoke(url, bearer_auth: nil, args: {}) headers = { "Content-Type": "application/json", "Accept": "application/json", } if bearer_auth headers["Authorization"] = "Bearer #{bearer_auth}" end response = Excon.post(url, headers: headers, body: JSON.generate(args), ) result = JSON.parse(response.body) if response.status == 400 case result when Array if result.length == 3 && result[0].is_a?(String) && result[1].is_a?(String) code = result[0] = result[1] details = result[2] raise WebFunction::Error.new(, code: code, details: details) else raise WebFunction::Error.new("Bad request", details: result) end when String raise WebFunction::Error.new(result) else raise WebFunction::Error.new("Bad request", details: result) end end if response.status != 200 raise WebFunction::Error.new("Something went wrong, unexpected status code [#{response.status}]") end result end |
Instance Method Details
#arguments ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/web_function/endpoint.rb', line 72 def arguments unless @endpoint["arguments"].is_a?(Array) return [] end @endpoint["arguments"].map { |argument| Argument.new(argument) } end |
#attributes ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/web_function/endpoint.rb', line 80 def attributes unless @endpoint["attributes"].is_a?(Array) return [] end @endpoint["attributes"].map { |attribute| Attribute.new(attribute) } end |
#docs ⇒ Object
68 69 70 |
# File 'lib/web_function/endpoint.rb', line 68 def docs @endpoint["docs"] end |
#errors ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/web_function/endpoint.rb', line 88 def errors unless @endpoint["errors"].is_a?(Array) return [] end @endpoint["errors"].map { |error| DocumentedError.new(error) } end |
#flags ⇒ Object
60 61 62 |
# File 'lib/web_function/endpoint.rb', line 60 def flags @endpoint["flags"] end |
#group ⇒ Object
64 65 66 |
# File 'lib/web_function/endpoint.rb', line 64 def group @endpoint["group"] end |
#name ⇒ Object
52 53 54 |
# File 'lib/web_function/endpoint.rb', line 52 def name @endpoint["name"] end |
#returns ⇒ Object
56 57 58 |
# File 'lib/web_function/endpoint.rb', line 56 def returns @endpoint["returns"] end |