module Transfluent::ApiHelper

Public Instance Methods

api_uri(method) click to toggle source
# File lib/transfluent/api_helper.rb, line 6
def api_uri method
  method = '/' + method unless method[0] == '/'
  url = @_ROOT_URL || 'https://transfluent.com/v2' 
  URI(url + method)
end
execute_get(uri, query = nil) click to toggle source
# File lib/transfluent/api_helper.rb, line 12
def execute_get uri, query = nil
  unless query.nil?
    uri.query = URI.encode_www_form query
  end

  parse_response(Net::HTTP.get(uri))
end
execute_post(uri, data) click to toggle source
# File lib/transfluent/api_helper.rb, line 20
def execute_post uri, data
  parse_response(Net::HTTP.post_form(uri, data).body)
end
parse_response(response) click to toggle source
# File lib/transfluent/api_helper.rb, line 24
def parse_response response
  if response.is_a? String then
    json = JSON.parse(response)
  else
    json = response
  end
  
  if json["status"] == "OK" 
    response = json['response']

    if response.is_a? Hash
      symbolize_hash response
    elsif response.is_a? Array
      symbolize_array response
    else
      response
    end
  elsif json["status"] == "ERROR"
    raise Transfluent::ApiError.new json['error']['type'], json['error']['message']
  end
end
symbolize_array(array) click to toggle source
# File lib/transfluent/api_helper.rb, line 54
def symbolize_array array
  array.inject([]) do |memo, v|
    memo.push(if v.is_a? Hash then symbolize_hash v else v end)
    memo
  end
end
symbolize_hash(hash) click to toggle source

Convert string keys in a hash to symbols recursively

# File lib/transfluent/api_helper.rb, line 47
def symbolize_hash hash
  hash.inject({}) do |memo,(k,v)| 
    memo[(k.to_sym rescue k) || k] = if v.is_a? Hash then symbolize_hash v else v end
    memo
  end 
end