Преглед на файлове

add basic function & api

Ray Lee преди 6 години
родител
ревизия
9f2ea1a831
променени са 11 файла, в които са добавени 70 реда и са изтрити 10 реда
  1. 4 0
      Gemfile
  2. 12 0
      Gemfile.lock
  3. 48 0
      app/controllers/main_controller.rb
  4. 6 0
      config/routes.rb
  5. 0 0
      test/controllers/.keep
  6. 0 0
      test/fixtures/.keep
  7. 0 0
      test/fixtures/files/.keep
  8. 0 0
      test/integration/.keep
  9. 0 0
      test/mailers/.keep
  10. 0 0
      test/models/.keep
  11. 0 10
      test/test_helper.rb

+ 4 - 0
Gemfile

@@ -31,6 +31,10 @@ gem 'bootsnap', '>= 1.1.0', require: false
 group :development, :test do
   # Call 'byebug' anywhere in the code to stop execution and get a debugger console
   gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
+  # pry debug tool
+  gem 'pry'
+  gem 'pry-rails'
+  gem 'pry-byebug'
 end
 
 group :development do

+ 12 - 0
Gemfile.lock

@@ -47,6 +47,7 @@ GEM
       msgpack (~> 1.0)
     builder (3.2.3)
     byebug (10.0.2)
+    coderay (1.1.2)
     concurrent-ruby (1.0.5)
     crass (1.0.4)
     erubi (1.7.1)
@@ -75,6 +76,14 @@ GEM
     nio4r (2.3.0)
     nokogiri (1.8.2)
       mini_portile2 (~> 2.3.0)
+    pry (0.11.3)
+      coderay (~> 1.1.0)
+      method_source (~> 0.9.0)
+    pry-byebug (3.6.0)
+      byebug (~> 10.0)
+      pry (~> 0.10)
+    pry-rails (0.3.6)
+      pry (>= 0.10.4)
     puma (3.11.4)
     rack (2.0.4)
     rack-test (1.0.0)
@@ -136,6 +145,9 @@ DEPENDENCIES
   bootsnap (>= 1.1.0)
   byebug
   listen (>= 3.0.5, < 3.2)
+  pry
+  pry-byebug
+  pry-rails
   puma (~> 3.11)
   rails (~> 5.2.0)
   spring

+ 48 - 0
app/controllers/main_controller.rb

@@ -0,0 +1,48 @@
+require 'csv'
+
+class MainController < ApplicationController
+  before_action :valify_event_key, only: [:save_data, :download_data]
+
+  def home
+    head :ok
+  end
+
+  def new_event_key
+    @event_key = SecureRandom.hex
+    File.open(data_path, "w+")
+    render json: { event_key: @event_key}
+  end
+
+  def save_data
+    hash = params.as_json.except('controller', 'action', 'event_key')
+    CSV.open(data_path, 'ab') do |csv|
+      return if hash.blank?
+      csv << hash.keys if first_row?
+      csv << hash.values
+    end
+    head :ok
+  end
+
+  def download_data
+    send_file(data_path, filename: "#{@event_key}.csv", type: 'text/csv; charset=utf-8; header=present')
+  end
+
+  private
+
+  def valify_event_key
+    @event_key = params[:event_key]
+    if @event_key.nil?
+      head :forbidden && return
+    elsif Dir.glob(data_path).blank?
+      head :forbidden && return
+    end
+  end
+
+  def first_row?
+    File.size(data_path) == 0
+  end
+
+  def data_path
+    Rails.root.join("storage/#{@event_key}.csv")
+  end
+end

+ 6 - 0
config/routes.rb

@@ -1,3 +1,9 @@
 Rails.application.routes.draw do
   # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
+
+  get   'event-key',                to: 'main#new_event_key'
+  post  'save-data/:event_key',     to: 'main#save_data'
+  get   'download-data/:event_key', to: 'main#download_data'
+
+  root 'main#home'
 end

+ 0 - 0
test/controllers/.keep


+ 0 - 0
test/fixtures/.keep


+ 0 - 0
test/fixtures/files/.keep


+ 0 - 0
test/integration/.keep


+ 0 - 0
test/mailers/.keep


+ 0 - 0
test/models/.keep


+ 0 - 10
test/test_helper.rb

@@ -1,10 +0,0 @@
-ENV['RAILS_ENV'] ||= 'test'
-require_relative '../config/environment'
-require 'rails/test_help'
-
-class ActiveSupport::TestCase
-  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
-  fixtures :all
-
-  # Add more helper methods to be used by all tests here...
-end