yourkarma 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +28 -0
  7. data/Rakefile +2 -0
  8. data/bin/yourkarma +8 -0
  9. data/lib/yourkarma.rb +5 -0
  10. data/lib/yourkarma/benchmarker.rb +36 -0
  11. data/lib/yourkarma/cli.rb +195 -0
  12. data/lib/yourkarma/client.rb +61 -0
  13. data/lib/yourkarma/device.rb +35 -0
  14. data/lib/yourkarma/version.rb +3 -0
  15. data/spec/cassettes/.keep +0 -0
  16. data/spec/cassettes/CLI/output/connected_to_a_hotspot/.yml +86 -0
  17. data/spec/cassettes/CLI/output/connected_to_a_hotspot_without_an_assigned_WAN_IP_address/.yml +45 -0
  18. data/spec/cassettes/CLI/output/connected_to_the_internet/.yml +86 -0
  19. data/spec/cassettes/CLI/output/connected_to_the_internet_with_a_slow_connection/.yml +45 -0
  20. data/spec/cassettes/CLI/status_code/connected_to_a_hotspot/.yml +86 -0
  21. data/spec/cassettes/CLI/status_code/connected_to_a_hotspot_without_an_assigned_WAN_IP_address/.yml +45 -0
  22. data/spec/cassettes/CLI/status_code/connected_to_the_internet/.yml +86 -0
  23. data/spec/cassettes/CLI/status_code/connected_to_the_internet_with_a_slow_connection/.yml +45 -0
  24. data/spec/features/cli_spec.rb +77 -0
  25. data/spec/spec_helper.rb +22 -0
  26. data/spec/support/have_line_matching_matcher.rb +5 -0
  27. data/spec/support/vcr.rb +12 -0
  28. data/spec/units/benchmarker_spec.rb +50 -0
  29. data/spec/units/cli_argument_parser_spec.rb +26 -0
  30. data/spec/units/cli_reporter_spec.rb +61 -0
  31. data/spec/units/cli_spec.rb +41 -0
  32. data/spec/units/client_spec.rb +54 -0
  33. data/spec/units/device_spec.rb +21 -0
  34. data/what_karma.gemspec +30 -0
  35. metadata +167 -0
