Skip to content

Commit

Permalink
Add charts controller and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ulverson committed Jun 9, 2017
1 parent 3292d61 commit 8e6675c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/controllers/charts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion config/charts.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@

development:
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"
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"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Rails.application.routes.draw do
root to: "charts#index"
get "charts/:name", to: "charts#show", as: :chart
end
33 changes: 33 additions & 0 deletions spec/controllers/charts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spec/support/requests.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Requests
end

require_relative "requests/bitbay"
require_relative "requests/bitbay"
require_relative "requests/json_helpers/request_helpers"
7 changes: 7 additions & 0 deletions spec/support/requests/json_helpers/request_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Requests
module JsonHelpers
def json
JSON.parse(response.body)
end
end
end

0 comments on commit 8e6675c

Please sign in to comment.