Class: Ubidots::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ubidots.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ ApiClient

Returns a new instance of ApiClient.



16
17
18
19
20
21
22
# File 'lib/ubidots.rb', line 16

def initialize(api_key=nil)
  raise "Invalid API key: #{key}" if api_key.nil?  
  @api_key = api_key
  set_api_key_header
  get_token
  set_token_header
end

Instance Method Details

#get(endpoint) ⇒ Object



64
65
66
67
68
# File 'lib/ubidots.rb', line 64

def get(endpoint)
  headers = @token_header
  response = RestClient.get "#{Ubidots::Constants::API_URL}#{endpoint}", headers
  return JSON.parse(response.body)
end

#get_datasources ⇒ Object



70
71
72
73
74
# File 'lib/ubidots.rb', line 70

def get_datasources
  response = get 'datasources'
  raw_items = response["results"]
  return transform_to_datasource_objects raw_items
end

#get_variables ⇒ Object



76
77
78
79
80
# File 'lib/ubidots.rb', line 76

def get_variables
  response = get 'variables'
  raw_items = response["results"]
  return transform_to_variable_objects raw_items
end

#post_with_apikey(endpoint) ⇒ Object



58
59
60
61
62
# File 'lib/ubidots.rb', line 58

def post_with_apikey(endpoint)
  headers = @apikey_header
  response = RestClient.post "#{Ubidots::Constants::API_URL}#{endpoint}", {}, headers
  return JSON.parse(response.body)
end