From 45bcc789621ee83891728662045f1e0417862047 Mon Sep 17 00:00:00 2001 From: Leena Date: Thu, 4 Feb 2016 16:19:21 +0530 Subject: [PATCH] API for app usage tracking --- app/controllers/app_usages_controller.rb | 25 +++++++++++++++++ app/models/app_usage.rb | 3 ++ config/routes.rb | 2 ++ .../20160204094618_create_app_usages.rb | 11 ++++++++ db/schema.rb | 10 ++++++- .../controllers/app_usages_controller_spec.rb | 28 +++++++++++++++++++ spec/factories/app_usages.rb | 13 +++++++++ spec/models/app_usage_spec.rb | 9 ++++++ 8 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 app/controllers/app_usages_controller.rb create mode 100644 app/models/app_usage.rb create mode 100644 db/migrate/20160204094618_create_app_usages.rb create mode 100644 spec/controllers/app_usages_controller_spec.rb create mode 100644 spec/factories/app_usages.rb create mode 100644 spec/models/app_usage_spec.rb diff --git a/app/controllers/app_usages_controller.rb b/app/controllers/app_usages_controller.rb new file mode 100644 index 0000000..8a8d2f3 --- /dev/null +++ b/app/controllers/app_usages_controller.rb @@ -0,0 +1,25 @@ +class AppUsagesController < ApplicationController + before_filter :authenticate_device + respond_to :json + + def create + app_usage = AppUsage.new(app_usage_params) + if app_usage.save + render json: {}, + status: :created + else + logger.warn "Error while saving App Usage #{app_usage.errors.full_messages}" + render json: {}, + status: :unprocessable_entity + end + end + private + + def app_usage_params + params.require(:app_usage). + permit(:package_name, + :usage_duration_in_seconds, + :used_on) + + end +end diff --git a/app/models/app_usage.rb b/app/models/app_usage.rb new file mode 100644 index 0000000..6ab7371 --- /dev/null +++ b/app/models/app_usage.rb @@ -0,0 +1,3 @@ +class AppUsage < ActiveRecord::Base + validates :package_name, :usage_duration_in_seconds, :used_on, presence: true +end diff --git a/config/routes.rb b/config/routes.rb index 13ee2d7..6cf79aa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + post 'app_usages/create', to: 'app_usages#create', :defaults => { :format => :json } + devise_for :admin_users, ActiveAdmin::Devise.config post 'heartbeats', to: 'heartbeats#create', :defaults => { :format => :json } diff --git a/db/migrate/20160204094618_create_app_usages.rb b/db/migrate/20160204094618_create_app_usages.rb new file mode 100644 index 0000000..8017410 --- /dev/null +++ b/db/migrate/20160204094618_create_app_usages.rb @@ -0,0 +1,11 @@ +class CreateAppUsages < ActiveRecord::Migration + def change + create_table :app_usages do |t| + t.string :package_name, null: false, index: false + t.integer :usage_duration_in_seconds, null: false, index: false + t.date :used_on, null: false, index: false + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 290eecc..9bb9c0f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151118101616) do +ActiveRecord::Schema.define(version: 20160204094618) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -49,6 +49,14 @@ ActiveRecord::Schema.define(version: 20151118101616) do add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true, using: :btree add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true, using: :btree + create_table "app_usages", force: :cascade do |t| + t.string "package_name", null: false + t.integer "usage_duration_in_seconds", null: false + t.date "used_on", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "apps", force: :cascade do |t| t.string "name" t.string "package_name" diff --git a/spec/controllers/app_usages_controller_spec.rb b/spec/controllers/app_usages_controller_spec.rb new file mode 100644 index 0000000..031ac87 --- /dev/null +++ b/spec/controllers/app_usages_controller_spec.rb @@ -0,0 +1,28 @@ +require 'rails_helper' + +RSpec.describe AppUsagesController, type: :controller do + + describe "POST #create" do + describe "Authorized" do + let(:device){create(:device)} + before(:each) do + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token) + end + it "Valid params" do + post :create, app_usage: attributes_for(:app_usage), format: :json + expect(response).to have_http_status(:success) + end + + it "InValid Params" do + post :create, app_usage: attributes_for(:invalid_app_usage), format: :json + expect(response).to have_http_status(:unprocessable_entity) + end + end + describe "POST #create" do + it "Unauthorized" do + post :create, app_usage: attributes_for(:app_usage), format: :json + expect(response).to have_http_status(:unauthorized) + end + end + end +end diff --git a/spec/factories/app_usages.rb b/spec/factories/app_usages.rb new file mode 100644 index 0000000..222f236 --- /dev/null +++ b/spec/factories/app_usages.rb @@ -0,0 +1,13 @@ +FactoryGirl.define do + factory :app_usage do + package_name "com.facebook.katana" + usage_duration_in_seconds 7000 + used_on Date.today - 1.day + end + factory :invalid_app_usage, class: AppUsage do + package_name "" + usage_duration_in_seconds 7000 + used_on Date.today - 1.day + end + +end diff --git a/spec/models/app_usage_spec.rb b/spec/models/app_usage_spec.rb new file mode 100644 index 0000000..0c5e54c --- /dev/null +++ b/spec/models/app_usage_spec.rb @@ -0,0 +1,9 @@ +require 'rails_helper' + +RSpec.describe AppUsage, type: :model do + let!(:app_usage) { create(:app_usage) } + + it { should validate_presence_of :package_name } + it { should validate_presence_of :usage_duration_in_seconds } + it { should validate_presence_of :used_on } +end