class NanoleafRuby::BaseRequester
Public Class Methods
new(raise_errors: false)
click to toggle source
# File lib/nanoleaf_ruby/base_requester.rb, line 7 def initialize(raise_errors: false) @raise_errors = raise_errors end
Public Instance Methods
delete(url:, params: {})
click to toggle source
# File lib/nanoleaf_ruby/base_requester.rb, line 29 def delete(url:, params: {}) self.class.delete(url, body: params.to_json) end
get(url:)
click to toggle source
# File lib/nanoleaf_ruby/base_requester.rb, line 11 def get(url:) response = self.class.get(url) parse_response(response: response) end
post(url:, params: {}, headers: {})
click to toggle source
# File lib/nanoleaf_ruby/base_requester.rb, line 24 def post(url:, params: {}, headers: {}) response = self.class.post(url, body: params.to_json, headers: headers) parse_response(response: response, params: params) end
put(url:, params: {}, headers: {})
click to toggle source
# File lib/nanoleaf_ruby/base_requester.rb, line 16 def put(url:, params: {}, headers: {}) body = params.to_json headers = headers.merge('Content-Type' => 'application/json', 'Content-Length' => body.length.to_s) response = self.class.put(url, body: body, headers: headers) parse_response(response: response, params: params) end
Private Instance Methods
error_lookup(error_code)
click to toggle source
rubocop:disable Metrics/MethodLength
# File lib/nanoleaf_ruby/base_requester.rb, line 36 def error_lookup(error_code) case error_code when 401 'Not authorized! This is an invalid token. Try calling generate_auth_token' when 404 'Resource not found' when 422 'Unprocessible Entity (check your params?)' when 500 'Internal Server Error' else 'Something went wrong' end end
parse_json(json_string)
click to toggle source
# File lib/nanoleaf_ruby/base_requester.rb, line 71 def parse_json(json_string) body = {} begin body = JSON.parse(json_string) if json_string && !json_string.empty? rescue JSON::ParserError return {} end body end
parse_response(response:, params: nil, body: {})
click to toggle source
rubocop:enable Metrics/MethodLength
# File lib/nanoleaf_ruby/base_requester.rb, line 52 def parse_response(response:, params: nil, body: {}) if response.code < 299 body[:data] = parse_json(response.body) body[:success] = true else error = write_error_message(response) body[:error] = error body[:success] = false end body[:code] = response.code body.merge!(raw: { body: response.body, params: params }) end
write_error_message(response)
click to toggle source
# File lib/nanoleaf_ruby/base_requester.rb, line 65 def write_error_message(response) error = "Error #{response.code}: #{error_lookup(response.code)}" raise error if @raise_errors error end