ruby_spark 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/core_example.rb +11 -0
- data/examples/tinker_example.rb +11 -0
- data/lib/{ruby_spark.rb → spark.rb} +4 -3
- data/lib/{ruby_spark → spark}/core.rb +22 -20
- data/lib/spark/tinker.rb +33 -0
- data/lib/{ruby_spark → spark}/version.rb +1 -1
- data/ruby_spark.gemspec +2 -2
- data/spec/{ruby_spark/core_spec.rb → spark/tinker_spec.rb} +8 -8
- data/spec/spec_helper.rb +1 -1
- metadata +9 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6bf3d029f59c6e969d9abeabd7a82d52718735e
|
4
|
+
data.tar.gz: 39aebe52e18003261d274ec1673d9c15d25ce89b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd94661e1548ef5097b5e26634e881963f5f558e3a9428ae30ea1a5f17c72205418338aac6a48d492fc1bdd9380df0edbac05e6799c7b69554687d4f3cc8974d
|
7
|
+
data.tar.gz: ab23e92c205d57b6d21f0463a950df1a6ee073edd337993eb97a31245ee64139f240da161f8d77e90ddaf37a808f30f980c398d9a7078eb87f9999a5302171ba
|
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'httparty'
|
2
2
|
|
3
|
-
require '
|
4
|
-
require '
|
3
|
+
require 'spark/version'
|
4
|
+
require 'spark/core'
|
5
|
+
require 'spark/tinker'
|
5
6
|
|
6
|
-
module
|
7
|
+
module Spark
|
7
8
|
class ConfigurationError < StandardError; end
|
8
9
|
|
9
10
|
class << self
|
@@ -1,37 +1,31 @@
|
|
1
|
-
module
|
1
|
+
module Spark
|
2
|
+
|
2
3
|
class Core
|
3
4
|
class ApiError < StandardError; end
|
4
5
|
|
5
|
-
def initialize(core_id, access_token =
|
6
|
-
raise
|
6
|
+
def initialize(core_id, access_token = Spark.access_token)
|
7
|
+
raise Spark::ConfigurationError.new("Access Token not defined") if access_token.nil?
|
7
8
|
|
8
9
|
@access_token = access_token
|
9
10
|
@core_id = core_id
|
10
11
|
end
|
11
12
|
|
12
|
-
def
|
13
|
-
response =
|
14
|
-
handle(response) do
|
15
|
-
response["return_value"] == 1
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def digital_read(pin)
|
20
|
-
response = post('digitalread', "D#{pin}")
|
13
|
+
def info
|
14
|
+
response = get("")
|
21
15
|
handle(response) do
|
22
|
-
response
|
16
|
+
response
|
23
17
|
end
|
24
18
|
end
|
25
19
|
|
26
|
-
def
|
27
|
-
response =
|
20
|
+
def variable(variable_name)
|
21
|
+
response = get(variable_name)
|
28
22
|
handle(response) do
|
29
|
-
response["
|
23
|
+
response["TEMPORARY_allTypes"]["number"]
|
30
24
|
end
|
31
25
|
end
|
32
26
|
|
33
|
-
def
|
34
|
-
response = post(
|
27
|
+
def function(function_name, arguments)
|
28
|
+
response = post(function_name, :params => arguments)
|
35
29
|
handle(response) do
|
36
30
|
response["return_value"]
|
37
31
|
end
|
@@ -39,13 +33,20 @@ module RubySpark
|
|
39
33
|
|
40
34
|
private
|
41
35
|
|
42
|
-
def post(action, params)
|
36
|
+
def post(action, params = {})
|
43
37
|
url = base_url + action
|
44
|
-
body = access_params.merge(
|
38
|
+
body = access_params.merge(params)
|
45
39
|
|
46
40
|
HTTParty.post(url, :body => body).parsed_response
|
47
41
|
end
|
48
42
|
|
43
|
+
def get(action, params = {})
|
44
|
+
url = base_url + action
|
45
|
+
query = access_params.merge(params)
|
46
|
+
|
47
|
+
HTTParty.get(url, :query => query).parsed_response
|
48
|
+
end
|
49
|
+
|
49
50
|
def handle(response, &block)
|
50
51
|
if error = error_from(response)
|
51
52
|
raise ApiError.new(error) if error.length > 0
|
@@ -70,4 +71,5 @@ module RubySpark
|
|
70
71
|
{:access_token => @access_token}
|
71
72
|
end
|
72
73
|
end
|
74
|
+
|
73
75
|
end
|
data/lib/spark/tinker.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Spark
|
2
|
+
|
3
|
+
class Tinker < Core
|
4
|
+
def digital_write(pin, message)
|
5
|
+
response = post('digitalwrite', :params => "D#{pin},#{message}")
|
6
|
+
handle(response) do
|
7
|
+
response["return_value"] == 1
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def digital_read(pin)
|
12
|
+
response = post('digitalread', :params => "D#{pin}")
|
13
|
+
handle(response) do
|
14
|
+
response["return_value"] == 1 ? "HIGH" : "LOW"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def analog_write(pin, value)
|
19
|
+
response = post('analogwrite', :params => "A#{pin},#{value}")
|
20
|
+
handle(response) do
|
21
|
+
response["return_value"] == 1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def analog_read(pin)
|
26
|
+
response = post('analogread', :params => "A#{pin}")
|
27
|
+
handle(response) do
|
28
|
+
response["return_value"]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/ruby_spark.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require '
|
5
|
-
require '
|
4
|
+
require 'spark/version'
|
5
|
+
require 'spark/core'
|
6
6
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
8
|
spec.name = "ruby_spark"
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'pry'
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe Spark::Tinker do
|
5
5
|
|
6
6
|
context "with Access Token set in config variable" do
|
7
|
-
before {
|
7
|
+
before { Spark.access_token = "good_access_token" }
|
8
8
|
subject { described_class.new("good_core_id") }
|
9
9
|
|
10
10
|
describe "#digital_write" do
|
@@ -15,12 +15,12 @@ describe RubySpark::Core do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it "returns the appropriate error when Access Token is bad" do
|
18
|
-
|
18
|
+
Spark.access_token = "bad_token"
|
19
19
|
|
20
20
|
VCR.use_cassette("bad_token") do
|
21
21
|
expect {
|
22
22
|
subject.digital_write(7, "HIGH")
|
23
|
-
}.to raise_error(
|
23
|
+
}.to raise_error(Spark::Core::ApiError)
|
24
24
|
end
|
25
25
|
|
26
26
|
VCR.use_cassette("bad_token") do
|
@@ -38,7 +38,7 @@ describe RubySpark::Core do
|
|
38
38
|
VCR.use_cassette("bad_core_id") do
|
39
39
|
expect {
|
40
40
|
subject.digital_write(7, "HIGH")
|
41
|
-
}.to raise_error(
|
41
|
+
}.to raise_error(Spark::Core::ApiError)
|
42
42
|
end
|
43
43
|
|
44
44
|
VCR.use_cassette("bad_core_id") do
|
@@ -54,7 +54,7 @@ describe RubySpark::Core do
|
|
54
54
|
VCR.use_cassette("spark_timeout") do
|
55
55
|
expect {
|
56
56
|
subject.digital_write(7, "HIGH")
|
57
|
-
}.to raise_error(
|
57
|
+
}.to raise_error(Spark::Core::ApiError)
|
58
58
|
end
|
59
59
|
|
60
60
|
VCR.use_cassette("spark_timeout") do
|
@@ -105,14 +105,14 @@ describe RubySpark::Core do
|
|
105
105
|
end
|
106
106
|
|
107
107
|
context "with no Access Token defined" do
|
108
|
-
before {
|
108
|
+
before { Spark.access_token = nil }
|
109
109
|
subject { described_class.new("good_core_id") }
|
110
110
|
|
111
111
|
it "returns proper error if Access Token is not defined" do
|
112
112
|
VCR.use_cassette("no_token") do
|
113
113
|
expect {
|
114
114
|
subject.digital_read(6)
|
115
|
-
}.to raise_error(
|
115
|
+
}.to raise_error(Spark::ConfigurationError)
|
116
116
|
end
|
117
117
|
|
118
118
|
VCR.use_cassette("no_token") do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_spark
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eli Fatsi
|
@@ -120,9 +120,12 @@ files:
|
|
120
120
|
- LICENSE.txt
|
121
121
|
- README.md
|
122
122
|
- Rakefile
|
123
|
-
-
|
124
|
-
-
|
125
|
-
- lib/
|
123
|
+
- examples/core_example.rb
|
124
|
+
- examples/tinker_example.rb
|
125
|
+
- lib/spark.rb
|
126
|
+
- lib/spark/core.rb
|
127
|
+
- lib/spark/tinker.rb
|
128
|
+
- lib/spark/version.rb
|
126
129
|
- ruby_spark.gemspec
|
127
130
|
- spec/fixtures/vcr_cassettes/analog_read.yml
|
128
131
|
- spec/fixtures/vcr_cassettes/analog_write.yml
|
@@ -132,7 +135,7 @@ files:
|
|
132
135
|
- spec/fixtures/vcr_cassettes/digital_write.yml
|
133
136
|
- spec/fixtures/vcr_cassettes/no_token.yml
|
134
137
|
- spec/fixtures/vcr_cassettes/spark_timeout.yml
|
135
|
-
- spec/
|
138
|
+
- spec/spark/tinker_spec.rb
|
136
139
|
- spec/spec_helper.rb
|
137
140
|
homepage: http://github.com/efatsi/ruby_spark
|
138
141
|
licenses:
|
@@ -167,5 +170,5 @@ test_files:
|
|
167
170
|
- spec/fixtures/vcr_cassettes/digital_write.yml
|
168
171
|
- spec/fixtures/vcr_cassettes/no_token.yml
|
169
172
|
- spec/fixtures/vcr_cassettes/spark_timeout.yml
|
170
|
-
- spec/
|
173
|
+
- spec/spark/tinker_spec.rb
|
171
174
|
- spec/spec_helper.rb
|