geckoboard-ruby 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 911c6fae3e14c01c56dcee219a1a0a36ea7f6c8f
4
+ data.tar.gz: e3d5e9c25f948e6c650feb253fc724c78860b2e6
5
+ SHA512:
6
+ metadata.gz: de07437d8fec724846db64021ea804a0973cf688b1da0a2292612bc3b43e7e68be5715f6f18ac8911afdb09cb665e9d7579f88b47fff8269bd925a9669b5d8bc
7
+ data.tar.gz: 7bbaa3a2567654fe4ead80bc105d53fcd2b2a0d9bbbd582fdaa0b09fff821d3bfa5d5486d2d57e116b9f1a85b1683721217563b40b92ec9e9d2a5d496577ff30
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in geckoboard-ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Geckoboard
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # geckoboard-ruby
2
+ [![CircleCI](https://circleci.com/gh/geckoboard/geckoboard-ruby.svg?style=svg)](https://circleci.com/gh/geckoboard/geckoboard-ruby)
3
+
4
+ Ruby client library for Geckoboard (https://developer.geckoboard.com).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'geckoboard-ruby'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install geckoboard-ruby
21
+
22
+ ## Usage
23
+
24
+ ### Ping to authenticate
25
+
26
+ Verify that your API key is valid and that you can reach the Geckoboard API.
27
+
28
+ ```ruby
29
+ client = Geckoboard.client('222efc82e7933138077b1c2554439e15')
30
+ client.ping # => true
31
+ ```
32
+
33
+ ### Find or create
34
+
35
+ Verify an existing dataset or create a new one.
36
+
37
+ ```ruby
38
+ dataset = client.datasets.find_or_create('sales.gross', fields: {
39
+ amount: {
40
+ type: :number,
41
+ name: 'Amount',
42
+ },
43
+ timestamp: {
44
+ type: :datetime,
45
+ name: 'Time'
46
+ }
47
+ })
48
+ ```
49
+
50
+ ### Delete
51
+
52
+ Delete a dataset and all data therein.
53
+
54
+ ```ruby
55
+ dataset.delete # => true
56
+ ```
57
+
58
+ Delete a dataset with a given id.
59
+
60
+ ```ruby
61
+ client.datasets.delete('sales.gross') # => true
62
+ ```
63
+
64
+ ### Put
65
+
66
+ Replace all data in the dataset.
67
+
68
+ ```ruby
69
+ dataset.put([
70
+ {
71
+ timestamp: DateTime.new(2016, 1, 2, 12, 0, 0),
72
+ amount: 409
73
+ },
74
+ {
75
+ timestamp: DateTime.new(2016, 1, 3, 12, 0, 0),
76
+ amount: 164
77
+ },
78
+ ])
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
84
+
85
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+
87
+ ## Contributing
88
+
89
+ 1. Fork it ( https://github.com/geckoboard/geckoboard-ruby/fork )
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "geckoboard"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.3.1
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'geckoboard/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'geckoboard-ruby'
8
+ spec.version = Geckoboard::VERSION
9
+ spec.authors = ['Daniel Upton']
10
+ spec.email = %w[daniel.upton@geckoboard.com]
11
+
12
+ spec.summary = %q{Ruby client library for Geckoboard}
13
+ spec.homepage = 'https://github.com/geckoboard/geckoboard-ruby'
14
+ spec.license = 'MIT'
15
+
16
+ spec.required_ruby_version = '>= 2.0'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = %w[lib]
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.8'
24
+ spec.add_development_dependency 'rspec', '~> 3.4'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'webmock', '~> 2.1'
27
+ end
data/lib/geckoboard.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'cgi'
4
+
5
+ require 'geckoboard/version'
6
+ require 'geckoboard/connection'
7
+ require 'geckoboard/client'
8
+ require 'geckoboard/datasets_client'
9
+ require 'geckoboard/dataset'
10
+ require 'geckoboard/payload_formatter'
11
+ require 'geckoboard/errors'
12
+
13
+ module Geckoboard
14
+ USER_AGENT = "Geckoboard-Ruby/#{VERSION}"
15
+
16
+ def self.client(api_key)
17
+ Client.new(api_key)
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module Geckoboard
2
+ class Client
3
+ attr_reader :connection
4
+
5
+ def initialize(api_key)
6
+ @connection = Connection.new(api_key)
7
+ end
8
+
9
+ def ping
10
+ connection.get('/')
11
+ true
12
+ end
13
+
14
+ def datasets
15
+ DatasetsClient.new(connection)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,74 @@
1
+ module Geckoboard
2
+ class Connection
3
+ attr_reader :api_key
4
+
5
+ def initialize(api_key)
6
+ @api_key = api_key
7
+ end
8
+
9
+ def get(path)
10
+ request = Net::HTTP::Get.new(path)
11
+
12
+ make_request(request)
13
+ end
14
+
15
+ def delete(path)
16
+ request = Net::HTTP::Delete.new(path)
17
+
18
+ make_request(request)
19
+ end
20
+
21
+ def put(path, body)
22
+ request = Net::HTTP::Put.new(path)
23
+ request['Content-Type'] = 'application/json'
24
+ request.body = body
25
+
26
+ make_request(request)
27
+ end
28
+
29
+ private
30
+
31
+ def make_request(request)
32
+ request.basic_auth(api_key, '')
33
+ request['User-Agent'] = USER_AGENT
34
+
35
+ response = http.request(request)
36
+ check_for_errors(response)
37
+ response
38
+ end
39
+
40
+ def check_for_errors(response)
41
+ return if response.code.to_i < 400
42
+
43
+ error_message = extract_error_message(response) ||
44
+ "Server responded with unexpected status code (#{response.code})"
45
+
46
+ exception = case response
47
+ when Net::HTTPUnauthorized then UnauthorizedError
48
+ when Net::HTTPConflict then ConflictError
49
+ when Net::HTTPBadRequest then BadRequestError
50
+ else UnexpectedStatusError
51
+ end
52
+
53
+ raise exception, error_message
54
+ end
55
+
56
+ def extract_error_message(response)
57
+ return unless response_is_json? response
58
+
59
+ JSON.parse(response.body)
60
+ .fetch('error', {})
61
+ .fetch('message', nil)
62
+ end
63
+
64
+ def response_is_json?(response)
65
+ (response['Content-Type'] || '').split(';').first == 'application/json'
66
+ end
67
+
68
+ def http
69
+ http = Net::HTTP.new('api.geckoboard.com', 443)
70
+ http.use_ssl = true
71
+ http
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,25 @@
1
+ module Geckoboard
2
+ class Dataset
3
+ attr_reader :client, :id, :fields
4
+
5
+ def initialize(client, id, fields)
6
+ @client = client
7
+ @id = id
8
+ @fields = fields
9
+ end
10
+
11
+ def delete
12
+ client.delete(id)
13
+ end
14
+
15
+ def put(data)
16
+ client.put_data(id, payload_formatter.format(data))
17
+ end
18
+
19
+ private
20
+
21
+ def payload_formatter
22
+ PayloadFormatter.new(self)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ module Geckoboard
2
+ class DatasetsClient
3
+ attr_reader :connection
4
+
5
+ def initialize(connection)
6
+ @connection = connection
7
+ end
8
+
9
+ def find_or_create(dataset_id, fields: {})
10
+ path = dataset_path(dataset_id)
11
+ response = connection.put(path, { fields: fields }.to_json)
12
+
13
+ data = JSON.parse(response.body)
14
+ Dataset.new(self, data.fetch('id'), data.fetch('fields'))
15
+ end
16
+
17
+ def delete(dataset_id)
18
+ path = dataset_path(dataset_id)
19
+ connection.delete(path)
20
+ true
21
+ end
22
+
23
+ def put_data(dataset_id, data)
24
+ path = "#{dataset_path(dataset_id)}/data"
25
+ connection.put(path, { data: data }.to_json)
26
+ true
27
+ end
28
+
29
+ private
30
+
31
+ def dataset_path(dataset_id)
32
+ "/datasets/#{CGI.escape(dataset_id)}"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module Geckoboard
2
+ BaseError = Class.new(StandardError)
3
+ BadRequestError = Class.new(BaseError)
4
+ ConflictError = Class.new(BaseError)
5
+ UnauthorizedError = Class.new(BaseError)
6
+ UnexpectedStatusError = Class.new(BaseError)
7
+ end
@@ -0,0 +1,46 @@
1
+ module Geckoboard
2
+ class PayloadFormatter
3
+ attr_reader :dataset
4
+
5
+ def initialize(dataset)
6
+ @dataset = dataset
7
+ end
8
+
9
+ def format(payload)
10
+ payload.map do |item|
11
+ item.inject({}) do |hash, (field_name, value)|
12
+ field_name = field_name.to_s
13
+
14
+ value = case field_type(field_name)
15
+ when 'date' then format_date(value)
16
+ when 'datetime' then format_datetime(value)
17
+ else
18
+ value
19
+ end
20
+
21
+ hash.merge(field_name => value)
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def field_type(name)
29
+ @field_types ||= dataset.fields.inject({}) do |hash, (field_name, definition)|
30
+ hash.merge(field_name => definition.fetch('type'))
31
+ end
32
+
33
+ @field_types.fetch(name, 'string')
34
+ end
35
+
36
+ def format_date(value)
37
+ return value unless value.respond_to? :strftime
38
+ value.strftime('%Y-%m-%d')
39
+ end
40
+
41
+ def format_datetime(value)
42
+ return value unless value.respond_to? :strftime
43
+ value.strftime('%Y-%m-%dT%H:%M:%S%:z')
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Geckoboard
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geckoboard-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Upton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.1'
69
+ description:
70
+ email:
71
+ - daniel.upton@geckoboard.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - circle.yml
85
+ - geckoboard-ruby.gemspec
86
+ - lib/geckoboard.rb
87
+ - lib/geckoboard/client.rb
88
+ - lib/geckoboard/connection.rb
89
+ - lib/geckoboard/dataset.rb
90
+ - lib/geckoboard/datasets_client.rb
91
+ - lib/geckoboard/errors.rb
92
+ - lib/geckoboard/payload_formatter.rb
93
+ - lib/geckoboard/version.rb
94
+ homepage: https://github.com/geckoboard/geckoboard-ruby
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '2.0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.2.0
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Ruby client library for Geckoboard
118
+ test_files: []
119
+ has_rdoc: