Class: Purple::Path

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kw_args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/purple/path.rb', line 37

def method_missing(method_name, *args, **kw_args, &)
  if children.any? { |child| child.name == method_name }
    child = children.find { |child| child.name == method_name }

    if child.is_param
      child.with(*args)
    end

    if child.children.any?
      child
    else
      callback_arguments = client.additional_callback_arguments.map do |arg|
        kw_args.delete(arg)
      end

      child.execute(*args, *callback_arguments)
    end
  else
    super
  end
end

Instance Method Details

#execute(params = {}, args = {}, *callback_arguments) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/purple/path.rb', line 59

def execute(params = {}, args = {}, *callback_arguments)
  headers = {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json'
  }

  if client.authorization
    authorization = client.authorization[:data].call

    headers.deep_merge!(authorization) if client.authorization[:type].in?(%i[bearer custom_headers])
    params.deep_merge!(authorization) if client.authorization[:type] == :custom_query
  end

  connection = Faraday.new(url: client.domain) do |conn|
    conn.options.timeout = client.timeout if client.timeout.present?
    conn.headers = headers
  end

  if client.domain.blank?
    raise ArgumentError, 'Client domain is not set. Please set the domain in the client configuration.'
  end

  unless client.domain.start_with?('http')
    raise ArgumentError, "Invalid URL: #{client.domain}. Ensure you have set protocol (http/https) in the client domain."
  end

  url = "#{client.domain}/#{full_path}"

  response = case method
             when :get
               connection.get(url, params)
             when :post
               connection.post(url, params.to_json)
             when :put
               connection.put(url, params.to_json)
             when :delete
               connection.delete(url, params)
             when :patch
               connection.patch(url, params.to_json)
             end

  resp_structure = responses.find { |resp| resp.status_code == response.status }

  if resp_structure.nil?
    raise "#{client.domain}/#{full_path} returns #{response.status}, but it is not defined in the client"
  else
    object = if resp_structure.body.is_a?(Purple::Responses::Body)
               resp_structure.body.validate!(response.body, args)
             elsif resp_structure.body == :default
               response.body
             else
               {}
             end

    client.callback&.call(url, params, headers, JSON.parse(response.body), *callback_arguments)

    if block_given?
      yield(resp_structure.status, object)
    else
      object
    end
  end
end

#full_pathObject



23
24
25
26
27
28
29
30
31
# File 'lib/purple/path.rb', line 23

def full_path
  current_path = if is_param
                   CGI.escape(@param_value.to_s)
                 else
                   name
                 end

  parent.nil? ? current_path : "#{parent.full_path}/#{current_path}"
end

#with(*args) ⇒ Object



33
34
35
# File 'lib/purple/path.rb', line 33

def with(*args)
  @param_value = args.first
end