sensr 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.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 3.0.0 2012-12-15
2
+
3
+ 1 major enhancement:
4
+ * Initial Release
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,5 @@
1
+ module Sensr
2
+ class APIError < StandardError
3
+
4
+ end
5
+ end
@@ -0,0 +1,112 @@
1
+ module Sensr
2
+ class Camera < SensrObject
3
+ # Find a camera by id. Only cameras owned by the current user and public cameras will be returned.
4
+ # ==== Attributes
5
+ # * +id+ The id of the camera to return
6
+ def self.find(id)
7
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + id.to_s, nil)
8
+ Camera.new(response)
9
+ end
10
+
11
+ # Get an array of cameras owned by the current user.
12
+ def self.owned
13
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/owned' , nil)
14
+ cameras = response["cameras"].collect { |c| Camera.new(c) }
15
+ cameras
16
+ end
17
+
18
+ # Get an array of cameras shared with the current user.
19
+ def self.shared
20
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/shared' , nil)
21
+ cameras = response["cameras"].collect { |c| Camera.new(c) }
22
+ cameras
23
+ end
24
+
25
+ # Create a new camera from a hash of attributes. The attributes "name", "sens", and "model" may be set.
26
+ def self.create(atts)
27
+ response = Sensr.request(:post, Sensr.api_base_url + '/cameras/create' , nil, atts)
28
+ Camera.new(response)
29
+ end
30
+
31
+ # Save any changed parameters to the camera.
32
+ def save
33
+ response = Sensr.request(:post, Sensr.api_base_url + '/cameras/update' , nil, self.attributes)
34
+ new_cam = response["camera"]
35
+ new_cam["urls"] = response["urls"]
36
+ self.attributes = new_cam
37
+ end
38
+
39
+ # Delete the camera
40
+ def destroy
41
+ response = Sensr.request(:delete, Sensr.api_base_url + '/cameras/' + self.attributes["id"].to_s , nil)
42
+ end
43
+
44
+ # Convert an epoch time to a date in the camera's timezone.
45
+ # ==== Attributes
46
+ # * +epoch_time+ A unix timestamp
47
+ def localtime(epoch_time)
48
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/localtime/" + epoch_time.to_s , nil)
49
+ end
50
+
51
+ # Get the camera's latest image
52
+ def latest
53
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/latest" , nil)
54
+ end
55
+
56
+ # Send a zoom in request to the camera
57
+ def zoomin
58
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/zoomin" , nil)
59
+ end
60
+
61
+ # Send a zoom out request to the camera
62
+ def zoomout
63
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/zoomout" , nil)
64
+ end
65
+
66
+ # Turn the camera's led on
67
+ def ledon
68
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/ledon" , nil)
69
+ end
70
+
71
+ # Turn the camera's led off
72
+ def ledoff
73
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/ledoff" , nil)
74
+ end
75
+
76
+ # Get a summary of the day specified by epoch_time. If no time is provided, return the last day which has data.
77
+ # ==== Attributes
78
+ # * +epoch_time+ A unix timestamp
79
+ def day(epoch_time = "")
80
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/day/" + epoch_time.to_s , nil)
81
+ end
82
+
83
+ # Get a summary of the hour specified by epoch_time. If no time is provided, return the last hour which has data.
84
+ # ==== Attributes
85
+ # * +epoch_time+ A unix timestamp
86
+ def hour(epoch_time = "")
87
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/hour/" + epoch_time.to_s , nil)
88
+ end
89
+
90
+ # Get a summary of the week specified by epoch_time. If no time is provided, return the last week which has data.
91
+ # ==== Attributes
92
+ # * +epoch_time+ A unix timestamp
93
+ def week(epoch_time = "")
94
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/week/" + epoch_time.to_s , nil)
95
+ end
96
+
97
+ # Get a summary of the month specified by epoch_time. If no time is provided, return the last month which has data.
98
+ # ==== Attributes
99
+ # * +epoch_time+ A unix timestamp
100
+ def month(epoch_time = "")
101
+ response = Sensr.request(:get, Sensr.api_base_url + '/cameras/' + self.id.to_s + "/month/" + epoch_time.to_s , nil)
102
+ end
103
+
104
+ # Create a new camera object from the attribute hash returned by a camera API call.
105
+ def initialize(hash) # :nodoc:
106
+ new_hash = hash["camera"]
107
+ new_hash["urls"] = hash["urls"]
108
+ super(new_hash)
109
+ end
110
+
111
+ end
112
+ end
data/lib/sensr/clip.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Sensr
2
+ class Clip < SensrObject
3
+
4
+ def initialize(id, hash)
5
+ super(hash)
6
+ end
7
+
8
+ def self.public
9
+ response = Sensr.request(:get, Sensr.api_base_url + '/clips/public.json', nil)
10
+ Clip.new(response)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ module Sensr
2
+ class Device < SensrObject
3
+
4
+ def initialize(id, hash) # :nodoc:
5
+ new_hash = hash["device"]
6
+ new_hash["id"] = id if new_hash && new_hash.size > 0 && new_hash["id"].nil?
7
+ super(new_hash)
8
+ end
9
+
10
+ # Find a device by id
11
+ # ==== Attributes
12
+ # * +id+ id of the device to be returned
13
+ def self.find(id)
14
+ response = Sensr.request(:get, Sensr.api_base_url + '/devices/' + id.to_s, nil)
15
+ Device.new(id, response)
16
+ end
17
+
18
+ # Save any updated device attributes
19
+ def save
20
+ response = Sensr.request(:put,
21
+ Sensr.api_base_url + '/devices/' + self.attributes["id"].to_s,
22
+ nil,
23
+ self.attributes)
24
+ self.attributes = response["device"]
25
+ end
26
+
27
+ end
28
+ end
data/lib/sensr/plan.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Sensr
2
+ class Plan < SensrObject
3
+
4
+ def initialize(hash) # :nodoc:
5
+ super(hash)
6
+ end
7
+
8
+ # Get the current user's plan
9
+ def self.current
10
+ response = Sensr.request(:get, Sensr.api_base_url + '/plans/current', nil)
11
+ Plan.new(response)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ module Sensr
2
+ class SensrObject # :nodoc:
3
+
4
+ def initialize(hash)
5
+ self.attributes = hash
6
+ end
7
+
8
+ def attributes
9
+ @hash.clone
10
+ end
11
+
12
+ def attributes=(hash)
13
+ @hash = hash.clone
14
+ end
15
+
16
+ def method_missing(name, *args)
17
+ return @hash[name.to_s] if @hash.key?(name.to_s)
18
+ if name.to_s =~ /(.*)=$/
19
+ att_name = $1
20
+ @hash[att_name] = args.first
21
+ return
22
+ end
23
+ super.method_missing(name)
24
+ end
25
+
26
+
27
+ end
28
+ end
data/lib/sensr/user.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Sensr
2
+ class User < SensrObject
3
+ # Get the current user
4
+ def self.me
5
+ response = Sensr.request(:get, Sensr.api_base_url + '/users/me', nil)
6
+ User.new(response)
7
+ end
8
+
9
+
10
+ def initialize(hash) # :nodoc:
11
+ new_hash = hash["user"]
12
+ new_hash["urls"] = hash["urls"]
13
+ super(new_hash)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Sensr
2
+ VERSION = '1.0.0'
3
+ end
data/lib/sensr.rb ADDED
@@ -0,0 +1,148 @@
1
+ require 'rubygems'
2
+ require 'cgi'
3
+ require 'openssl'
4
+
5
+ gem 'rest-client', '~> 1.4'
6
+ require 'rest_client'
7
+ require 'multi_json'
8
+
9
+ require 'sensr/version'
10
+ require 'sensr/sensr_object'
11
+ require 'sensr/camera'
12
+ require 'sensr/user'
13
+ require 'sensr/device'
14
+ require 'sensr/clip'
15
+ require 'sensr/plan'
16
+ require 'sensr/api_error'
17
+
18
+ module Sensr
19
+
20
+ @@api_base_url = "https://api.sensr.net/u/v3"
21
+ @@oauth_token = nil
22
+
23
+ # Set the oauth token to be used for future API calls
24
+ #
25
+ # ==== Attributes
26
+ # * +token+ A valid sensr oauth token
27
+ def self.oauth_token=(token)
28
+ @@oauth_token = token
29
+ end
30
+
31
+ # Get the oauth token previously assigned
32
+ def self.oauth_token
33
+ @@oauth_token
34
+ end
35
+
36
+ # Return the base url for API queries
37
+ def self.api_base_url
38
+ @@api_base_url
39
+ end
40
+
41
+ # Set the client ID to be used for API queries
42
+ # ==== Attributes
43
+ # * +id+ A valid sensr API client ID
44
+ def self.client_id=(id)
45
+ @@client_id = id
46
+ end
47
+
48
+ # Return the client ID previously assigned
49
+ def self.client_id
50
+ @@client_id
51
+ end
52
+
53
+ # Set the client secret to be used for API queries
54
+ # ==== Attributes
55
+ # * +secret+ The client secret associated with the current client ID
56
+ def self.client_secret=(secret)
57
+ @@client_secret = secret
58
+ end
59
+
60
+ # Return the client secret previously assigned
61
+ def self.client_secret
62
+ @@client_secret
63
+ end
64
+
65
+ # Fetch the oauth token for a specific user, using previously assigned client id and secret.
66
+ # Also assigns the oauth_token to be used for all future API queries.
67
+ # ==== Attributes
68
+ # * +username+ Username
69
+ # * +password+ Password
70
+ def self.oauth_token_for_user(username, password)
71
+ params = {
72
+ "grant_type" => "password",
73
+ "client_id" => self.client_id,
74
+ "client_secret" => self.client_secret,
75
+ "scope" => "user read write",
76
+ "username" => username,
77
+ "password" => password
78
+ }
79
+ response = Sensr.request(:post, "https://api.sensr.net/oauth/access_token", nil, params)
80
+ self.oauth_token = response["access_token"]
81
+ end
82
+
83
+ # Makes a request to the Sensr API
84
+ # ==== Attributes
85
+ # * +method+ http method to be used e.g. :get,:post,:put,:delete
86
+ # * +url+ the url to request
87
+ # * +oauth_token+ a valid sensr oauth token to be used for the request
88
+ # * +params+ the parameters to be passed in the request body, for puts/posts
89
+ # * +headers+ the http headers to set by default
90
+ def self.request(method, url, oauth_token, params={}, headers={})
91
+ oauth_token ||= self.oauth_token
92
+ payload = nil
93
+ if params && params.size > 0
94
+ if [:put, :post].index(method.to_s.downcase.to_sym)
95
+ payload = params.collect{|key, value| "#{key.to_s}=#{CGI::escape(value.to_s)}"}.join("&")
96
+ end
97
+ end
98
+
99
+ request_headers = {
100
+ :content_type => "application/x-www-form-urlencoded"
101
+ }.merge(headers)
102
+
103
+ request_headers[:authorization] = "OAuth " + oauth_token.to_s if oauth_token
104
+
105
+ opts = {
106
+ :method => method,
107
+ :url => url,
108
+ :headers => request_headers,
109
+ :timeout => 120,
110
+ :payload => payload
111
+ }
112
+
113
+ begin
114
+ response = RestClient::Request.execute(opts)
115
+ return MultiJson.load(response.body)
116
+ rescue RestClient::ExceptionWithResponse => e
117
+ handle_api_error(e)
118
+ rescue RestClient::Exception => e
119
+ raise APIError.new(e.message)
120
+ rescue MultiJson::DecodeError
121
+ raise APIError.new("Invalid JSON returned by API (#{response.code}: #{response.body.inspect})", response.code, response.body)
122
+ end
123
+ end
124
+
125
+ def self.handle_api_error(e)
126
+ case e.http_code
127
+ when 400 then
128
+ raise APIError.new("Bad request: " + e.http_body.to_s)
129
+ when 401
130
+ raise APIError.new("Access denied: " + e.http_body.to_s)
131
+ when 404
132
+ raise APIError.new("Resource not found: " + e.http_body.to_s)
133
+ end
134
+
135
+ begin
136
+ json = MultiJson.load(e.http_body)
137
+ rescue MultiJson::DecodeError
138
+ raise APIError.new("Invalid JSON returned by API (#{e.http_code}: #{e.http_body.inspect})", response.code, response.body)
139
+ end
140
+
141
+ if json["error"].to_s.strip.size > 0
142
+ raise APIError.new(json)
143
+ end
144
+
145
+ json
146
+ end
147
+
148
+ end
data/sensr.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
+
3
+ require 'sensr/version'
4
+
5
+ spec = Gem::Specification.new do |s|
6
+ s.name = 'sensr'
7
+ s.version = Sensr::VERSION
8
+ s.summary = 'Ruby bindings for the Sensr API'
9
+ s.description = 'Sensr: Watch Your Stuff.'
10
+ s.authors = ['Sensr Net, Inc']
11
+ s.email = ['info@sensr.net']
12
+ s.homepage = 'https://api.sensr.net'
13
+ s.require_paths = %w{lib}
14
+
15
+ s.add_dependency('rest-client', '~> 1.4')
16
+ s.add_dependency('multi_json', '>= 1.0.4', '< 2')
17
+
18
+ s.files = ["Gemfile", "History.txt", "sensr.gemspec", "VERSION", "lib/sensr.rb", "lib/sensr/user.rb", "lib/sensr/version.rb", "lib/sensr/device.rb", "lib/sensr/sensr_object.rb", "lib/sensr/plan.rb", "lib/sensr/clip.rb", "lib/sensr/camera.rb", "lib/sensr/api_error.rb"]
19
+ s.require_paths = ['lib']
20
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensr
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sensr Net, Inc
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: multi_json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.0.4
38
+ - - <
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: 1.0.4
49
+ - - <
50
+ - !ruby/object:Gem::Version
51
+ version: '2'
52
+ description: ! 'Sensr: Watch Your Stuff.'
53
+ email:
54
+ - info@sensr.net
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - Gemfile
60
+ - History.txt
61
+ - sensr.gemspec
62
+ - VERSION
63
+ - lib/sensr.rb
64
+ - lib/sensr/user.rb
65
+ - lib/sensr/version.rb
66
+ - lib/sensr/device.rb
67
+ - lib/sensr/sensr_object.rb
68
+ - lib/sensr/plan.rb
69
+ - lib/sensr/clip.rb
70
+ - lib/sensr/camera.rb
71
+ - lib/sensr/api_error.rb
72
+ homepage: https://api.sensr.net
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.24
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Ruby bindings for the Sensr API
96
+ test_files: []