neura-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in neura-client.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tom Caspy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Neura::Client
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'neura-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install neura-client
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,49 @@
1
+ require "neura-client/version"
2
+ require "neura-client/authenticator"
3
+ require "neura-client/pending_action"
4
+ require "neura-client/device"
5
+
6
+ module Neura
7
+ module Client
8
+ HOST="http://neura.dev"
9
+
10
+ class << self
11
+ def authenticator
12
+ @authenticator ||= Authenticator.new(email: email, password: password)
13
+ end
14
+
15
+ def headers
16
+ @headers ||= {
17
+ 'Authorization' => 'Bearer ' + access_token,
18
+ 'Accept-Language' => 'en-us',
19
+ 'Connection' => 'keep-alive',
20
+ 'Accept' => 'application/json'
21
+ }
22
+ end
23
+
24
+ def email=(email)
25
+ @email = email
26
+ end
27
+
28
+ def email
29
+ @email || "tcaspy@gmail.com"
30
+ end
31
+
32
+ def password=(password)
33
+ @password = password
34
+ end
35
+
36
+ def password
37
+ @password || "1234"
38
+ end
39
+
40
+ def access_token=(token)
41
+ @access_token = token
42
+ end
43
+
44
+ def access_token
45
+ @access_token ||= authenticator.access_token
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ require "httparty"
2
+
3
+ module Neura
4
+ module Client
5
+ class Authenticator
6
+
7
+ attr_accessor :access_token
8
+
9
+ def initialize(params = {})
10
+ @access_token = params[:token]
11
+ unless @access_token
12
+ @access_token = fetch_token(params)
13
+ end
14
+ end
15
+
16
+ def fetch_token(params)
17
+ result = HTTParty.post("#{Neura::Client::HOST}/external_api/v1/auth.json", body: params)
18
+ result["token"]
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,36 @@
1
+ module Neura
2
+ module Client
3
+ class Device
4
+
5
+ def initialize(device_id, device)
6
+ @id = device_id
7
+ @device = device
8
+ end
9
+
10
+ def update_attributes(attributes)
11
+ HTTParty.post("#{Neura::Client::HOST}/external_api/v1/nodes/#{@id}/update", body: {attributes: attributes}, headers: Neura::Client.headers)
12
+ end
13
+
14
+ def fire_event(event = {})
15
+ HTTParty.post("#{Neura::Client::HOST}/external_api/v1/nodes/#{@id}/event", body: {event: attributes}, headers: Neura::Client.headers)
16
+ end
17
+
18
+ def get_pending_actions
19
+ result = HTTParty.get("#{Neura::Client::HOST}/external_api/v1/nodes/#{@id}/pending_actions", headers: Neura::Client.headers)
20
+ [*result["pending_actions"]].map{|pa| Neura::Client::PendingAction.new(pa["pending_action"])}
21
+ end
22
+
23
+ def perform_pending_actions
24
+ actions = get_pending_actions
25
+ actions.each do |action|
26
+ if @device.perform!(action)
27
+ action.done!
28
+ else
29
+ action.reject!
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ module Neura
2
+ module Client
3
+ class PendingAction
4
+
5
+ attr_accessor :id, :command, :payload
6
+
7
+ def initialize(params)
8
+ @id = params["id"]
9
+ @command = params["command"]
10
+ @status = params["status"]
11
+ @payload = params["payload"]
12
+ end
13
+
14
+ def set_status(status)
15
+ HTTParty.post("#{Neura::Client::HOST}/external_api/v1/nodes/#{@id}/update_action", body: {status: status}, headers: Neura::Client.headers)
16
+ end
17
+
18
+ def done!
19
+ set_status(:done)
20
+ end
21
+
22
+ def reject!
23
+ set_status(:rejected)
24
+ end
25
+
26
+ def stale!
27
+ set_status(:stale)
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ class Speaker
2
+ def speak(message)
3
+ `say "#{message}"`
4
+ end
5
+
6
+ def perform!(action)
7
+ if action.command == "speak"
8
+ speak(*action.payload)
9
+ else
10
+ false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Neura
2
+ module Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'neura-client/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "neura-client"
8
+ gem.version = Neura::Client::VERSION
9
+ gem.authors = ["Tom Caspy"]
10
+ gem.email = ["tcaspy@gmail.com"]
11
+ gem.description = %q{Neura client is a ruby client for the neura's custom device api}
12
+ gem.summary = %q{Using this gem allows easy integration with the neura in order to implement a custom device controller.}
13
+ gem.homepage = "http://client.theneura.com"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency "httparty", "~> 0.8.3"
20
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neura-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Caspy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ prerelease: false
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.3
22
+ none: false
23
+ type: :runtime
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: 0.8.3
29
+ none: false
30
+ description: Neura client is a ruby client for the neura's custom device api
31
+ email:
32
+ - tcaspy@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/neura-client.rb
43
+ - lib/neura-client/authenticator.rb
44
+ - lib/neura-client/device.rb
45
+ - lib/neura-client/pending_action.rb
46
+ - lib/neura-client/speaker_of_the_house.rb
47
+ - lib/neura-client/version.rb
48
+ - neura-client.gemspec
49
+ homepage: http://client.theneura.com
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ none: false
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ none: false
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Using this gem allows easy integration with the neura in order to implement
73
+ a custom device controller.
74
+ test_files: []
75
+ has_rdoc: