class Ubidots::ServerBridge

Public Class Methods

new(api_key=nil, token=nil, base_url=nil) click to toggle source
# File lib/ubidots/server_bridge.rb, line 4
def initialize(api_key=nil, token=nil, base_url=nil)
  @base_url = base_url ? base_url : Ubidots::Constants::API_URL
  if api_key
    @api_key = api_key
    set_api_key_header
    get_token
    set_token_header
  elsif token
    @api_key = nil
    @token = token
    set_token_header
  end
end

Public Instance Methods

delete(endpoint) click to toggle source
# File lib/ubidots/server_bridge.rb, line 82
def delete(endpoint)
  headers = @token_header
  RestClient.delete "#{@base_url}#{endpoint}", headers
end
get(endpoint) click to toggle source
# File lib/ubidots/server_bridge.rb, line 62
def get(endpoint)
  headers = @token_header
  response = RestClient.get "#{@base_url}#{endpoint}", headers
  return JSON.parse(response.body)
end
get_with_url(url) click to toggle source
# File lib/ubidots/server_bridge.rb, line 68
def get_with_url(url)
  headers = @token_header
  response = RestClient.get url, headers
  return JSON.parse(response.body)
end
post(endpoint, data) click to toggle source
# File lib/ubidots/server_bridge.rb, line 75
def post(endpoint, data)
  headers = @token_header
  data = prepare_data(data)
  response = RestClient.post "#{@base_url}#{endpoint}", data, headers
  return JSON.parse(response.body)
end
post_with_apikey(endpoint) click to toggle source
# File lib/ubidots/server_bridge.rb, line 56
def post_with_apikey(endpoint)
  headers = @apikey_header
  response = RestClient.post "#{@base_url}#{endpoint}", {}, headers
  return JSON.parse(response.body)
end
transform_to_datasource_objects(raw_items) click to toggle source
# File lib/ubidots/server_bridge.rb, line 40
def transform_to_datasource_objects(raw_items)
  datasources = []
  raw_items.each_with_index do |raw_item, i|
    datasources[i] = Ubidots::Datasource.new(self, raw_item)
  end
  return datasources
end
transform_to_variable_objects(raw_items) click to toggle source
# File lib/ubidots/server_bridge.rb, line 48
def transform_to_variable_objects(raw_items)
  variables = []
  raw_items.each_with_index do |raw_item, i|
    variables[i] = Ubidots::Variable.new(self, raw_item)
  end
  return variables
end

Private Instance Methods

get_token() click to toggle source
# File lib/ubidots/server_bridge.rb, line 20
def get_token
  endpoint = "auth/token/"
  response = post_with_apikey endpoint
  @token = response['token']
end
prepare_data(data) click to toggle source
# File lib/ubidots/server_bridge.rb, line 34
def prepare_data(data)
  return data
end
set_api_key_header() click to toggle source
# File lib/ubidots/server_bridge.rb, line 26
def set_api_key_header
  @apikey_header = { 'X-UBIDOTS-APIKEY' => @api_key }
end
set_token_header() click to toggle source
# File lib/ubidots/server_bridge.rb, line 30
def set_token_header
  @token_header = { 'X-AUTH-TOKEN' => @token }
end