ninja_blocks 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.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ Ruby Ninja Blocks
2
+ ===
3
+ A simple library to help interacting with the Ninja Blocks Platform.
4
+
5
+
6
+ ## API Overview
7
+
8
+ ### User
9
+ ```ruby
10
+ # Fetch a user's profile anyformation
11
+ user.info
12
+
13
+ # Fetch a user's activity stream
14
+ user.stream
15
+
16
+ # Fetch a user's pusher channel
17
+ user.pusher_channel
18
+ ```
19
+
20
+ ### Device
21
+ ```ruby
22
+ # Fetch a user's devices, a hash map of guid => metadata
23
+ device.list
24
+
25
+ # Send `command` to device `guid`
26
+ device.actuate(guid, command)
27
+
28
+ # Subscribe to a device's data feed. Ninja Blocks will POST the requested
29
+ # device's data to the `url` provided here.
30
+ # Optionally `overwrite`s an existing callback `url`
31
+ device.subscribe(guid, url)
32
+
33
+ # Unubscribe from a device's data feed.
34
+ device.unubscribe(guid)
35
+
36
+ # Fetch any historical data about this device. Optionally specify the period's `start` and `end` timestamp.
37
+ device.data(guid, start, end)
38
+
39
+ # Fetch the last heartbeat received by this device.
40
+ device.last_heartbeat(guid)
41
+ ```
42
+ This is by no means exhaustive, and more functionality will be forthcoming.
data/example/app.rb ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'faraday'
5
+ require 'rest-client'
6
+ require 'json'
7
+ require 'ninja_blocks'
8
+ require 'chronic'
9
+
10
+ TOKEN = 'YOURTOKEN'
11
+
12
+ device = Device.new
13
+ user = User.new
14
+
15
+ # Fetch a user's devices
16
+ puts device.list()
17
+
18
+ # Send `command` to device `guid`
19
+ puts device.actuate("2712BB000674_0_0_1000", "000000").inspect
20
+
21
+ # Subscribe to a device's data feed. Optionally `overwrite`s an existing callback `url`
22
+ puts device.subscribe("2712BB000674_0_0_1000",'http://requestb.in/13ozq1w1')
23
+
24
+ # Unubscribe from a device's data feed.
25
+ puts device.unsubscribe("2712BB000674_0_0_1000")
26
+
27
+ # Return the historical data for the specified device.
28
+ puts device.data("2712BB000674_2_0_8", Chronic.parse('yesterday').to_i, Chronic.parse('today').to_i)
29
+
30
+ # Fetch any historical data about this device. Optionally specify the period's `start` and `end` timestamp.
31
+ puts device.last_heartbeat("2712BB000674_2_0_8")
32
+
33
+
34
+ #Fetch a user's profile anyformation
35
+ puts user.info()
36
+
37
+ # Fetch a user's activity stream
38
+ puts user.stream()
39
+
40
+ # Fetch a user's pusher channel
41
+ puts user.pusher_channel
@@ -0,0 +1,14 @@
1
+ require "ninja_blocks/version"
2
+ module NinjaBlocks
3
+ require 'ninja_blocks/abstruct'
4
+ require 'ninja_blocks/device'
5
+ require 'ninja_blocks/user'
6
+ def self.token
7
+ @token
8
+ end
9
+ def self.token=(token)
10
+ @token = token
11
+ end
12
+ end
13
+
14
+
@@ -0,0 +1,34 @@
1
+ module NinjaBlocks
2
+ class Abstruct
3
+ def get(url)
4
+ execute(:get,url)
5
+ end
6
+ def put(url)
7
+ execute(:put,url)
8
+ end
9
+ def post(url)
10
+ execute(:post,url)
11
+ end
12
+
13
+ def delete(url)
14
+ execute(:delete,url)
15
+ end
16
+
17
+ def connection
18
+ @connection = Faraday.new(:url => 'https://a.ninja.is')
19
+ @connection.headers['accepts'] = 'application/json'
20
+ @connection.headers['Content-Type'] = 'application/json'
21
+ @connection
22
+ end
23
+
24
+ def token
25
+ @token || NinjaBlocks.token
26
+ end
27
+
28
+ def execute(method,url)
29
+ response = connection.send(method,"#{url}?user_access_token=#{self.token}")
30
+ JSON.parse(response.body)
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,43 @@
1
+ module NinjaBlocks
2
+ class Device <Abstruct
3
+
4
+ def list
5
+ hash_of_response = get("https://api.ninja.is/rest/v0/devices")
6
+
7
+ devices = []
8
+
9
+ hash_of_response["data"].each do |d|
10
+ device_hash = {}
11
+ device_hash["guid"] = d[0]
12
+ device_hash = device_hash.merge(d[1])
13
+ devices << device_hash
14
+ end
15
+
16
+ devices
17
+ end
18
+
19
+ def actuate(guid, da)
20
+ json = JSON.dump('DA'=> dsa)
21
+ put("https://api.ninja.is/rest/v0/device/#{guid}", json)
22
+ end
23
+
24
+ def subscribe(guid, url)
25
+ json = JSON.dump('url'=> url)
26
+ post("https://api.ninja.is/rest/v0/device/#{guid}/callback", json)
27
+ end
28
+
29
+ def unsubscribe(guid)
30
+ delete("https://api.ninja.is/rest/v0/device/#{guid}/callback")
31
+ end
32
+
33
+ def data(guid, from, to)
34
+ get("https://api.ninja.is/rest/v0/device/#{guid}/data?from=#{from}&to=#{to}")
35
+ end
36
+
37
+ def last_heartbeat(guid)
38
+ get("https://api.ninja.is/rest/v0/device/#{guid}/heartbeat")
39
+ end
40
+
41
+ end
42
+ end
43
+
@@ -0,0 +1,21 @@
1
+ module NinjaBlocks
2
+ class User < Abstruct
3
+ def info
4
+ get('https://api.ninja.is/rest/v0/user')
5
+ end
6
+
7
+ def token
8
+ ::NinjaBlcoks.token
9
+ end
10
+
11
+ def stream
12
+ get('https://api.ninja.is/rest/v0/user/stream')
13
+ end
14
+
15
+ def pusher_channel
16
+ get('https://api.ninja.is/rest/v0/user/pusherchannel')
17
+ end
18
+ end
19
+ end
20
+
21
+
@@ -0,0 +1,3 @@
1
+ module NinjaBlocks
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ninja_blocks/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ninja_blocks"
8
+ gem.version = NinjaBlocks::VERSION
9
+ gem.authors = ["Marcus Schappi"]
10
+ gem.email = ["marcus@schappi.com"]
11
+ gem.description = "A simple library for talking to the Ninja Blocks Platform."
12
+ gem.summary = "Ninja Blocks!"
13
+ gem.homepage = "http://ninjablocks.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
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ninja_blocks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marcus Schappi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-03 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple library for talking to the Ninja Blocks Platform.
15
+ email:
16
+ - marcus@schappi.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - example/app.rb
23
+ - lib/ninja_blocks.rb
24
+ - lib/ninja_blocks/abstruct.rb
25
+ - lib/ninja_blocks/device.rb
26
+ - lib/ninja_blocks/user.rb
27
+ - lib/ninja_blocks/version.rb
28
+ - ninja_blocks.gemspec
29
+ homepage: http://ninjablocks.com
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.24
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Ninja Blocks!
53
+ test_files: []