Class: DuffelAPI::Services::WebhooksService

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

Defined Under Namespace

Classes: PingResult

Constant Summary

Constants inherited from BaseService

BaseService::DEFAULT_ALL_PARAMS

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

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

Instance Method Details

#create(options = {}) ⇒ Resources::Webhook

Creates an webhook

Parameters:

  • [required, (Hash)

    a customizable set of options

Returns:

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/duffel_api/services/webhooks_service.rb', line 30

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 = {}) ⇒ PingResult

Pings a webhook by ID

Parameters:

  • id (String)

Returns:

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/duffel_api/services/webhooks_service.rb', line 79

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 = {}) ⇒ Resources::Webhook

Updates a webhook by ID

Parameters:

  • id (String)
  • [required, (Hash)

    a customizable set of options

Returns:

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/duffel_api/services/webhooks_service.rb', line 55

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