Module: Sensr

Defined in:
lib/sensr.rb,
lib/sensr/clip.rb,
lib/sensr/plan.rb,
lib/sensr/user.rb,
lib/sensr/camera.rb,
lib/sensr/device.rb,
lib/sensr/version.rb,
lib/sensr/api_error.rb,
lib/sensr/sensr_object.rb

Defined Under Namespace

Classes: APIError, Camera, Clip, Device, Plan, SensrObject, User

Constant Summary collapse

VERSION =
'1.0.2'
@@api_base_url =
"https://api.sensr.net/u/v3"
@@oauth_token =
nil

Class Method Summary collapse

Class Method Details

.api_base_urlObject

Return the base url for API queries



37
38
39
# File 'lib/sensr.rb', line 37

def self.api_base_url
  @@api_base_url
end

.client_idObject

Return the client ID previously assigned



49
50
51
# File 'lib/sensr.rb', line 49

def self.client_id
  @@client_id
end

.client_id=(id) ⇒ Object

Set the client ID to be used for API queries

Attributes

  • id A valid sensr API client ID



44
45
46
# File 'lib/sensr.rb', line 44

def self.client_id=(id)
  @@client_id = id
end

.client_secretObject

Return the client secret previously assigned



61
62
63
# File 'lib/sensr.rb', line 61

def self.client_secret
  @@client_secret
end

.client_secret=(secret) ⇒ Object

Set the client secret to be used for API queries

Attributes

  • secret The client secret associated with the current client ID



56
57
58
# File 'lib/sensr.rb', line 56

def self.client_secret=(secret)
  @@client_secret = secret
end

.handle_api_error(e) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/sensr.rb', line 125

def self.handle_api_error(e)
  case e.http_code
  when 400 then
    raise APIError.new("Bad request: " + e.http_body.to_s)
  when 401
    raise APIError.new("Access denied: " + e.http_body.to_s)
  when 404 
    raise APIError.new("Resource not found: " + e.http_body.to_s)
  end

  begin
    json = MultiJson.load(e.http_body)
  rescue MultiJson::DecodeError
    raise APIError.new("Invalid JSON returned by API (#{e.http_code}: #{e.http_body.inspect})", response.code, response.body)
  end

  if json["error"].to_s.strip.size > 0
    raise APIError.new(json)
  end

  json
end

.oauth_tokenObject

Get the oauth token previously assigned



32
33
34
# File 'lib/sensr.rb', line 32

def self.oauth_token
  @@oauth_token
end

.oauth_token=(token) ⇒ Object

Set the oauth token to be used for future API calls

Attributes

  • token A valid sensr oauth token



27
28
29
# File 'lib/sensr.rb', line 27

def self.oauth_token=(token)
  @@oauth_token = token
end

.oauth_token_for_user(username, password) ⇒ Object

Fetch the oauth token for a specific user, using previously assigned client id and secret. Also assigns the oauth_token to be used for all future API queries.

Attributes

  • username Username

  • password Password



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sensr.rb', line 70

def self.oauth_token_for_user(username, password)
  params = {
    "grant_type" => "password",
    "client_id" => self.client_id,
    "client_secret" => self.client_secret,
    "scope" => "user read write",
    "username" => username,
    "password" => password
  }
  response = Sensr.request(:post, "https://api.sensr.net/oauth/access_token", nil, params)
  self.oauth_token = response["access_token"]
end

.request(method, url, oauth_token, params = {}, headers = {}) ⇒ Object

Makes a request to the Sensr API

Attributes

  • method http method to be used e.g. :get,:post,:put,:delete

  • url the url to request

  • oauth_token a valid sensr oauth token to be used for the request

  • params the parameters to be passed in the request body, for puts/posts

  • headers the http headers to set by default



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sensr.rb', line 90

def self.request(method, url, oauth_token, params={}, headers={})
  oauth_token ||= self.oauth_token
  payload = nil
  if params && params.size > 0
    if [:put, :post].index(method.to_s.downcase.to_sym)
      payload = params.collect{|key, value| "#{key.to_s}=#{CGI::escape(value.to_s)}"}.join("&")
    end
  end

  request_headers = {
    :content_type => "application/x-www-form-urlencoded"
  }.merge(headers)

  request_headers[:authorization] = "OAuth " + oauth_token.to_s if oauth_token

  opts = {
    :method => method,
    :url => url,
    :headers => request_headers,
    :timeout => 120,
    :payload => payload
  }

  begin 
    response = RestClient::Request.execute(opts)
    return MultiJson.load(response.body)
  rescue RestClient::ExceptionWithResponse => e
    handle_api_error(e)
  rescue RestClient::Exception => e
    raise APIError.new(e.message)
  rescue MultiJson::DecodeError
    raise APIError.new("Invalid JSON returned by API (#{response.code}: #{response.body.inspect})", response.code, response.body)
  end
end