Class: Freshdesk::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, params = {}, id = nil) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/freshdesk/resource.rb', line 6

def initialize(endpoint, params = {}, id = nil)
  @endpoint             = endpoint
  @params               = params
  @id                   = id
  @domain               = Freshdesk.domain
  @user_name_or_api_key = Freshdesk.user_name_or_api_key
  @password_or_x        = Freshdesk.password_or_x
  @api_version          = Freshdesk.api_version

  @resource ||= RestClient::Resource.new(
    api_url,
    @user_name_or_api_key,
    @password_or_x
  )
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



22
23
24
# File 'lib/freshdesk/resource.rb', line 22

def endpoint
  @endpoint
end

#paramsObject (readonly)

Returns the value of attribute params.



22
23
24
# File 'lib/freshdesk/resource.rb', line 22

def params
  @params
end

Instance Method Details

#api_urlObject



24
25
26
# File 'lib/freshdesk/resource.rb', line 24

def api_url
  "https://#{@domain}.freshdesk.com/api/#{@api_version}#{@endpoint}"
end

#deleteObject



44
45
46
47
48
# File 'lib/freshdesk/resource.rb', line 44

def delete
  @resource.delete(accept: "application/json")
rescue RestClient::Exception => e
  raise e, api_error_message(e)
end

#getObject



38
39
40
41
42
# File 'lib/freshdesk/resource.rb', line 38

def get
  @resource.get(accept: "application/json")
rescue RestClient::Exception => e
  raise e, api_error_message(e)
end

#json_payloadObject



28
29
30
# File 'lib/freshdesk/resource.rb', line 28

def json_payload
  @params.to_json
end

#postObject



32
33
34
35
36
# File 'lib/freshdesk/resource.rb', line 32

def post
  @resource.post(json_payload, content_type: "application/json")
rescue RestClient::Exception => e
  raise e, api_error_message(e)
end

#putObject



50
51
52
53
54
# File 'lib/freshdesk/resource.rb', line 50

def put
  @resource.put(json_payload, content_type: "application/json")
rescue RestClient::Exception => e
  raise e, api_error_message(e)
end