From 8e6675c5c022f9f5ec8d2475fa0dc41e4e6ad25a Mon Sep 17 00:00:00 2001 From: ulverson Date: Fri, 9 Jun 2017 09:53:39 +0200 Subject: [PATCH] Add charts controller and specs --- app/controllers/charts_controller.rb | 10 ++++++ config/charts.yml | 8 ++++- config/routes.rb | 1 + spec/controllers/charts_controller_spec.rb | 33 +++++++++++++++++++ spec/rails_helper.rb | 1 + spec/support/requests.rb | 3 +- .../requests/json_helpers/request_helpers.rb | 7 ++++ 7 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 spec/controllers/charts_controller_spec.rb create mode 100644 spec/support/requests/json_helpers/request_helpers.rb diff --git a/app/controllers/charts_controller.rb b/app/controllers/charts_controller.rb index bb95472..1691aec 100644 --- a/app/controllers/charts_controller.rb +++ b/app/controllers/charts_controller.rb @@ -3,4 +3,14 @@ class ChartsController < ApplicationController def index end + def show + chart_config = Charts::Config.config[params[:name]] + @chart = Charts::Chart.new( + params[:chart_name].to_s.capitalize, + chart_config[:request_url], + chart_config[:storage_key] + ) + render json: @chart.chart_data + end + end diff --git a/config/charts.yml b/config/charts.yml index 153d678..912eb08 100644 --- a/config/charts.yml +++ b/config/charts.yml @@ -1,4 +1,3 @@ - development: bitcoin: storage_key: "btc_usd" @@ -6,6 +5,13 @@ development: ethereum: storage_key: "eth_usd" request_url: "https://bitbay.net/API/Public/ETHUSD/ticker.json" +test: + bitcoin: + storage_key: "btc_usd" + request_url: "https://bitbay.net/API/Public/BTCUSD/ticker.json" + ethereum: + storage_key: "eth_usd" + request_url: "https://bitbay.net/API/Public/ETHUSD/ticker.json" production: bitcoin: storage_key: "btc_usd" diff --git a/config/routes.rb b/config/routes.rb index e030a25..3f2330a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ Rails.application.routes.draw do root to: "charts#index" + get "charts/:name", to: "charts#show", as: :chart end diff --git a/spec/controllers/charts_controller_spec.rb b/spec/controllers/charts_controller_spec.rb new file mode 100644 index 0000000..e0ec229 --- /dev/null +++ b/spec/controllers/charts_controller_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" +require 'charts' + +describe ChartsController, type: :controller do + + let(:api_request) { Charts::ApiRequest.new(bitbay_request_url) } + let(:storage) { Charts::ResponseStorage.new(api_request, "btc_usd") } + + context "when there is chart data" do + before do + stub_bitbay_successfull_response + storage.store_request + end + + it "returns chart data" do + get :show, params: { name: "bitcoin" } + expect(response).to be_success + expect(JSON.parse(response.body)).to eq(storage.values) + end + + end + + context "when there is no chart data" do + + it "returns empty array" do + get :show, params: { name: "ethereum" } + expect(response).to be_success + expect(JSON.parse(response.body)).to eq([]) + end + + end + +end \ No newline at end of file diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 35ba48a..42d2f7e 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -13,6 +13,7 @@ config.use_transactional_fixtures = false config.include Features config.include Requests::Bitbay + config.include Requests::JsonHelpers, type: :request config.infer_spec_type_from_file_location! config.filter_rails_from_backtrace! end diff --git a/spec/support/requests.rb b/spec/support/requests.rb index 546aa03..adbcfbe 100644 --- a/spec/support/requests.rb +++ b/spec/support/requests.rb @@ -1,4 +1,5 @@ module Requests end -require_relative "requests/bitbay" \ No newline at end of file +require_relative "requests/bitbay" +require_relative "requests/json_helpers/request_helpers" \ No newline at end of file diff --git a/spec/support/requests/json_helpers/request_helpers.rb b/spec/support/requests/json_helpers/request_helpers.rb new file mode 100644 index 0000000..de341d5 --- /dev/null +++ b/spec/support/requests/json_helpers/request_helpers.rb @@ -0,0 +1,7 @@ +module Requests + module JsonHelpers + def json + JSON.parse(response.body) + end + end +end \ No newline at end of file