summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Van Doorn <nick@nv.delivery>2021-12-09 15:25:02 -0800
committerNick Van Doorn <nick@nv.delivery>2021-12-09 15:25:02 -0800
commit40c80aa717ca9debc613e9d933e66b7835a6fe4e (patch)
treee4e47f68c4278fdb549a8181ee53972bf1fd4c74
Init commitHEADmaster
-rw-r--r--.ruby-version1
-rw-r--r--Gemfile14
-rw-r--r--Gemfile.lock71
-rw-r--r--app.rb13
-rw-r--r--gpio.rb25
-rw-r--r--spec/app_spec.rb11
-rw-r--r--spec/fixtures/gpio1/direction1
-rw-r--r--spec/fixtures/gpio1/value1
-rw-r--r--spec/gpio_spec.rb61
-rw-r--r--spec/spec_helper.rb19
10 files changed, 217 insertions, 0 deletions
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 <form method='POST' action='/'><input type='submit' value='#{gpio.value ? 'Off' : 'On'}'></form>"
+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