Commit 14a6cfba by John Doe

working on joule api adapter

parent 07ff5946
#frozen_string_literal: true
# Wrapper around Joule HTTP service
class JouleAdapter
include HTTParty
default_timeout 5
open_timeout 5
read_timeout 5
attr_reader :url
def initialize(url)
@url = url
end
def module_info
begin
resp = self.class.get("#{@url}/modules.json")
return nil unless resp.success?
items = resp.parsed_response
# if the site exists but is not a joule server...
required_keys = %w(name exec_cmd)
items.each do |item|
return nil unless item.respond_to?(:has_key?) &&
required_keys.all? { |s| item.key? s }
item.symbolize_keys!
end
rescue
return nil
end
return items
end
end
class JouleModulesControllerController < ApplicationController
end
class JouleModule < ApplicationRecord
belongs_to :nilm
has_many :joule_pipes
end
class JoulePipe < ApplicationRecord
belongs_to :joule_module
belongs_to :db_stream
end
# frozen_string_literal: true
# Handles construction of database objects
class UpdateJouleModules
include ServiceStatus
def initialize()
end
def run()
self
end
end
class CreateJouleModules < ActiveRecord::Migration[5.1]
def change
create_table :joule_modules do |t|
t.string :name
t.string :description
t.boolean :web_interface
t.string :exec_cmd
t.string :status
t.integer :pid
t.string :joule_id
t.timestamps
end
create_table :joule_pipes do |t|
t.belongs_to :joule_pipe, index: true
t.belongs_to :db_stream, index: true
t.string :direction
t.timestamps
end
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170525005625) do
ActiveRecord::Schema.define(version: 20180224021655) do
create_table "data_views", force: :cascade do |t|
t.integer "user_id"
......@@ -104,6 +104,28 @@ ActiveRecord::Schema.define(version: 20170525005625) do
t.boolean "available"
end
create_table "joule_modules", force: :cascade do |t|
t.string "name"
t.string "description"
t.boolean "web_interface"
t.string "exec_cmd"
t.string "status"
t.integer "pid"
t.string "joule_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "joule_pipes", force: :cascade do |t|
t.integer "joule_pipe_id"
t.integer "db_stream_id"
t.string "direction"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["db_stream_id"], name: "index_joule_pipes_on_db_stream_id"
t.index ["joule_pipe_id"], name: "index_joule_pipes_on_joule_pipe_id"
end
create_table "memberships", force: :cascade do |t|
t.integer "user_group_id"
t.integer "user_id"
......
# frozen_string_literal: true
require 'rails_helper'
describe JouleAdapter do
# use the benchtop server joule API
let (:url) {'http://172.16.1.12/joule'}
it 'retrieves module infos', :vcr do
adapter = JouleAdapter.new(url)
adapter.module_info.each do |m|
expect(m).to include(:name, :exec_cmd, :web_interface)
end
end
end
require 'rails_helper'
RSpec.describe JouleModulesControllerController, type: :controller do
end
FactoryGirl.define do
factory :joule_module do
end
end
require 'rails_helper'
RSpec.describe JouleModule, type: :model do
describe 'object' do
let(:joule_module) {JouleModule.new}
specify { expect(joule_module).to respond_to(:name) }
specify { expect(joule_module).to respond_to(:description) }
specify { expect(joule_module).to respond_to(:exec_cmd) }
specify { expect(joule_module).to respond_to(:web_interface) }
specify { expect(joule_module).to respond_to(:status) }
specify { expect(joule_module).to respond_to(:joule_id) }
end
it 'removes pipes when destroyed' do
@joule_module = JouleModule.create
@joule_module.joule_pipes << JoulePipe.create(
db_stream: DbStream.create,
direction: 'output')
expect(JouleModule.find_by_id(@joule_module.id).pipes.count).to equal 1
@joule_module.destroy
expect(JouleModule.count).to equal 0
# deletes associated pipes
expect(JoulePipe.count).to equal 0
# does not delete the streams
expect(DbStream.count).to equal 2
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment