ubidots 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7f9139ab686fb7dc1cafcf9425c0f669c4e07925
4
+ data.tar.gz: 6581b65a533950e99809d8358ade91e8148de489
5
+ SHA512:
6
+ metadata.gz: faad8b8591445655428b583e7e3facbb13393c1a9633cc4a255c281fa59849aaaf80498f41a70912ffb9b544a2ca08160bb022a82330fb335f6bad508ea3ddd4
7
+ data.tar.gz: 0dfcafe5c4f5c15bc7fb5ee27c8707312cb44f1ba1d2c691a673f60aa358234219728f0bc0268d34b92918de161ac551614e8f855f6df370c15bce24d7cc6aba
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ prueba.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create ruby-1.9.3@ubidots
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ubidots.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Federico Builes
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,86 @@
1
+ # Ubidots
2
+
3
+ Ruby gem for connecting to the [Ubidots](http://ubidots.com) API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ubidots'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ubidots
18
+
19
+ ## Usage
20
+
21
+ ### Getting an API token
22
+
23
+ The first thing you need to do to start using the API is to get a token. For this you'll need to
24
+ obtain your account API key on the website and then call `Ubidots.api_key`:
25
+
26
+ Ubidots.api_key("458cacdd594246daf5126bf106ccaaefabf1bf56")
27
+
28
+
29
+ ### Users
30
+
31
+ After getting your application token you can get a user object by calling:
32
+
33
+ user = Ubidots::User.find("username")
34
+
35
+ This user has an associated set of datasources that you can retrieve like this:
36
+
37
+ user = Ubidots::User.find("username")
38
+ user.datasources # => [...]
39
+
40
+ #### Profiles
41
+
42
+ You can see the user's profile by calling `#profile` on your user object:
43
+
44
+ user = Ubidots::User.find("username")
45
+ user.profile # => { followers: 11, following: 24...}
46
+
47
+ ### Data Sources
48
+
49
+ The datasources for the logged in user can be retrieved by doing:
50
+
51
+ Ubidots.datasources
52
+
53
+ For a specific datasources you can pass the `datasource_id` to the `#find` method:
54
+
55
+ source = Ubidots.datasources.find(datsource_id)
56
+
57
+ And you can read the variables from this datsources:
58
+
59
+ source = Ubidots.datasources.find(datsource_id)
60
+ variables = source.variables
61
+
62
+ ### Variables
63
+
64
+ To obtain a list of the variables associated to the current user you can call:
65
+
66
+ Ubidots.variables
67
+
68
+ For a specific variable just call `#find` on the variables collection and pass it the `variable_id`:
69
+
70
+ Ubidots.variables.find(variable_id)
71
+
72
+ #### Values
73
+
74
+ Each variable has a list of values. To obtain these just call `#values` on the specific variable:
75
+
76
+ variable = Ubidots.variables.find(variable_id)
77
+ values = variable.values
78
+
79
+ You can get specific information about each value by passing a `value_id`:
80
+
81
+ values = Ubidots.variables.find(variable_id).values
82
+ value = values.find(value_id)
83
+
84
+
85
+ ### Error Handling
86
+ ### Query parameters
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/ubidots.rb ADDED
@@ -0,0 +1,90 @@
1
+ require "rest_client"
2
+ require "json"
3
+ require "ubidots/version"
4
+ require "ubidots/constants"
5
+ require "ubidots/user"
6
+ require "ubidots/variable"
7
+ require "ubidots/datasource"
8
+ require "ubidots/util/collection_with_finders"
9
+ require "ubidots/util/array"
10
+
11
+ module Ubidots
12
+ include Constants
13
+
14
+ class ApiClient
15
+
16
+ def initialize(api_key=nil)
17
+ raise "Invalid API key: #{key}" if api_key.nil?
18
+ @api_key = api_key
19
+ set_api_key_header
20
+ get_token
21
+ set_token_header
22
+ end
23
+
24
+ private
25
+
26
+ def get_token
27
+ endpoint = "auth/token/"
28
+ response = post_with_apikey endpoint
29
+ @token = response['token']
30
+ end
31
+
32
+ def set_api_key_header
33
+ @apikey_header = { 'X-UBIDOTS-APIKEY' => @api_key }
34
+ end
35
+
36
+ def set_token_header
37
+ @token_header = { 'X-AUTH-TOKEN' => @token }
38
+ end
39
+
40
+ def transform_to_datasource_objects(raw_items)
41
+ datasources = []
42
+ raw_items.each_with_index do |raw_item, i|
43
+ datasources[i] = Ubidots::Datasource.new(raw_item)
44
+ end
45
+ return datasources
46
+ end
47
+
48
+ def transform_to_variable_objects(raw_items)
49
+ variables = []
50
+ raw_items.each_with_index do |raw_item, i|
51
+ variables[i] = Ubidots::Variable.new(raw_item)
52
+ end
53
+ return variables
54
+ end
55
+
56
+ public
57
+
58
+ def post_with_apikey(endpoint)
59
+ headers = @apikey_header
60
+ response = RestClient.post "#{Ubidots::Constants::API_URL}#{endpoint}", {}, headers
61
+ return JSON.parse(response.body)
62
+ end
63
+
64
+ def get(endpoint)
65
+ headers = @token_header
66
+ response = RestClient.get "#{Ubidots::Constants::API_URL}#{endpoint}", headers
67
+ return JSON.parse(response.body)
68
+ end
69
+
70
+ def get_datasources
71
+ response = get 'datasources'
72
+ raw_items = response["results"]
73
+ return transform_to_datasource_objects raw_items
74
+ end
75
+
76
+ def get_variables
77
+ response = get 'variables'
78
+ raw_items = response["results"]
79
+ return transform_to_variable_objects raw_items
80
+ end
81
+
82
+ protected
83
+
84
+ def invalid?
85
+ !defined?(@@token) || !defined?(@@key)
86
+ end
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,5 @@
1
+ module Ubidots
2
+ module Constants
3
+ API_URL = "http://things.ubidots.com/api/v1.6/"
4
+ end
5
+ end
@@ -0,0 +1,39 @@
1
+ require 'ubidots/datasource_service'
2
+ module Ubidots
3
+ class Datasource
4
+
5
+ attr_reader :id, :name, :url, :last_activity, :tags, :description, :created_at
6
+ attr_reader :owner, :parent, :context, :variables_url, :number_of_variables
7
+
8
+ def initialize(data)
9
+ @id = data["id"]
10
+ @name = data["name"]
11
+ @url = data["url"]
12
+ @last_activity = data["last_activity"]
13
+ @tags = data["tags"]
14
+ @description = data["description"]
15
+ @created_at = data["created_at"]
16
+ @owner = data["owner"]
17
+ @parent = data["parent"]
18
+ @context = data["context"]
19
+ @variables_url = data["variables_url"]
20
+ @number_of_variables = data["number_of_variables"]
21
+ end
22
+
23
+ def get_variables
24
+ endpoint = "datasources/#{@id}/variables"
25
+ response = Ubidots::ApiClient::get endpoint
26
+ raw_items = response["results"]
27
+ return Ubidots::ApiClient::transform_to_variable_objects raw_items
28
+ end
29
+
30
+
31
+ def variables
32
+ VariableService.retrieve_for_datasource(self)
33
+ end
34
+
35
+ def primary_key
36
+ name
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,17 @@
1
+ require 'ubidots/datasource'
2
+
3
+ module Ubidots
4
+ class DatasourceService
5
+ def self.retrieve(username)
6
+ url = "#{Ubidots::API_URL}/users/#{username}/datasources/"
7
+ response = RestClient.get url, { "X-UbidotsApiKey" => @@key }
8
+ JSON.parse(response.body).to_collection_with_finders
9
+ end
10
+
11
+ def self.retrieve_without_username
12
+ url = "#{Ubidots::API_URL}/users/datasources/"
13
+ response = RestClient.get url, Ubidots::default_headers
14
+ JSON.parse(response.body).to_collection_with_finders
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ require 'ubidots/user_service'
2
+
3
+ module Ubidots
4
+ class User
5
+ attr_reader :datasources, :variables, :followers, :following
6
+ attr_reader :created_at, :blog, :username, :profile
7
+
8
+ def initialize(params={})
9
+ @profile = params
10
+ @profile.each do |key, value|
11
+ instance_variable_set("@#{key}", value)
12
+ end
13
+ end
14
+
15
+ def self.find(username)
16
+ data = UserService.retrieve(username)
17
+ return nil if data.nil?
18
+ User.new(data.merge( username: username ))
19
+ end
20
+
21
+ def datasources
22
+ DatasourceService.retrieve(username).to_collection_with_finders
23
+ end
24
+
25
+ def primary_key
26
+ username
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ module Ubidots
2
+ class UserService
3
+ def self.retrieve(username)
4
+ url = "#{Constants::API_URL}/users/#{username}/"
5
+ response = RestClient.get url, { "X-UbidotsApiKey" => @@key }
6
+ JSON.parse(response.body)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def to_collection_with_finders
3
+ CollectionWithFinders.new(self)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class CollectionWithFinders < Array
2
+ def find(id)
3
+ self.find_all { |item| item.send(:primary_key) == id }.first
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ require 'ubidots/variable_service'
2
+
3
+ module Ubidots
4
+ class Variable
5
+
6
+ attr_reader :id, :name, :url, :last_activity, :tags, :description, :created_at
7
+ attr_reader :icon, :unit, :raw_datasource, :properties, :values_url, :last_value
8
+
9
+ def initialize(data)
10
+ @id = data["id"]
11
+ @name = data["name"]
12
+ @url = data["url"]
13
+ @last_activity = data["last_activity"]
14
+ @tags = data["tags"]
15
+ @description = data["description"]
16
+ @created_at = data["created_at"]
17
+ @icon = data["icon"]
18
+ @unit = data["unit"]
19
+ @raw_datasource = data["datasource"]
20
+ @properties = data["properties"]
21
+ @values_url = data["values_url"]
22
+ @last_value = data["last_value"]
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,15 @@
1
+ module Ubidots
2
+ class VariableService
3
+ def self.retrieve_from_datasource(source)
4
+ url = "#{Ubidots::API_URL}/datasources/#{source.primary_key}/variables/"
5
+ response = RestClient.get url, { "X-UbidotsApiKey" => @@key }
6
+ JSON.parse(response.body).to_collection_with_finders
7
+ end
8
+
9
+ def self.retrieve_for_current_user
10
+ url = "#{Ubidots::API_URL}/variables/"
11
+ response = RestClient.get url, { "X-UbidotsApiKey" => @@key }
12
+ JSON.parse(response.body).to_collection_with_finders
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Ubidots
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe CollectionWithFinders do
4
+ describe "#initialize" do
5
+ it "it's enumerable" do
6
+ arr = [3, 2, 1]
7
+ collection = CollectionWithFinders.new(arr)
8
+ collection.sort.should == [1, 2, 3]
9
+ end
10
+ end
11
+
12
+ describe "#find" do
13
+ it "finds the record by providing the primary key" do
14
+ arr = ["foo", "ohnoes", "nowai"].to_collection_with_finders
15
+ arr.find(3).should == "foo"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ubidots::Datasource do
4
+ describe "#variables" do
5
+ it "returns a list of variables" do
6
+ source = Ubidots::Datasource.new
7
+ Ubidots::VariableService.stub(:retrieve_for_datasource).and_return([1, 2, 3])
8
+ source.variables.should == [1, 2, 3]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ require_relative '../lib/ubidots'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = 'random'
13
+ end
14
+
15
+ @@key = "test"
16
+ @@token = "test"
17
+
18
+ # useful for testing any cases of CollectionWithFinders
19
+ class String
20
+ def primary_key
21
+ length
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ubidots do
4
+ describe ".datasources" do
5
+ it "returns a list of data sources" do
6
+ Ubidots::DatasourceService.stub(:retrieve_without_username).and_return([1, 2, 3])
7
+ Ubidots.datasources.should == [1, 2, 3]
8
+ end
9
+
10
+ it "returns a collection with finders" do
11
+ expected = ["foo", "baaar"].to_collection_with_finders
12
+ Ubidots::DatasourceService.stub(:retrieve_without_username).and_return(expected)
13
+ Ubidots.datasources.find(3).should == "foo"
14
+ end
15
+ end
16
+
17
+ describe ".variables" do
18
+ it "returns a collection with finders" do
19
+ expected = ["foo", "baaar"].to_collection_with_finders
20
+ Ubidots::VariableService.stub(:retrieve_for_current_user).and_return(expected)
21
+ Ubidots.variables.find(3)
22
+ end
23
+ end
24
+ end
data/spec/user_spec.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ubidots::User do
4
+ let(:params) { { followers: 11, following: 12 } }
5
+
6
+ before do
7
+ Ubidots::UserService.stub(:retrieve).and_return(params)
8
+ Ubidots::DatasourceService.stub(:retrieve).and_return([1, 2, 3])
9
+ end
10
+
11
+ describe ".initialize" do
12
+ it "automatically initializes all the object params" do
13
+ user = Ubidots::User.new(params)
14
+ user.followers.should == 11
15
+ user.following.should == 12
16
+ end
17
+ end
18
+
19
+ describe ".find" do
20
+ it "finds a user through the API" do
21
+ user = Ubidots::User.find("federico")
22
+ user.following.should == 12
23
+ user.username.should == "federico"
24
+ end
25
+
26
+ it "returns nil if no user is found" do
27
+ Ubidots::UserService.should_receive(:retrieve).with("federico").and_return(nil)
28
+ user = Ubidots::User.find("federico")
29
+ user.should be_nil
30
+ end
31
+ end
32
+
33
+ describe "#datasources" do
34
+ it "returns the datasources for a given user" do
35
+ user = Ubidots::User.find("federico")
36
+ user.datasources.should == [1, 2 , 3]
37
+ end
38
+ end
39
+
40
+ describe "#profile" do
41
+ it "returns the user's profile" do
42
+ user = Ubidots::User.find("federico")
43
+ user.profile.should == params.merge(username: "federico")
44
+ end
45
+ end
46
+ end
data/ubidots.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ubidots/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ubidots"
8
+ gem.version = Ubidots::VERSION
9
+ gem.authors = ["Federico Builes"]
10
+ gem.email = ["federico.builes@gmail.com"]
11
+ gem.description = "Ruby library to access the Ubidots API"
12
+ gem.summary = "Ruby library to access the Ubidots API"
13
+ gem.homepage = ""
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
+
20
+ gem.add_dependency("rest-client")
21
+ gem.add_dependency("json")
22
+ gem.add_development_dependency("rspec")
23
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ubidots
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Federico Builes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
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: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: Ruby library to access the Ubidots API
56
+ email:
57
+ - federico.builes@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rvmrc"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/ubidots.rb
70
+ - lib/ubidots/constants.rb
71
+ - lib/ubidots/datasource.rb
72
+ - lib/ubidots/datasource_service.rb
73
+ - lib/ubidots/user.rb
74
+ - lib/ubidots/user_service.rb
75
+ - lib/ubidots/util/array.rb
76
+ - lib/ubidots/util/collection_with_finders.rb
77
+ - lib/ubidots/variable.rb
78
+ - lib/ubidots/variable_service.rb
79
+ - lib/ubidots/version.rb
80
+ - spec/collection_with_finders_spec.rb
81
+ - spec/datasource_spec.rb
82
+ - spec/spec_helper.rb
83
+ - spec/ubidots_spec.rb
84
+ - spec/user_spec.rb
85
+ - ubidots.gemspec
86
+ homepage: ''
87
+ licenses: []
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Ruby library to access the Ubidots API
109
+ test_files:
110
+ - spec/collection_with_finders_spec.rb
111
+ - spec/datasource_spec.rb
112
+ - spec/spec_helper.rb
113
+ - spec/ubidots_spec.rb
114
+ - spec/user_spec.rb