qntf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6c3b6bed0c5c20c842bc59aed59414071a5b85b
4
+ data.tar.gz: ab0e902a0437061cf8ea2fcebdf1b168490f742a
5
+ SHA512:
6
+ metadata.gz: 58987e760d2cdb0be6f6f961f90c9bcce4a6b9f56ca91b98cd4c7b04085556fc46e2af44e2caa302990908c1d35f1350adaf9301ce3abc7be51b53ccfbb9d340
7
+ data.tar.gz: c4e112eec97add2de2898fa3775b7ff947036e8a1c92aa8aded1bb7eda1f8ec9f74eb1868f9f462ee662ce2c53ae3501ef9ce8b38ec7477c611a97ebe9f62df2
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in qntf.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Leon Rische
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
+ # Qntf
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'qntf'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install qntf
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/qntf/fork )
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 a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,4 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'qntf/cli'
4
+ Qntf::CLI.start
@@ -0,0 +1 @@
1
+ require 'qntf/storage/json'
@@ -0,0 +1,81 @@
1
+ require 'chronic'
2
+ require 'json'
3
+ require 'csv'
4
+ require 'time'
5
+ require 'thor'
6
+
7
+ require 'qntf/storage/json'
8
+
9
+ class Qntf::CLI < Thor
10
+ desc "list", "list available datasets"
11
+
12
+ def list
13
+ datasets = Qntf::Json::Dataset.all
14
+
15
+ datasets.each do |d|
16
+ puts d.name
17
+ end
18
+ end
19
+
20
+ desc 'data [NAME]', 'get the data of a dataset'
21
+ def data(name)
22
+ dataset = Qntf::Json::Dataset.new(name)
23
+ dataset.data.each do |d|
24
+ puts d
25
+ end
26
+ end
27
+
28
+ desc "new", "create a new dataset"
29
+ def new
30
+ name = ask('Name:').chomp
31
+ fields = ask('Fields(seperated by comma):').split(',').map(&:strip)
32
+ puts "Creating dataset #{name}"
33
+ Qntf::Json::Dataset.new(name, fields)
34
+ end
35
+
36
+ desc "add_field [NAME]", "add a field to a dataset"
37
+ def add_field(name)
38
+ field = ask('Field name:').chomp
39
+ dataset = Qntf::Json::Dataset.new(name)
40
+ dataset.fields.push(field)
41
+ end
42
+
43
+ desc "fields [NAME]", "get the fields of a dataset"
44
+ def fields(name)
45
+ dataset = Qntf::Json::Dataset.new(name)
46
+ puts dataset.fields.join(', ')
47
+ end
48
+
49
+ desc "store [NAME]", "store data in dataset"
50
+ def store(name)
51
+ dataset = Qntf::Json::Dataset.new(name)
52
+ hash = {}
53
+ for field in dataset.fields
54
+ value = ask("#{field}:").chomp
55
+ hash[field] = parseNum(value) unless value.empty?
56
+ end
57
+
58
+ date = ask_time
59
+
60
+ dataset.add_datapoint(hash, date)
61
+ end
62
+
63
+ private
64
+ def parseNum(v)
65
+ v.match('\.').nil? ? Integer(v) : Float(v) rescue v.to_s
66
+ end
67
+
68
+ def ask_time
69
+ loop do
70
+ string = ask("time:").chomp
71
+ if string.empty?
72
+ return nil
73
+ end
74
+ time = Chronic.parse(string)
75
+
76
+ if ask("#{time.to_s}, continue? [y/n]").chomp == 'y'
77
+ return time.utc.iso8601(6)
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,5 @@
1
+ class Time
2
+ def self.microseconds
3
+ (Time.now.to_f * 1_000_000).to_i
4
+ end
5
+ end
@@ -0,0 +1,69 @@
1
+ require 'json'
2
+ require 'fileutils'
3
+ require 'time'
4
+
5
+ module Qntf
6
+ module Json
7
+ class Dataset
8
+ def self.all
9
+ files = Dir.glob(source + '/*.json')
10
+ files.map { |x| new(File.basename(x, '.json')) }
11
+ end
12
+
13
+ def self.source
14
+ ENV['QNTFDIR'] || "#{ENV['HOME']}/.qntf"
15
+ end
16
+
17
+ attr_accessor :name, :path, :fields, :data
18
+
19
+ def initialize(name, fields = [])
20
+ @name = name
21
+ @fields = fields
22
+ @data = {}
23
+
24
+ @path = File.expand_path("#{name}.json", self.class.source)
25
+
26
+ if File.exist?(@path) and !File.zero?(@path)
27
+ load
28
+ else
29
+ bootstrap
30
+ end
31
+ end
32
+
33
+ def add_field(field)
34
+ @fields << field
35
+ save
36
+ end
37
+
38
+ def add_datapoint(datapoint, date=nil)
39
+ date ||= Time.now.utc.iso8601(6)
40
+ data[date] = datapoint
41
+ save
42
+ end
43
+
44
+ def to_hash
45
+ {fields: @fields, data: @data}
46
+ end
47
+
48
+ def load
49
+ data = JSON.parse(File.read(@path))
50
+ @fields = data['fields']
51
+ @data = data['data']
52
+ end
53
+
54
+ def save
55
+ File.open(@path, 'w') do |file|
56
+ file.puts to_hash.to_json
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def bootstrap
63
+ FileUtils.mkdir(self.class.source) unless File.directory?(self.class.source)
64
+ FileUtils.touch(@path)
65
+ save
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module Qntf
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'qntf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "qntf"
8
+ spec.version = Qntf::VERSION
9
+ spec.authors = ["Leon Rische"]
10
+ spec.email = ["hello@l3kn.de"]
11
+ spec.summary = "JSON Time-Series Storage"
12
+ spec.description = "JSON-based Self Quantification Utility"
13
+ spec.homepage = "http://www.l3kn.de"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'thor'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,38 @@
1
+ require 'qntf/storage/json'
2
+ require 'spec_helper'
3
+
4
+ describe 'Qntf::Storage::Json', :fakefs do
5
+ subject { Qntf::Storage::Json::Dataset }
6
+
7
+ describe '.all' do
8
+ before { File.open('test.json', 'w') { |f| f.puts '{fields: [], data: {}}' }}
9
+ before { File.open('test2.json', 'w') { |f| f.puts '{fields: [], data: {}}' }}
10
+
11
+ it 'lists all datasets in the given directory' do
12
+ expect(subject.all).to eq ['test', 'test2']
13
+ end
14
+ end
15
+
16
+ describe '.new' do
17
+ it 'creates a new boilerplate dataset' do
18
+
19
+ dataset = subject.new('test3')
20
+ expect(subject.all).to include 'test3'
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'Qntf::Storage::Json.new', :fakefs do
26
+ subject { Qntf::Storage::Json::Dataset.new('test4') }
27
+
28
+ describe '.fields and .add_field' do
29
+ it 'has no fields by default' do
30
+ expect(subject.fields).to eq []
31
+ end
32
+
33
+ it 'adds fields correctly' do
34
+ subject.add_field('hodor')
35
+ expect(subject.fields).to eq ['hodor']
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ require 'rspec'
2
+
3
+ require 'fakefs/safe'
4
+ require 'fakefs/spec_helpers'
5
+
6
+ $:.unshift File.expand_path('../../lib', __FILE__)
7
+
8
+
9
+
10
+ RSpec.configure do |config|
11
+ config.include FakeFS::SpecHelpers, :fakefs
12
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qntf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leon Rische
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: JSON-based Self Quantification Utility
56
+ email:
57
+ - hello@l3kn.de
58
+ executables:
59
+ - qntf-cli
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/qntf-cli
69
+ - lib/qntf.rb
70
+ - lib/qntf/cli.rb
71
+ - lib/qntf/core_ext/time.rb
72
+ - lib/qntf/storage/json.rb
73
+ - lib/qntf/version.rb
74
+ - qntf.gemspec
75
+ - spec/qntf/storage/json_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage: http://www.l3kn.de
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: JSON Time-Series Storage
101
+ test_files:
102
+ - spec/qntf/storage/json_spec.rb
103
+ - spec/spec_helper.rb