Class: DuffelAPI::Services::WebhooksService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/duffel_api/services/webhooks_service.rb

Defined Under Namespace

Classes: PingResult

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #make_request

Constructor Details

This class inherits a constructor from DuffelAPI::Services::BaseService

Instance Method Details

#create(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/duffel_api/services/webhooks_service.rb', line 20

def create(options = {})
  path = "/air/webhooks"

  params = options.delete(:params) || {}
  options[:params] = {}
  options[:params]["data"] = params

  begin
    response = make_request(:post, path, options)

    # Response doesn't raise any errors until #body is called
    response.tap(&:raw_body)
  end

  return if response.raw_body.nil?

  Resources::Webhook.new(unenvelope_body(response.parsed_body), response)
end

#ping(id, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/duffel_api/services/webhooks_service.rb', line 58

def ping(id, options = {})
  path = substitute_url_pattern("/air/webhooks/:id/actions/ping", "id" => id)

  params = options.delete(:params) || {}
  options[:params] = {}
  options[:params]["data"] = params

  begin
    response = make_request(:post, path, options)

    # Response doesn't raise any errors until #body is called
    response.tap(&:raw_body)
  end
rescue DuffelAPI::Errors::Error => e
  # We expect this API call to *ALWAYS* lead to an error being raised since
  # it returns a non-JSON 204 response even if it's successful. We just catch
  # that and bubble it up in a nicer way.
  if e.api_response.status_code == 204
    PingResult.new(e.api_response)
  else
    raise e
  end
end

#update(id, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/duffel_api/services/webhooks_service.rb', line 39

def update(id, options = {})
  path = substitute_url_pattern("/air/webhooks/:id", "id" => id)

  params = options.delete(:params) || {}
  options[:params] = {}
  options[:params]["data"] = params

  begin
    response = make_request(:patch, path, options)

    # Response doesn't raise any errors until #body is called
    response.tap(&:raw_body)
  end

  return if response.raw_body.nil?

  Resources::Webhook.new(unenvelope_body(response.parsed_body), response)
end