bergcloud 1.0.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: cccf9b1846224505f77b470e172ba59df2e120ab
4
+ data.tar.gz: 4e0f75f11d0235bb5ab55e7e6d2713e998730d2d
5
+ SHA512:
6
+ metadata.gz: 6c9e04aa1e16b92a4943461ec7ded9c21de454f925d9e22f76976f8aa51d562eb53096bfc1c3a7e5c3428976a5d5de0a7be47ea6089ef93accf5ecc60614054c
7
+ data.tar.gz: 785d5e54e1aff5e5faeff0f12ab31b1954ab0a163e730c65fe439dab3d23ab2b6ebd98341c255d542ad00a04a75e88d12e81f384062c662dfe342b4dac9e74e1
data/.gitignore ADDED
@@ -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/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bergcloud.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex Forey
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.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # ![alt text](http://static.alfo.im/berg_logo.png "BERG") BERGCloud [![Build Status](https://travis-ci.org/alfo/bergcloud.svg)](https://travis-ci.org/alfo/bergcloud) [![Code Climate](https://codeclimate.com/github/alfo/bergcloud.png)](https://codeclimate.com/github/alfo/bergcloud)
2
+
3
+ A gem for interacting with [BERG's Cloud API v2](http://bergcloud.com/devcenter/api/v2/cloud-v2)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bergcloud'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bergcloud
18
+
19
+ ## Usage
20
+
21
+ Get started:
22
+
23
+ ```ruby
24
+ BERGCloud.api_token = 'your_api_token'
25
+ ```
26
+
27
+ Project methods:
28
+
29
+ ```ruby
30
+ project = BERGCloud::Project.new('your_project_id')
31
+
32
+ # Check if a project is valid
33
+ project.valid? #=> true or false
34
+
35
+ # Claim a device
36
+ project.claim('your-claim-code')
37
+
38
+ # Check a claim status
39
+ project.claim_status('your-claim-code')
40
+
41
+ # Send a command
42
+ project.send_command(:device_id => 'your_device_id', :name => 'my-command', :payload => [1, 2, 3])
43
+
44
+ # List devices
45
+ project.devices
46
+
47
+ # List events
48
+ project.events
49
+ ```
50
+
51
+ Event methods:
52
+
53
+ ```ruby
54
+ event = BERGCloud::Event.new('my-event-id')
55
+
56
+ # Get event information
57
+ event.info
58
+ ```
59
+
60
+ Command methods:
61
+
62
+ ```ruby
63
+ command = BERGCloud::Command.new('my-command-id')
64
+
65
+ # Get command info
66
+ command.info
67
+
68
+ # Remove a command from the queue
69
+ command.delete
70
+ ```
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :spec
6
+
7
+ desc "Run all specs"
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.pattern = 'spec/**/*_spec.rb'
10
+ t.rspec_opts = ["-c", "-f progress"]
11
+ end
data/bergcloud.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bergcloud/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bergcloud"
8
+ spec.version = BERGCloud::VERSION
9
+ spec.authors = ["Alex Forey"]
10
+ spec.email = ["me@alexforey.com"]
11
+ spec.description = %q{BERG Cloud API Wrapper Gem}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/alfo/bergcloud"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,91 @@
1
+ module BERGCloud
2
+
3
+ class Client
4
+
5
+ def self.api_token
6
+ @api_token
7
+ end
8
+
9
+ def self.api_token=(api_token)
10
+ @api_token = api_token
11
+ end
12
+
13
+ def self.get(path, params = {})
14
+ request(path, params, 'get')
15
+ end
16
+
17
+ def self.post(path, params = {})
18
+ request(path, params, 'post')
19
+ end
20
+
21
+ def self.delete(path, params = {})
22
+ request(path, params, 'delete')
23
+ end
24
+
25
+ def self.put(path, params = {})
26
+ request(path, params, 'put')
27
+ end
28
+
29
+ private
30
+
31
+ def self.request(path, params = {}, method)
32
+
33
+ http = Net::HTTP.new(self.host)
34
+
35
+ path = self.base_url + path
36
+
37
+ # Make a request object
38
+
39
+ case method
40
+ when 'post'
41
+ req = Net::HTTP::Post.new(path)
42
+ req.set_form_data(params)
43
+ when 'get'
44
+ req = Net::HTTP::Get.new(path)
45
+ when 'delete'
46
+ req = Net::HTTP::Delete.new(path)
47
+ when 'put'
48
+ req = Net::HTTP::Put.new(path)
49
+ req.set_form_data(params)
50
+ end
51
+
52
+
53
+ # Set the required headers
54
+ req['Accept'] = 'application/json'
55
+ req['Content-Type'] = 'application/json'
56
+ req['Berg-API-Token'] = @api_token
57
+
58
+ res = http.request(req)
59
+
60
+ case res.code.to_i
61
+ when 200, 404
62
+ res.body = JSON.parse(res.body) rescue nil
63
+ if res.body.is_a?(Hash)
64
+ res.body = Hash[res.body.map{ |k, v| [k.to_sym, v] }]
65
+ end
66
+ when 400, 422
67
+ puts "Malformed request"
68
+ raise BERGCloud::Error::RequestError, "Malformed request"
69
+ when 500
70
+ puts "Server Error"
71
+ raise BERGCloud::Error::RequestError, "Server error"
72
+ else
73
+ puts "Something else went wrong"
74
+ raise BERGCloud::Error::RequestError, "Something else went wrong. Response code was: #{res.code}"
75
+ end
76
+
77
+ return res
78
+
79
+ end
80
+
81
+ def self.base_url
82
+ "/api/v2"
83
+ end
84
+
85
+ def self.host
86
+ "api.bergcloud.com"
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,19 @@
1
+ module BERGCloud
2
+ class Command
3
+
4
+ attr_reader :command_id
5
+
6
+ def initialize(command_id)
7
+ @command_id = command_id
8
+ end
9
+
10
+ def info
11
+ BERGCloud::Client.get("/commands/#{@command_id}").body || {}
12
+ end
13
+
14
+ def delete
15
+ BERGCloud::Client.delete("/commands/#{@command_id}").body || {}
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ module BERGCloud
2
+ class Error < StandardError
3
+
4
+ class ParamsError < BERGCloud::Error
5
+
6
+ end
7
+
8
+ class RequestError < BERGCloud::Error
9
+
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module BERGCloud
2
+ class Event
3
+
4
+ attr_reader :event_id
5
+
6
+ def initialize(event_id)
7
+ @event_id = event_id
8
+ end
9
+
10
+ def info
11
+ BERGCloud::Client.get("/commands/#{@event_id}").body || {}
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ module BERGCloud
2
+ class Project
3
+
4
+ attr_reader :project_id
5
+
6
+ def initialize(project_id)
7
+ @project_id = project_id
8
+ end
9
+
10
+ def valid?
11
+ res = BERGCloud::Client.get("/projects/#{@project_id}")
12
+ res.code == "200"
13
+ end
14
+
15
+ def claim(claim_code)
16
+ BERGCloud::Client.post("/projects/#{@project_id}/claims/#{claim_code}").body
17
+ end
18
+
19
+ def claim_status(claim_code)
20
+ BERGCloud::Client.get("/projects/#{@project_id}/claims/#{claim_code}").body
21
+ end
22
+
23
+ def devices
24
+ BERGCloud::Client.get("/projects/#{@project_id}/devices").body
25
+ end
26
+
27
+ def events
28
+ BERGCloud::Client.get("/projects/#{@project_id}/events").body
29
+ end
30
+
31
+ def send_command(options)
32
+ raise BERGCloud::Error::ParamsError, "Command must include a payload, name and device_id" unless options[:payload] and options[:device_id] and options[:name]
33
+ BERGCloud::Client.post("/projects/#{@project_id}/commands", options).body
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module BERGCloud
2
+ VERSION = "1.0.0"
3
+ end
data/lib/bergcloud.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "net/http"
2
+ require "json"
3
+
4
+ require "bergcloud/version"
5
+ require "bergcloud/error"
6
+ require "bergcloud/client"
7
+ require "bergcloud/project"
8
+ require "bergcloud/event"
9
+ require "bergcloud/command"
10
+
11
+ module BERGCloud
12
+
13
+ class << self
14
+
15
+ def api_token=(api_token)
16
+ BERGCloud::Client.api_token = api_token
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe BERGCloud do
4
+ it "should store the API token correctly" do
5
+ api_token = 'something'
6
+ BERGCloud.api_token = api_token
7
+ BERGCloud::Client.api_token.should == api_token
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe BERGCloud::Command do
4
+
5
+ it "should initialize the command_id" do
6
+ command_id = 'something'
7
+ command = BERGCloud::Command.new(command_id)
8
+ command.command_id.should == command_id
9
+ end
10
+
11
+ it "should return a hash for command info" do
12
+ BERGCloud.api_token = API_TOKEN
13
+ command = BERGCloud::Command.new('12345')
14
+ command.info.should be_an_instance_of Hash
15
+ end
16
+
17
+ it "should return a hash when deleting a command" do
18
+ BERGCloud.api_token = API_TOKEN
19
+ command = BERGCloud::Command.new('12345')
20
+ command.delete.should be_an_instance_of Hash
21
+ end
22
+
23
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe BERGCloud::Event do
4
+
5
+ it "should initialize the event_id" do
6
+ event_id = 'something'
7
+ event = BERGCloud::Event.new(event_id)
8
+ event.event_id.should == event_id
9
+ end
10
+
11
+ it "should return a hash for for event info" do
12
+ BERGCloud.api_token = API_TOKEN
13
+ event = BERGCloud::Event.new('12345')
14
+ event.info.should be_an_instance_of Hash
15
+ end
16
+
17
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe BERGCloud::Project do
4
+ it "should initialize the project_id" do
5
+ project_id = 'something'
6
+ proj = BERGCloud::Project.new(project_id)
7
+ proj.project_id.should == project_id
8
+ end
9
+
10
+ it "should return true or false for valid?" do
11
+ BERGCloud.api_token = API_TOKEN
12
+ proj = BERGCloud::Project.new(PROJECT_ID)
13
+ proj.valid?.should == !!proj.valid?
14
+ end
15
+
16
+ it "should return a hash for claiming a device" do
17
+ BERGCloud.api_token = API_TOKEN
18
+ proj = BERGCloud::Project.new(PROJECT_ID)
19
+ proj.claim('test-token').should be_an_instance_of Hash
20
+ end
21
+
22
+ it "should return a hash for info about a claim" do
23
+ BERGCloud.api_token = API_TOKEN
24
+ proj = BERGCloud::Project.new(PROJECT_ID)
25
+ proj.claim_status('test-token').should be_an_instance_of Hash
26
+ end
27
+
28
+ it "should return an array for a device list" do
29
+ BERGCloud.api_token = API_TOKEN
30
+ proj = BERGCloud::Project.new(PROJECT_ID)
31
+ proj.devices.should be_an_instance_of Array
32
+ end
33
+
34
+ it "should return an array for an event list" do
35
+ BERGCloud.api_token = API_TOKEN
36
+ proj = BERGCloud::Project.new(PROJECT_ID)
37
+ proj.events.should be_an_instance_of Array
38
+ end
39
+
40
+ it "should return raise an error unless all the options are included" do
41
+ BERGCloud.api_token = API_TOKEN
42
+ proj = BERGCloud::Project.new(PROJECT_ID)
43
+ expect{proj.send_command(:a => 'b')}.to raise_error(BERGCloud::Error::ParamsError)
44
+ end
45
+
46
+ it "should not raise an error if all the options are included" do
47
+ BERGCloud.api_token = API_TOKEN
48
+ proj = BERGCloud::Project.new(PROJECT_ID)
49
+ expect{proj.send_command(:payload => [1], :device_id => '123', :name => 'go')}.to_not raise_error(BERGCloud::Error::ParamsError)
50
+ end
51
+
52
+ end
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require File.join(File.dirname(__FILE__), '/../lib/bergcloud')
5
+
6
+ API_TOKEN = ENV['BERGCLOUD_TOKEN'] || 'f9fb854c-8d7bda78-59dac7dc-9480fe63'
7
+ PROJECT_ID = ENV['PROJECT_ID'] || 'a9c752c8e0f66d2461c47422f7d64bd3'
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bergcloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Forey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-20 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: BERG Cloud API Wrapper Gem
56
+ email:
57
+ - me@alexforey.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bergcloud.gemspec
69
+ - lib/bergcloud.rb
70
+ - lib/bergcloud/client.rb
71
+ - lib/bergcloud/command.rb
72
+ - lib/bergcloud/error.rb
73
+ - lib/bergcloud/event.rb
74
+ - lib/bergcloud/project.rb
75
+ - lib/bergcloud/version.rb
76
+ - spec/bergcloud/base_spec.rb
77
+ - spec/bergcloud/command_spec.rb
78
+ - spec/bergcloud/event_spec.rb
79
+ - spec/bergcloud/project_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/alfo/bergcloud
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: BERG Cloud API Wrapper Gem
105
+ test_files:
106
+ - spec/bergcloud/base_spec.rb
107
+ - spec/bergcloud/command_spec.rb
108
+ - spec/bergcloud/event_spec.rb
109
+ - spec/bergcloud/project_spec.rb
110
+ - spec/spec_helper.rb