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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08cc629326d77db1563de7e9e5042fda0de306f1
4
- data.tar.gz: 8746072414f6befb38835392722163d5a145205c
3
+ metadata.gz: c6bf3d029f59c6e969d9abeabd7a82d52718735e
4
+ data.tar.gz: 39aebe52e18003261d274ec1673d9c15d25ce89b
5
5
  SHA512:
6
- metadata.gz: 61c816b0357594179c4ebac680605e61f25afa850fd53a12a1c7dbcdaa16e31add5d11d3ac8d47376eac3433067eb83f58c3102212fcaaacd9217fe4261d1683
7
- data.tar.gz: b3641b8658cd5de1dd309225ca0f268031fb8877b885654ee8fb735587a9caa7bae39d34d2dfdaa4bc94fd7d1345d1a81d0394a807f3aa395474884bb0a5de3f
6
+ metadata.gz: fd94661e1548ef5097b5e26634e881963f5f558e3a9428ae30ea1a5f17c72205418338aac6a48d492fc1bdd9380df0edbac05e6799c7b69554687d4f3cc8974d
7
+ data.tar.gz: ab23e92c205d57b6d21f0463a950df1a6ee073edd337993eb97a31245ee64139f240da161f8d77e90ddaf37a808f30f980c398d9a7078eb87f9999a5302171ba
@@ -0,0 +1,11 @@
1
+ require "bundler/setup"
2
+ require "spark"
3
+
4
+ core = Spark::Core.new(ARGV[0], ARGV[1])
5
+
6
+ p core.info
7
+ p core.function("set", "1")
8
+
9
+ while true do
10
+ p core.variable("light")
11
+ end
@@ -0,0 +1,11 @@
1
+ require "bundler/setup"
2
+ require "spark"
3
+
4
+ core = Spark::Tinker.new(ARGV[0], ARGV[1])
5
+
6
+ p core.info
7
+
8
+ while true do
9
+ p core.digital_write("6", ["HIGH", "LOW"].sample)
10
+ sleep(3)
11
+ end
@@ -1,9 +1,10 @@
1
1
  require 'httparty'
2
2
 
3
- require 'ruby_spark/version'
4
- require 'ruby_spark/core'
3
+ require 'spark/version'
4
+ require 'spark/core'
5
+ require 'spark/tinker'
5
6
 
6
- module RubySpark
7
+ module Spark
7
8
  class ConfigurationError < StandardError; end
8
9
 
9
10
  class << self
@@ -1,37 +1,31 @@
1
- module RubySpark
1
+ module Spark
2
+
2
3
  class Core
3
4
  class ApiError < StandardError; end
4
5
 
5
- def initialize(core_id, access_token = RubySpark.access_token)
6
- raise RubySpark::ConfigurationError.new("Access Token not defined") if access_token.nil?
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 digital_write(pin, message)
13
- response = post('digitalwrite', "D#{pin},#{message}")
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["return_value"] == 1 ? "HIGH" : "LOW"
16
+ response
23
17
  end
24
18
  end
25
19
 
26
- def analog_write(pin, value)
27
- response = post('analogwrite', "A#{pin},#{value}")
20
+ def variable(variable_name)
21
+ response = get(variable_name)
28
22
  handle(response) do
29
- response["return_value"] == 1
23
+ response["TEMPORARY_allTypes"]["number"]
30
24
  end
31
25
  end
32
26
 
33
- def analog_read(pin)
34
- response = post('analogread', "A#{pin}")
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(:params => params)
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module RubySpark
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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 'ruby_spark/version'
5
- require 'ruby_spark/core'
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 RubySpark::Core do
4
+ describe Spark::Tinker do
5
5
 
6
6
  context "with Access Token set in config variable" do
7
- before { RubySpark.access_token = "good_access_token" }
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
- RubySpark.access_token = "bad_token"
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(RubySpark::Core::ApiError)
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(RubySpark::Core::ApiError)
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(RubySpark::Core::ApiError)
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 { RubySpark.access_token = nil }
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(RubySpark::ConfigurationError)
115
+ }.to raise_error(Spark::ConfigurationError)
116
116
  end
117
117
 
118
118
  VCR.use_cassette("no_token") do
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'vcr'
4
4
 
5
- require 'ruby_spark' # and any other gems you need
5
+ require 'spark' # and any other gems you need
6
6
 
7
7
  VCR.configure do |c|
8
8
  c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
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.2
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
- - lib/ruby_spark.rb
124
- - lib/ruby_spark/core.rb
125
- - lib/ruby_spark/version.rb
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/ruby_spark/core_spec.rb
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/ruby_spark/core_spec.rb
173
+ - spec/spark/tinker_spec.rb
171
174
  - spec/spec_helper.rb