Class: SabreDevStudio::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/sabre_dev_studio/base.rb

Constant Summary collapse

@@token =
nil

Class Method Summary collapse

Class Method Details

.access_tokenObject



26
27
28
# File 'lib/sabre_dev_studio/base.rb', line 26

def self.access_token
  @@token
end

.get(path, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sabre_dev_studio/base.rb', line 44

def self.get(path, options = {})
  attempt = 0
  begin
    attempt += 1
    get_access_token if @@token.nil?
    headers = {
      'Authorization'   => "Bearer #{@@token}",
      'Accept-Encoding' => 'gzip'
    }
    data = super(
      SabreDevStudio.configuration.uri + path,
      :query       => options[:query],
      :ssl_version => :TLSv1,
      :headers     => headers
    )
    verify_response(data)
    return data
  rescue SabreDevStudio::Unauthorized
    if attempt == 1
      get_access_token
      retry
    end
  end
end

.get_access_tokenObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sabre_dev_studio/base.rb', line 30

def self.get_access_token
  uri           = SabreDevStudio.configuration.uri
  client_id     = Base64.strict_encode64(SabreDevStudio.configuration.client_id)
  client_secret = Base64.strict_encode64(SabreDevStudio.configuration.client_secret)
  credentials   = Base64.strict_encode64("#{client_id}:#{client_secret}")
  headers       = { 'Authorization' => "Basic #{credentials}" }
  req           = post("#{uri}/v1/auth/token",
                        :body        => { :grant_type => 'client_credentials' },
                        :ssl_version => :TLSv1,
                        :verbose     => true,
                        :headers     => headers)
  @@token       = req['access_token']
end

.verify_response(data) ⇒ Object



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
# File 'lib/sabre_dev_studio/base.rb', line 71

def self.verify_response(data)
  # NOTE: should all of these raise or should some reissue the request?
  case data.response.code.to_i
  when 200
    # nothing to see here, please move on
    return
  when 400
    raise SabreDevStudio::BadRequest.new(data)
  when 401
    raise SabreDevStudio::Unauthorized.new(data)
  when 403
    raise SabreDevStudio::Forbidden.new(data)
  when 404
    raise SabreDevStudio::NotFound.new(data)
  when 406
    raise SabreDevStudio::NotAcceptable.new(data)
  when 429
    raise SabreDevStudio::RateLimited.new(data)
  when 500
    raise SabreDevStudio::InternalServerError.new(data)
  when 503
    raise SabreDevStudio::ServiceUnavailable.new(data)
  when 504
    raise SabreDevStudio::GatewayTimeout.new(data)
  else
    raise SabreDevStudio::Error.new(data)
  end
end