Class: ActionKitRest::API

Inherits:
Vertebrae::API
  • Object
show all
Defined in:
lib/action_kit_rest/api.rb

Direct Known Subclasses

Client

Instance Method Summary collapse

Instance Method Details

#default_optionsObject



29
30
31
32
33
34
35
# File 'lib/action_kit_rest/api.rb', line 29

def default_options
  {
    user_agent: 'ActionKitRestGem',
    prefix: '/rest/v1',
    content_type: 'application/json; charset=utf-8'
  }
end

#extract_data_from_params(params) ⇒ Object

:nodoc:



21
22
23
24
25
26
27
# File 'lib/action_kit_rest/api.rb', line 21

def extract_data_from_params(params) # :nodoc:
  if params.key?('data') && params['data'].present?
    params['data']
  else
    params
  end
end

#post_json_request(path, params) ⇒ Object



9
10
11
12
13
# File 'lib/action_kit_rest/api.rb', line 9

def post_json_request(path, params)
  p = {}
  p['data'] = params.to_json
  post_request(path, p)
end

#put_json_request(path, params) ⇒ Object



15
16
17
18
19
# File 'lib/action_kit_rest/api.rb', line 15

def put_json_request(path, params)
  p = {}
  p['data'] = params.to_json
  put_request(path, p)
end

#request(*args) ⇒ Object



5
6
7
# File 'lib/action_kit_rest/api.rb', line 5

def request(*args)
  ActionKitRest::Response::Wrapper.new(super(*args))
end

#setupObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/action_kit_rest/api.rb', line 37

def setup
  # Override the faraday connection to use ActionKitRest's custom error handler
  connection.faraday_connection = Faraday.new(connection.configuration.faraday_options) do |f|
    if connection.configuration.authenticated?
      f.request :authorization, :basic, connection.configuration.username, connection.configuration.password
    end
    f.request :multipart
    f.request :url_encoded

    f.response :logger if ENV['DEBUG']

    unless initialisation_options[:raw]
      f.response :mashify
      f.response :json
    end

    f.use ActionKitRest::Response::RaiseError
    
    # Handle adapter registration - some adapters need to be required separately in Faraday 2.x
    adapter_name = connection.configuration.adapter
    begin
      f.adapter adapter_name
    rescue Faraday::Error => e
      # Fall back to default adapter if the requested one isn't available
      warn "Adapter #{adapter_name} not available (#{e.message}), falling back to default"
      f.adapter Faraday.default_adapter
    end
  end
end