@@ -0,0 +1,45 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://hotspot.yourkarma.com/api/status.json
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ Host:
17
+ - hotspot.yourkarma.com
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Cache-Control:
24
+ - no-cache
25
+ - no-store, no-cache, must-revalidate, post-check=0, pre-check=0
26
+ Expires:
27
+ - "-1"
28
+ - "-1"
29
+ Content-Type:
30
+ - application/json; charset=utf-8
31
+ Access-Control-Allow-Origin:
32
+ - "*"
33
+ Transfer-Encoding:
34
+ - chunked
35
+ Date:
36
+ - Wed, 30 Apr 2014 21:49:57 GMT
37
+ Server:
38
+ - lighttpd/1.4.30-devel-4855
39
+ body:
40
+ encoding: UTF-8
41
+ string: "{\"device\":{\"name\":\"IMW-C918W\",\"swversion\":\"R4855\",\"hwversion\":\"R06\",\"uptime\":\"P0Y0M0DT0H4M37S\",\"batterypower\":100,\"charging\":false,\"waninterface\":{\"macaddress\":\"001E312C42D0\",\"ipaddress\":\"74.60.178.162\",\"bsid\":\"00:00:02:07:44:E9\",\"rssi\":-75,\"cinr\":21,\"connectionduration\":\"P0Y0M0DT0H0M18S\"},\"wifiinterface\":{\"ssid\":\"Karma
42
+ Wi-Fi\",\"users\":1}}}\r\n"
43
+ http_version:
44
+ recorded_at: Wed, 30 Apr 2014 21:49:58 GMT
45
+ recorded_with: VCR 2.9.0
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+ require 'webmock/rspec'
3
+
4
+ describe "CLI", :vcr do
5
+ let(:io) { StringIO.new }
6
+ let(:args) { ['--timeout', '10', '--verbose'] }
7
+ let(:cli) { YourKarma::CLI.run(io, args) }
8
+
9
+ describe "output" do
10
+ subject do
11
+ cli
12
+ io.string
13
+ end
14
+
15
+ context "connected to a hotspot" do
16
+ it { should match /connected/i }
17
+ end
18
+
19
+ context "connected to the internet" do
20
+ it { should match "online" }
21
+ it { should_not match /slow/i }
22
+ end
23
+
24
+ context "connected to the internet with a slow connection" do
25
+ before do
26
+ stub_request(:any, /.*yourkarma.com\/dashboard.*/).to_raise(Timeout::Error.new "Fail")
27
+ end
28
+ it { should match /slow/i }
29
+ end
30
+
31
+ context "connected to a hotspot without an assigned WAN IP address" do
32
+ before do
33
+ stub_request(:any, /.*yourkarma.com\/dashboard.*/).to_raise(Timeout::Error.new "Fail")
34
+ end
35
+ it { should match /acquiring IP address/i }
36
+ it { should_not match "Fail" }
37
+ end
38
+
39
+ context "not connected to a hotspot" do
40
+ before do
41
+ stub_request(:any, /.*yourkarma.com.*/).to_raise(Timeout::Error.new "Fail")
42
+ end
43
+ it { should match /Not connected/i }
44
+ it { should match "Fail" }
45
+ end
46
+ end
47
+
48
+ describe "status code" do
49
+ subject { cli }
50
+
51
+ context "connected to the internet" do
52
+ it { should be 0 }
53
+ end
54
+
55
+ context "connected to the internet with a slow connection" do
56
+ before do
57
+ stub_request(:any, /.*yourkarma.com\/dashboard.*/).to_raise(Timeout::Error.new "Fail")
58
+ end
59
+ it { should be 1 }
60
+ end
61
+
62
+ context "connected to a hotspot without an assigned WAN IP address" do
63
+ before do
64
+ stub_request(:any, /.*yourkarma.com\/dashboard.*/).to_raise(Timeout::Error.new "Fail")
65
+ end
66
+ it { should be 1 }
67
+ end
68
+
69
+ context "not connected to a hotspot" do
70
+ before do
71
+ stub_request(:any, /.*yourkarma.com.*/).to_raise(Timeout::Error.new "Fail")
72
+ end
73
+ it { should be 1 }
74
+ end
75
+ end
76
+ end
77
+
@@ -0,0 +1,22 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require File.join(File.expand_path(File.dirname(__FILE__)), "../lib/yourkarma.rb")
9
+
10
+ Dir[File.join(File.expand_path(File.dirname(__FILE__)), "support/**/*.rb")].each { |f| require f }
11
+
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+ end
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :have_line_matching do |expected|
2
+ match do |actual|
3
+ actual.readlines.any? {|line| Regexp.new(expected) =~ line }
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'vcr'
2
+
3
+ VCR.configure do |c|
4
+ c.hook_into :webmock
5
+ c.cassette_library_dir = 'spec/cassettes'
6
+
7
+ c.default_cassette_options = {
8
+ record: :new_episodes
9
+ }
10
+
11
+ c.configure_rspec_metadata!
12
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe YourKarma::Benchmarker do
4
+
5
+ describe "#options" do
6
+ subject { YourKarma::Benchmarker.new.options }
7
+ its([:timeout]) { should eq 10 }
8
+ its([:benchmark]) { should match "yourkarma.com" }
9
+ end
10
+
11
+ describe "#benchmark" do
12
+ let(:benchmarker) { YourKarma::Benchmarker.new(benchmark: "example.com") }
13
+ let(:response) { double :response }
14
+ subject { benchmarker.benchmark }
15
+ before do
16
+ Net::HTTP.stub(:get_response) { response }
17
+ end
18
+
19
+ it "makes request to url" do
20
+ subject
21
+ uri = URI('example.com')
22
+ expect(Net::HTTP).to have_received(:get_response).with(uri)
23
+ end
24
+
25
+ it "returns execution time" do
26
+ Benchmark.stub(:realtime) { 123.45 }
27
+ subject.should eq 123.45
28
+ end
29
+
30
+ context "timing out" do
31
+ before do
32
+ Timeout.stub(:timeout) { raise Timeout::Error }
33
+ end
34
+
35
+ it "raises ConnectionError" do
36
+ expect(-> { subject }).to raise_error YourKarma::Benchmarker::ConnectionError
37
+ end
38
+ end
39
+
40
+ context "with an HTTP error" do
41
+ before do
42
+ Net::HTTP.stub(:get_response) { raise SocketError }
43
+ end
44
+
45
+ it "raises ConnectionError" do
46
+ expect(-> { subject }).to raise_error YourKarma::Benchmarker::ConnectionError
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ describe "YourKarma::CLI::ArgumentParser" do
4
+ subject(:cli) { YourKarma::CLI::ArgumentParser.new }
5
+
6
+ describe "#parse" do
7
+ let(:arguments) { ["--url", "http://example.com"] }
8
+ subject { cli.parse(arguments) }
9
+ its([:verbose]) { should be_false }
10
+ its([:url]) { should eq "http://example.com"}
11
+
12
+ context "help argument" do
13
+ let(:arguments) { ["--help"] }
14
+ it "should raise InvalidOption exception" do
15
+ expect(-> { subject }).to raise_error YourKarma::CLI::ArgumentParser::InvalidOption
16
+ end
17
+ end
18
+
19
+ context "invalid arguments" do
20
+ let(:arguments) { ["--WRONG"] }
21
+ it "should raise InvalidOption exception" do
22
+ expect(-> { subject }).to raise_error YourKarma::CLI::ArgumentParser::InvalidOption
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+
3
+ describe "CLI::Reporter" do
4
+ let(:arguments) { { verbose: true } }
5
+ let(:reporter) { YourKarma::CLI::Reporter.new arguments }
6
+
7
+ describe "#options" do
8
+ subject { reporter.options }
9
+
10
+ its([:io]) { should eq STDOUT }
11
+ its([:verbose]) { should be true }
12
+ end
13
+ end
14
+
15
+ describe "CLI::Reporter", "#report_on" do
16
+ let(:io) { StringIO.new }
17
+ let(:device) { YourKarma::Device.new ssid: "My Karma" }
18
+ let(:benchmark) { 123.45 }
19
+ let(:reporter) { YourKarma::CLI::Reporter.new({ io: io, verbose: true }) }
20
+ let!(:status) { reporter.report_on(device, benchmark) }
21
+ subject { status }
22
+
23
+ it { should be 0 }
24
+
25
+ describe "io" do
26
+ subject { io.string }
27
+ it { should match "My Karma" }
28
+ it { should match /123.5/ }
29
+ end
30
+ end
31
+
32
+ describe "CLI::Reporter", "#report_connectivity_failure" do
33
+ let(:io) { StringIO.new }
34
+ let(:reporter) { YourKarma::CLI::Reporter.new({ io: io }) }
35
+ let(:message) { "Whoops" }
36
+ let!(:status) { reporter.report_connectivity_failure(message) }
37
+ subject { status }
38
+
39
+ it { should be 1 }
40
+
41
+ describe "io" do
42
+ subject { io.string }
43
+ it { should match "Not connected" }
44
+ end
45
+ end
46
+
47
+ describe "CLI::Reporter", "#report_benchmark_failure_on" do
48
+ let(:io) { StringIO.new }
49
+ let(:device) { YourKarma::Device.new ssid: "My Karma", ipaddress: "192.168.1.1" }
50
+ let(:reporter) { YourKarma::CLI::Reporter.new({ io: io }) }
51
+ let!(:status) { reporter.report_benchmark_failure_on(device) }
52
+ subject { status }
53
+
54
+ it { should be 1 }
55
+
56
+ describe "io" do
57
+ subject { io.string }
58
+ it { should match "My Karma" }
59
+ it { should match "slow" }
60
+ end
61
+ end
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe "CLI", ".new" do
4
+ let(:options) { { verbose: true } }
5
+ subject { YourKarma::CLI.new options }
6
+ its(:options) { should eq options }
7
+ end
8
+
9
+ describe "CLI", ".run" do
10
+ it "should parse command line arguments, instantiate CLI instance and run it" do
11
+ io = double(:io)
12
+ arguments = double(:arguments)
13
+ options = {}
14
+ argument_parser = double(:argument_parser, parse: options )
15
+ exit_code = double(:exit_code)
16
+ cli = double(:cli, run: exit_code)
17
+ YourKarma::CLI::ArgumentParser.stub(:new) { argument_parser }
18
+ YourKarma::CLI.stub(:new) { cli }
19
+ YourKarma::CLI.run io, arguments
20
+ expect(cli).to have_received(:run).with { { io: io } }
21
+ end
22
+ end
23
+
24
+ describe "CLI", "#run" do
25
+ it "should fetch status from device, benchmark speed and report" do
26
+ client = double(:client, get: {})
27
+ device = double(:device)
28
+ benchmark = double(:benchmark)
29
+ benchmarker = double(:benchmarker, benchmark: benchmark)
30
+ exit_code = double(:exit_code)
31
+ reporter = double(:reporter, report_on: exit_code)
32
+ YourKarma::Client.stub(:new) { client }
33
+ YourKarma::Device.stub(:new) { device }
34
+ YourKarma::Benchmarker.stub(:new) { benchmarker }
35
+ YourKarma::CLI::Reporter.stub(:new) { reporter }
36
+ cli = YourKarma::CLI.new
37
+ response = cli.run
38
+ expect(reporter).to have_received(:report_on).with(device, benchmark)
39
+ expect(response).to eq(exit_code)
40
+ end
41
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe YourKarma::Client do
4
+ describe "#options" do
5
+ let(:client) { YourKarma::Client.new }
6
+ subject { client.options }
7
+ its([:url]) { should match "yourkarma.com" }
8
+ end
9
+
10
+ describe "#get" do
11
+ let(:client) { YourKarma::Client.new(url: "example.com") }
12
+ let(:response) { double :response, body: '{"device": {"foo": "bar"}}' }
13
+ subject { client.get }
14
+ before do
15
+ Net::HTTP.stub(:get_response) { response }
16
+ end
17
+
18
+ its(['foo']) { should eq 'bar' }
19
+
20
+ it "makes request to url" do
21
+ subject
22
+ uri = URI('example.com')
23
+ expect(Net::HTTP).to have_received(:get_response).with(uri)
24
+ end
25
+
26
+ context "timing out" do
27
+ before do
28
+ Net::HTTP.stub(:get_response) { raise Timeout::Error }
29
+ end
30
+
31
+ it "raises ConnectionError" do
32
+ expect(-> { subject }).to raise_error YourKarma::Client::ConnectionError
33
+ end
34
+ end
35
+
36
+ context "with an HTTP error" do
37
+ before do
38
+ Net::HTTP.stub(:get_response) { raise SocketError }
39
+ end
40
+
41
+ it "raises ConnectionError" do
42
+ expect(-> { subject }).to raise_error YourKarma::Client::ConnectionError
43
+ end
44
+ end
45
+
46
+ context "with bad JSON" do
47
+ let(:response) { double :response, body: '{ WRONG }' }
48
+
49
+ it "raises BadResponseError" do
50
+ expect(-> { subject }).to raise_error YourKarma::Client::BadResponseError
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe YourKarma::Device do
4
+ let(:attrs) { { 'ssid' => "My Karma", 'ipaddress' => "127.0.0.1" } }
5
+ subject { YourKarma::Device.new attrs }
6
+
7
+ its(:ssid) { should eq "My Karma" }
8
+ its(:ipaddress) { should eq "127.0.0.1" }
9
+
10
+ end
11
+
12
+ describe YourKarma::Device, "#valid_ipaddress?" do
13
+ context "with a real IP address" do
14
+ subject { YourKarma::Device.new 'ipaddress' => "127.0.0.1" }
15
+ its(:valid_ipaddress?) { should be_true }
16
+ end
17
+ context "with a real IP address" do
18
+ subject { YourKarma::Device.new 'ipaddress' => "N/A" }
19
+ its(:valid_ipaddress?) { should be_false }
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yourkarma/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yourkarma"
8
+ spec.version = YourKarma::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.authors = ["Greg Sterndale"]
11
+ spec.email = ["gsterndale@gmail.com"]
12
+ spec.summary = %q{Karma hotspot status client & CLI}
13
+ spec.description = %q{Determine connectivity, battery and connection speed for your device.}
14
+ spec.homepage = "http://github.com/gsterndale/yourkarma"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", "~> 2.1"
25
+ spec.add_development_dependency "webmock", "~> 1.15"
26
+ spec.add_development_dependency "vcr", "~> 2.6"
27
+
28
+ # Release every merge to master as a prerelease
29
+ spec.version = "#{spec.version}.pre#{ENV['TRAVIS_BUILD_NUMBER']}" if ENV['TRAVIS']
30
+ end