class FreshServiceApiv2

Attributes

apikey[RW]
baseurl[RW]
header[RW]
password[RW]
username[RW]

Public Class Methods

new(uri) click to toggle source
# File lib/freshservice_apiv2.rb, line 8
def initialize(uri)
  @baseurl = uri.chomp('/')
end

Public Instance Methods

delete(uri) click to toggle source
# File lib/freshservice_apiv2.rb, line 23
def delete(uri)
  begin
    pick_header
    request = RestClient::Resource.new(baseurl+uri)
    response = request.delete(@header)
    return response.code,JSON.parse(response),response
  rescue RestClient::Exception => e
    return e.response.code, JSON.parse(e.response.body)
  end
end
get(uri) click to toggle source
# File lib/freshservice_apiv2.rb, line 12
def get(uri)
  begin
    pick_header
    request = RestClient::Resource.new(baseurl+uri)
    response = request.get(@header)
    return response.code,JSON.parse(response),response
  rescue RestClient::Exception => e
    return e.response.code, JSON.parse(e.response.body)
  end
end
post(uri,data) click to toggle source
# File lib/freshservice_apiv2.rb, line 45
def post(uri,data)
  begin
    pick_header
    request = RestClient::Resource.new(baseurl+uri)
    response = request.post(data,@header)
    return response.code,JSON.parse(response),response
  rescue RestClient::Exception => e
    return e.response.code, JSON.parse(e.response.body)
  end

end
update(uri,data) click to toggle source
# File lib/freshservice_apiv2.rb, line 34
def update(uri,data)
  begin
    pick_header
    request = RestClient::Resource.new(baseurl+uri)
    response = request.put(data,@header)
    return response.code,JSON.parse(response),response
  rescue RestClient::Exception => e
    return e.response.code, JSON.parse(e.response.body)
  end
end

Protected Instance Methods

authenticate() click to toggle source
# File lib/freshservice_apiv2.rb, line 64
def authenticate
  if (@username) && (@password) && (@apikey == nil)
    pass = Base64.strict_encode64("#{@username}:#{@password}")
    pass
  elsif @apikey
    encoded = Base64.strict_encode64("#{@apikey}:X")
    "Basic #{encoded}"
  end
end
pick_header() click to toggle source
# File lib/freshservice_apiv2.rb, line 59
def pick_header
  @header = @header != nil ? @header : { 'Content-Type' => 'application/json' }
  @header['Authorization'] = authenticate
end