Class: WebFunction::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/web_function/endpoint.rb

Class Method Summary collapse

Instance Method Summary collapse

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]
        message = result[1]
        details = result[2]

        raise WebFunction::Error.new(message, 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

#argumentsObject



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

#attributesObject



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

#docsObject



68
69
70
# File 'lib/web_function/endpoint.rb', line 68

def docs
  @endpoint["docs"]
end

#errorsObject



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

#flagsObject



60
61
62
# File 'lib/web_function/endpoint.rb', line 60

def flags
  @endpoint["flags"]
end

#groupObject



64
65
66
# File 'lib/web_function/endpoint.rb', line 64

def group
  @endpoint["group"]
end

#nameObject



52
53
54
# File 'lib/web_function/endpoint.rb', line 52

def name
  @endpoint["name"]
end

#returnsObject



56
57
58
# File 'lib/web_function/endpoint.rb', line 56

def returns
  @endpoint["returns"]
end