gemfile cleanup; added js/lib
This commit is contained in:
parent
56d5965dd2
commit
98a204ab59
11
Gemfile
11
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'
|
||||
|
||||
|
@ -1,22 +0,0 @@
|
||||
<template>
|
||||
<div id="app">
|
||||
<p>{{ message }}</p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data: function () {
|
||||
return {
|
||||
message: "Hello Vue!"
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
p {
|
||||
font-size: 2em;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
74
app/javascript/lib/Api.js
Normal file
74
app/javascript/lib/Api.js
Normal file
@ -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;
|
44
app/javascript/lib/Errors.js
Normal file
44
app/javascript/lib/Errors.js
Normal file
@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user