From 40c80aa717ca9debc613e9d933e66b7835a6fe4e Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Thu, 9 Dec 2021 15:25:02 -0800 Subject: Init commit --- .ruby-version | 1 + Gemfile | 14 +++++++++ Gemfile.lock | 71 +++++++++++++++++++++++++++++++++++++++++++ app.rb | 13 ++++++++ gpio.rb | 25 +++++++++++++++ spec/app_spec.rb | 11 +++++++ spec/fixtures/gpio1/direction | 1 + spec/fixtures/gpio1/value | 1 + spec/gpio_spec.rb | 61 +++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 19 ++++++++++++ 10 files changed, 217 insertions(+) create mode 100644 .ruby-version create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 app.rb create mode 100644 gpio.rb create mode 100644 spec/app_spec.rb create mode 100644 spec/fixtures/gpio1/direction create mode 100644 spec/fixtures/gpio1/value create mode 100644 spec/gpio_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..743af5e --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.6.8 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..4b02bbe --- /dev/null +++ b/Gemfile @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } + +# gem "rails" + +gem "rspec", "~> 3.10" +gem "sinatra", "~> 2.1" +gem 'pry' +gem 'rack-test' + +gem "capybara", "~> 3.35" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..3529798 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,71 @@ +GEM + remote: https://rubygems.org/ + specs: + addressable (2.8.0) + public_suffix (>= 2.0.2, < 5.0) + capybara (3.36.0) + addressable + matrix + mini_mime (>= 0.1.3) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (>= 1.5, < 3.0) + xpath (~> 3.2) + coderay (1.1.3) + diff-lcs (1.4.4) + matrix (0.4.2) + method_source (1.0.0) + mini_mime (1.1.2) + mini_portile2 (2.6.1) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + nokogiri (1.12.5) + mini_portile2 (~> 2.6.1) + racc (~> 1.4) + pry (0.14.1) + coderay (~> 1.1) + method_source (~> 1.0) + public_suffix (4.0.6) + racc (1.6.0) + rack (2.2.3) + rack-protection (2.1.0) + rack + rack-test (1.1.0) + rack (>= 1.0, < 3) + regexp_parser (2.1.1) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-support (3.10.2) + ruby2_keywords (0.0.5) + sinatra (2.1.0) + mustermann (~> 1.0) + rack (~> 2.2) + rack-protection (= 2.1.0) + tilt (~> 2.0) + tilt (2.0.10) + xpath (3.2.0) + nokogiri (~> 1.8) + +PLATFORMS + ruby + +DEPENDENCIES + capybara (~> 3.35) + pry + rack-test + rspec (~> 3.10) + sinatra (~> 2.1) + +BUNDLED WITH + 1.17.2 diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..6d5cd74 --- /dev/null +++ b/app.rb @@ -0,0 +1,13 @@ +require 'sinatra' +require './gpio' + +gpio = Gpio.new(1) + +get '/' do + "Lightswitch
" +end + +post '/' do + gpio.toggle + redirect '/' +end diff --git a/gpio.rb b/gpio.rb new file mode 100644 index 0000000..e7ad10e --- /dev/null +++ b/gpio.rb @@ -0,0 +1,25 @@ +class Gpio + def initialize(pin, prefix: "/sys/class/gpio") + @pin = pin + @prefix = prefix + end + + attr_reader :pin, :prefix + + def export + File.write("#{prefix}/export", pin) + File.write("#{prefix}/gpio#{pin}/direction", "out") + end + + def value + File.read("#{prefix}/gpio#{pin}/value").strip == "1" ? true : false + end + + def value=(value) + File.write("#{prefix}/gpio#{pin}/value", value ? "1" : "0") + end + + def toggle + self.value = !value + end +end diff --git a/spec/app_spec.rb b/spec/app_spec.rb new file mode 100644 index 0000000..151d690 --- /dev/null +++ b/spec/app_spec.rb @@ -0,0 +1,11 @@ +require './spec/spec_helper' + +RSpec.describe "App feature spec" do + it "renders text" do + visit "/" + expect(page).to have_content("Lightswitch") + expect(page).to have_button("On") + click_button "On" + expect(page).to have_button("Off") + end +end diff --git a/spec/fixtures/gpio1/direction b/spec/fixtures/gpio1/direction new file mode 100644 index 0000000..c585e19 --- /dev/null +++ b/spec/fixtures/gpio1/direction @@ -0,0 +1 @@ +out \ No newline at end of file diff --git a/spec/fixtures/gpio1/value b/spec/fixtures/gpio1/value new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/spec/fixtures/gpio1/value @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/spec/gpio_spec.rb b/spec/gpio_spec.rb new file mode 100644 index 0000000..48dd8ec --- /dev/null +++ b/spec/gpio_spec.rb @@ -0,0 +1,61 @@ +require 'fileutils' +require './spec/spec_helper' +require './gpio' + +RSpec.describe Gpio do + let(:gpio) { described_class.new(1, prefix: "./spec/fixtures") } + + before do + gpio.value = false + end + + describe "#export" do + subject { gpio.export } + + before do + FileUtils.touch("./spec/fixtures/export") + end + + after do + FileUtils.remove("./spec/fixtures/export") + end + + it "writes the pin to the export file" do + subject + export_file = File.read("./spec/fixtures/export") + expect(export_file).to eq("1") + end + + it "sets the pin as an output" do + subject + direction_file = File.read("./spec/fixtures/gpio1/direction") + expect(direction_file).to eq("out") + end + end + + describe "#toggle" do + subject { gpio.toggle } + + it "toggles the value to true" do + expect { subject }.to change { gpio.value }.from(false).to(true) + end + end + + describe "#value" do + subject { gpio.value } + + it "returns false" do + expect(subject).to be_falsey + end + + context "value is set to true" do + before do + gpio.value = true + end + + it "returns true" do + expect(subject).to be_truthy + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..8dc19c8 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,19 @@ +require './app.rb' +require 'rspec' +require 'rack/test' +require 'capybara/rspec' + +ENV['RACK_ENV'] = 'test' + +module RSpecMixin + include Rack::Test::Methods + def app + Sinatra::Application + end + Capybara.app = Sinatra::Application.new +end + +RSpec.configure do |config| + config.include RSpecMixin + config.include Capybara::DSL +end -- cgit v1.2.3