Class: SabreApiRuby::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sabre_api_ruby/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/sabre_api_ruby/client.rb', line 7

def initialize(configuration)
  @configuration = configuration
  configuration.validate!
  setup_oauth_client
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/sabre_api_ruby/client.rb', line 5

def access_token
  @access_token
end

#configurationObject (readonly)

Returns the value of attribute configuration.



5
6
7
# File 'lib/sabre_api_ruby/client.rb', line 5

def configuration
  @configuration
end

#oauth_clientObject (readonly)

Returns the value of attribute oauth_client.



5
6
7
# File 'lib/sabre_api_ruby/client.rb', line 5

def oauth_client
  @oauth_client
end

Instance Method Details

#cancel_booking(booking_id, params = {}) ⇒ Object



21
22
23
# File 'lib/sabre_api_ruby/client.rb', line 21

def cancel_booking(booking_id, params = {})
  Services::CancelBooking.new(self).call(booking_id, params)
end

#check_flight_tickets(booking_id) ⇒ Object



29
30
31
# File 'lib/sabre_api_ruby/client.rb', line 29

def check_flight_tickets(booking_id)
  Services::CheckFlightTickets.new(self).call(booking_id)
end

#create_booking(params = {}) ⇒ Object



13
14
15
# File 'lib/sabre_api_ruby/client.rb', line 13

def create_booking(params = {})
  Services::CreateBooking.new(self).call(params)
end

#get_access_tokenObject



33
34
35
36
37
38
39
40
# File 'lib/sabre_api_ruby/client.rb', line 33

def get_access_token
  return @access_token if @access_token && !@access_token.expired?

  @access_token = @oauth_client.client_credentials.get_token
  @access_token
rescue OAuth2::Error => e
  raise AuthenticationError, "Failed to obtain access token: #{e.message}"
end

#get_booking(booking_id) ⇒ Object



17
18
19
# File 'lib/sabre_api_ruby/client.rb', line 17

def get_booking(booking_id)
  Services::GetBooking.new(self).call(booking_id)
end

#modify_booking(booking_id, params = {}) ⇒ Object



25
26
27
# File 'lib/sabre_api_ruby/client.rb', line 25

def modify_booking(booking_id, params = {})
  Services::ModifyBooking.new(self).call(booking_id, params)
end

#request(method, endpoint, params = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sabre_api_ruby/client.rb', line 42

def request(method, endpoint, params = {})
  token = get_access_token

  response = connection.send(method) do |req|
    req.url endpoint
    req.headers['Authorization'] = "Bearer #{token.token}"
    req.headers['Content-Type'] = 'application/json'
    req.headers['User-Agent'] = configuration.user_agent

    case method
    when :get
      req.params.update(params)
    when :post, :put, :patch
      req.body = params.to_json
    end
  end

  handle_response(response)
rescue Faraday::TimeoutError
  raise APIError, 'Request timeout'
rescue Faraday::ConnectionFailed
  raise APIError, 'Connection failed'
end