Class: Lightrail::Connection
- Inherits:
-
Object
- Object
- Lightrail::Connection
- Defined in:
- lib/lightrail_client/connection.rb
Class Method Summary collapse
- .connection ⇒ Object
- .handle_response(response) ⇒ Object
- .make_get_request_and_parse_response(url) ⇒ Object
- .make_post_request_and_parse_response(url, body) ⇒ Object
- .ping ⇒ Object
Class Method Details
.connection ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/lightrail_client/connection.rb', line 3 def self.connection conn = Faraday.new Lightrail.api_base, ssl: {version: :TLSv1_2} conn.headers['Content-Type'] = 'application/json; charset=utf-8' conn.headers['Authorization'] = "Bearer #{Lightrail.api_key}" conn.headers['User-Agent'] = "Lightrail-Ruby/#{Gem.loaded_specs['lightrail_client'].version.version}" conn end |
.handle_response(response) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lightrail_client/connection.rb', line 28 def self.handle_response(response) body = JSON.parse(response.body) || {} = body['message'] || '' case response.status when 200...300 JSON.parse(response.body) when 400 if ( =~ /insufficient value/i) raise Lightrail::InsufficientValueError.new(, response) else raise Lightrail::BadParameterError.new(, response) end when 401, 403 raise Lightrail::AuthorizationError.new(, response) when 404 raise Lightrail::CouldNotFindObjectError.new(, response) when 409 raise Lightrail::BadParameterError.new(, response) else raise LightrailError.new("Server responded with: (#{response.status}) #{}", response) end end |
.make_get_request_and_parse_response(url) ⇒ Object
23 24 25 26 |
# File 'lib/lightrail_client/connection.rb', line 23 def self.make_get_request_and_parse_response (url) resp = Lightrail::Connection.connection.get {|req| req.url url} self.handle_response(resp) end |
.make_post_request_and_parse_response(url, body) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/lightrail_client/connection.rb', line 15 def self.make_post_request_and_parse_response (url, body) resp = Lightrail::Connection.connection.post do |req| req.url url req.body = JSON.generate(body) end self.handle_response(resp) end |
.ping ⇒ Object
11 12 13 |
# File 'lib/lightrail_client/connection.rb', line 11 def self.ping self.make_get_request_and_parse_response('ping') end |