class Adafruit::IO::Client

Public Class Methods

new(options) click to toggle source
# File lib/adafruit/io/client.rb, line 30
def initialize(options)
  @key = options[:key]
  @username = options[:username]

  @debug = !!options[:debug]
end

Public Instance Methods

inspect() click to toggle source

Text representation of the client, masking key

@return [String]

Calls superclass method
# File lib/adafruit/io/client.rb, line 40
def inspect
  inspected = super
  inspected = inspected.gsub! @key, "#{@key[0..3]}#{'*' * (@key.size - 3)}" if @key
  inspected
end
last_response() click to toggle source
# File lib/adafruit/io/client.rb, line 46
def last_response
  @last_response
end

Private Instance Methods

api_url(username, *args) click to toggle source
# File lib/adafruit/io/client.rb, line 86
def api_url(username, *args)
  safe_path_join *['api', 'v2', username].concat(args)
end
conn() click to toggle source
# File lib/adafruit/io/client.rb, line 63
def conn
  if api_endpoint
    url = api_endpoint
  else
    url = 'https://io.adafruit.com'
  end

  Faraday.new(:url => url) do |c|
    c.headers['X-AIO-Key'] = @key
    c.headers['Accept'] = 'application/json'
    c.headers['User-Agent'] = "AdafruitIO-Ruby/#{VERSION} (#{RUBY_PLATFORM})"
    c.request :json

    # if @debug is true, Faraday will get really noisy when making requests
    if @debug
      c.response :logger
    end

    c.use :instrumentation
    c.adapter Faraday.default_adapter
  end
end
safe_path_expand(path, current, last) click to toggle source
# File lib/adafruit/io/client.rb, line 99
def safe_path_expand(path, current, last)
  if path[0] === '/' && current != 0
    path = path[1..-1]
  end

  unless path[-1] === '/' || current == last
    path = [path, '/']
  end

  path
end
safe_path_join(*paths) click to toggle source

safely build URL paths from segments

# File lib/adafruit/io/client.rb, line 91
def safe_path_join(*paths)
  paths = paths.compact.map(&:to_s).reject(&:empty?)
  last = paths.length - 1
  paths.each_with_index.map { |path, index|
    safe_path_expand(path, index, last)
  }.join
end