module Epitech::Api
Constants
- BASE_URL
JSON API URL
- VERSION
Gem version
Public Instance Methods
call_api(opts={})
click to toggle source
Used to call the webservice @param opts [Hash] Hash used as URL params, formatted by {#hash_to_req} @return What the Web API is supposed to return
# File lib/utils.rb, line 8 def call_api(opts={}) tmp = open(URI(URI.escape(BASE_URL + hash_to_req(opts)))).read tmp = tmp.split("\n").drop(1).join("\n") while tmp.start_with?(%Q(SSH connection failed.)) json = JSON.parse(tmp) raise get_error_class(json['error']) if json['error'] != 'none' json['result'] end
Private Instance Methods
get_error_class(str)
click to toggle source
# File lib/utils.rb, line 18 def get_error_class(str) to_class "Epitech::Api::Errors::#{to_camelcase(str)}" end
hash_to_req(hash)
click to toggle source
Transform a Hash to Web API URL params @param hash [Hash] @return [String] formatted from ‘hash’ as follow:
{ action: 'connect', login: 'exampl_e' } becomes "action=connect&login=exampl_e"
# File lib/utils.rb, line 35 def hash_to_req(hash) req = '' hash.each_pair do |key, val| req << key.to_s << '=' << val.to_s << '&' unless val.nil? end req.chop end
to_camelcase(str)
click to toggle source
# File lib/utils.rb, line 22 def to_camelcase(str) str.split('_').collect(&:capitalize).join end
to_class(str)
click to toggle source
# File lib/utils.rb, line 26 def to_class(str) Object.const_get str end