From 98a204ab593a388d2011b36dcaca053af52ba08e Mon Sep 17 00:00:00 2001 From: Dan Elbert Date: Thu, 29 Mar 2018 22:08:13 -0500 Subject: [PATCH] gemfile cleanup; added js/lib --- Gemfile | 11 ------ app/javascript/app.vue | 22 ----------- app/javascript/lib/Api.js | 74 ++++++++++++++++++++++++++++++++++++ app/javascript/lib/Errors.js | 44 +++++++++++++++++++++ 4 files changed, 118 insertions(+), 33 deletions(-) delete mode 100644 app/javascript/app.vue create mode 100644 app/javascript/lib/Api.js create mode 100644 app/javascript/lib/Errors.js diff --git a/Gemfile b/Gemfile index 63a4892..85859eb 100644 --- a/Gemfile +++ b/Gemfile @@ -3,23 +3,12 @@ source 'https://rubygems.org' gem 'rails', '5.2.0.rc2' gem 'sqlite3' gem 'pg', '~> 0.21.0' -# gem 'sass-rails', '~> 5.0' -# gem 'uglifier', '>= 1.3.0' gem 'puma', '~> 3.11' -# See https://github.com/rails/execjs#readme for more supported runtimes -# gem 'therubyracer', platforms: :ruby - gem 'webpacker', '3.4.1' gem 'bootsnap', '>= 1.1.0', require: false -# Use jquery as the JavaScript library -# gem 'jquery-rails', '~> 4.3.1' -# gem 'bootstrap-sass', '~> 3.3.7' -# gem 'kaminari', '~> 1.1.1' -# gem 'turbolinks', '~> 5.1.0' gem 'jbuilder', '~> 2.7' -# gem 'cocoon', '~> 1.2.9' gem 'unitwise', '~> 2.2.0' gem 'redcarpet', '~> 3.4.0' diff --git a/app/javascript/app.vue b/app/javascript/app.vue deleted file mode 100644 index e304dc1..0000000 --- a/app/javascript/app.vue +++ /dev/null @@ -1,22 +0,0 @@ - - - - - diff --git a/app/javascript/lib/Api.js b/app/javascript/lib/Api.js new file mode 100644 index 0000000..f2d3c2a --- /dev/null +++ b/app/javascript/lib/Api.js @@ -0,0 +1,74 @@ +import config from '../config'; +import * as Errors from './Errors'; + +class Api { + constructor() { + } + + baseUrl() { return config.baseApiUrl; } + + url(path) { + return this.baseUrl() + path; + } + + checkStatus(response) { + if (response.status >= 200 && response.status < 300) { + return response; + } else if (response.status === 404) { + throw new Errors.ApiNotFoundError(response.statusText, response); + } else { + throw new Errors.ApiServerError(response.statusText || "Unknown Server Error", response); + } + } + + parseJSON(response) { + return response.json(); + } + + performRequest(url, method, params = {}) { + const hasBody = Object.keys(params || {}).length !== 0; + + const headers = new Headers(); + headers.append('Accept', 'application/json'); + + const opts = { + headers, + method: method + }; + + if (hasBody) { + headers.append('Content-Type', 'application/json'); + opts.body = JSON.stringify(params); + } + + return fetch(url, opts).then(this.checkStatus).then(this.parseJSON); + } + + get(url, params = {}) { + + const queryParams = []; + + for (let key in params) { + const val = params[key]; + if (Array.isArray(val)) { + for (let x of val) { + queryParams.push(encodeURIComponent(key) + "[]=" + (x === null ? '' : encodeURIComponent(x))); + } + } else { + queryParams.push(encodeURIComponent(key) + "=" + (val === null ? '' : encodeURIComponent(val))); + } + } + + if (queryParams.length) { + url = url + "?" + queryParams.join("&"); + } + + return this.performRequest(url, "GET"); + } + + +} + +const api = new Api(); + +export default api; diff --git a/app/javascript/lib/Errors.js b/app/javascript/lib/Errors.js new file mode 100644 index 0000000..26bdc1f --- /dev/null +++ b/app/javascript/lib/Errors.js @@ -0,0 +1,44 @@ + +export function ApiError(message, response) { + this.message = (message || "Unknown API Error Occurred"); + this.response = response; +} +ApiError.prototype = Object.assign(new Error(), { + name: "ApiError", + + responseCode: function() { + if (this.response) { + return this.response.status; + } else { + return null; + } + } +}); + +export function ApiServerError(message, response) { + this.message = (message || "Unknown API Server Error Occurred"); + this.response = response; +} +ApiServerError.prototype = Object.assign(new ApiError(), { + name: "ApiServerError" +}); + +export function ApiNotFoundError(message, response) { + this.message = (message || "Unknown API Server Error Occurred"); + this.response = response; +} +ApiNotFoundError.prototype = Object.assign(new ApiError(), { + name: "ApiNotFoundError" +}); + + +export function onlyFor(errorType, handler) { + return (err) => { + if (err instanceof errorType) { + return handler(err); + } else { + return Promise.reject(err); + } + }; +} +