freshdesk-api-client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd73cd89e68bb221ab2913c0ca2d11997e9276a2
4
+ data.tar.gz: 5eb140df6498a686d1ee7ae249af0ad96691809e
5
+ SHA512:
6
+ metadata.gz: 9a5e3c2d991d7d63145e67016674e8fb35019c20aa8c1468c7495a7c87bdf36d039da6b6f9f20c68fc1431ed1ed83221e754d8a5a61f013ee0541436ba9c6870
7
+ data.tar.gz: b1c558358bd8ffb809eb5756cf4ce86325e5156092f21c468ae9c8f4ad08c238217db762047f50ab5900e925805537e1d2165059a19da279ac84f5a7a216ad53
@@ -0,0 +1,11 @@
1
+ require 'base64'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ require_relative 'freshdesk/api/client/helper'
6
+ require_relative 'freshdesk/api/client/request'
7
+ require_relative 'freshdesk/api/client/discussion'
8
+ require_relative 'freshdesk/api/client/ticket'
9
+ require_relative 'freshdesk/api/client/user'
10
+ require_relative 'freshdesk/api/client/solution'
11
+
@@ -0,0 +1,81 @@
1
+ module Freshdesk
2
+ module Api
3
+ module Client
4
+
5
+ class Discussion
6
+
7
+ CATEGORIES = "discussions/categories".freeze
8
+ FORUMS = "discussions/forums".freeze
9
+ TOPICS = "discussions/topics".freeze
10
+ POSTS = "discussions/posts".freeze
11
+
12
+ #Initialize with your freshdesk account url and api key.
13
+ def initialize base_url, api_key
14
+ @connection = Freshdesk::Api::Client::Request.new base_url, api_key
15
+ end
16
+
17
+ #Get category by passing its id.
18
+ def get_category id
19
+ Freshdesk::Api::Client.convert_to_hash( @connection.get CATEGORIES, id )
20
+ end
21
+
22
+ #Get Forum by passing its id.
23
+ def get_forum id
24
+ Freshdesk::Api::Client.convert_to_hash( @connection.get FORUMS, id )
25
+ end
26
+
27
+ #Get topic by passing its id.
28
+ def get_topic id
29
+ Freshdesk::Api::Client.convert_to_hash( @connection.get TOPICS, id )
30
+ end
31
+
32
+ #Create a category by passing payload. Please refer https://freshdesk.com/api#forum
33
+ def create_category payload
34
+ Freshdesk::Api::Client.convert_to_hash( @connection.post CATEGORIES, payload )
35
+ end
36
+
37
+ #Create a forum by passing payload. Please refer https://freshdesk.com/api#forum
38
+ def create_forum payload
39
+ Freshdesk::Api::Client.convert_to_hash( @connection.post FORUMS, payload )
40
+ end
41
+
42
+ #Create a topic by passing payload. Please refer https://freshdesk.com/api#forum
43
+ def create_topic payload
44
+ Freshdesk::Api::Client.convert_to_hash( @connection.post TOPICS, payload )
45
+ end
46
+
47
+ #Create a post by passing payload. Please refer https://freshdesk.com/api#forum
48
+ def create_post topic_id, payload
49
+ Freshdesk::Api::Client.convert_to_hash( @connection.post POSTS, payload, topic_id, nil, "create_post" )
50
+ end
51
+
52
+ #Lists all categories
53
+ def list_categories
54
+ Freshdesk::Api::Client.convert_to_hash( @connection.get CATEGORIES )
55
+ end
56
+
57
+ #Delete category by passing its id.
58
+ def delete_category id
59
+ Freshdesk::Api::Client.delete_status_wrapper do
60
+ ( @connection.delete CATEGORIES, id ).code
61
+ end
62
+ end
63
+
64
+ #Delete Forum by passing its id.
65
+ def delete_forum id
66
+ Freshdesk::Api::Client.delete_status_wrapper do
67
+ ( @connection.delete FORUMS, id ).code
68
+ end
69
+ end
70
+
71
+ #Delete Post by passing its id. A Post cannot be deleted alone.
72
+ def delete_topic id
73
+ Freshdesk::Api::Client.delete_status_wrapper do
74
+ ( @connection.delete TOPICS, id ).code
75
+ end
76
+ end
77
+
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,18 @@
1
+ module Freshdesk
2
+ module Api
3
+ module Client
4
+ def self.convert_to_hash response
5
+ JSON.parse response.to_str
6
+ end
7
+
8
+ def self.delete_status_wrapper
9
+ response = yield if block_given?
10
+ if !response.nil? and (response >= 200 and response < 299)
11
+ true
12
+ else
13
+ false
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,98 @@
1
+ require 'rest_client'
2
+
3
+ module Freshdesk
4
+ module Api
5
+ module Client
6
+ class Request
7
+
8
+ def initialize base_url, api_key
9
+ @base_url = base_url
10
+ @auth = create_auth_header api_key
11
+ end
12
+
13
+ def get endpoint, id=nil, filters=nil, secondary_id=nil, method=nil
14
+ url = create_url endpoint, id, filters, secondary_id, method
15
+ handle_exceptions do
16
+ RestClient::Request.execute( url: url, method: :get, headers: @auth )
17
+ end
18
+ end
19
+
20
+ def post endpoint, payload, primary_id=nil, secondary_id=nil, method=nil
21
+ url = create_url endpoint, primary_id, nil, secondary_id, method
22
+ handle_exceptions do
23
+ RestClient::Request.execute(
24
+ payload: payload.to_json,
25
+ headers: @auth.merge({content_type: "application/json"}),
26
+ method: :post,
27
+ url: url
28
+ )
29
+ end
30
+ end
31
+
32
+ def put endpoint, id, payload
33
+ url = create_url endpoint, id
34
+ handle_exceptions do
35
+ RestClient::Request.execute(
36
+ payload: payload.to_json,
37
+ headers: @auth.merge({content_type: "application/json"}),
38
+ method: :put,
39
+ url: url
40
+ )
41
+ end
42
+ end
43
+
44
+ def delete endpoint, primary_id, secondary_id=nil, method=nil
45
+ url = create_url endpoint, primary_id, nil, secondary_id, method
46
+ p url
47
+ handle_exceptions do
48
+ RestClient::Request.execute( url: url, method: :delete, headers: @auth )
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def handle_exceptions
55
+ begin
56
+ yield if block_given?
57
+ rescue RestClient::NotFound
58
+ raise Freshdesk::Api::InvalidEndpointError, "Endpoint is Invalid"
59
+ rescue RestClient::InternalServerError
60
+ raise Freshdesk::Api::ServerError, "Server Error"
61
+ rescue RestClient::UnprocessableEntity
62
+ raise Freshdesk::Api::AlreadyExistsError, "Entity already exists"
63
+ rescue RestClient::Found
64
+ raise Freshdesk::Api::ConnectionError, "Connection to the server failed. Please check username/password"
65
+ rescue Exception
66
+ raise
67
+ end
68
+ end
69
+
70
+ def create_url(endpoint, id=nil, filters=nil, secondary_id=nil, method=nil)
71
+ base = @base_url.end_with?("/") ? @base_url : "#{@base_url}/"
72
+ endpoint = endpoint.end_with?("/") ? endpoint : "#{endpoint}/"
73
+
74
+ url = "#{base}#{endpoint}#{id}"
75
+ url = "#{base}helpdesk/#{endpoint}#{id}" if(endpoint.include? "tickets" or endpoint.include? "conversations")
76
+ url = "#{base}discussions/topics/#{id}/posts" if(endpoint.include? "discussions/posts" and !id.nil? and method.eql? "create_post")
77
+ url = "#{base}solution/categories/#{id}/folders" if(method.eql? "create_folder")
78
+ url = "#{base}solution/categories/#{id}/folders/#{secondary_id}/articles" if(method.eql? "create_article")
79
+ url = "#{base}solution/categories/#{id}" if(method.eql? "list_folders" or method.eql? "delete_category")
80
+ url = "#{base}solution/categories/#{id}/folders/#{secondary_id}" if(method.eql? "list_articles" or method.eql? "delete_folder")
81
+
82
+
83
+ if url.end_with?("/")
84
+ url = url.slice(0, url.length - 1)
85
+ end
86
+ return "#{url}.json?#{filters}" unless filters.nil?
87
+ "#{url}.json"
88
+ end
89
+
90
+ def create_auth_header api_key
91
+ token = Base64.encode64("#{api_key}:X")
92
+ {Authorization: "Basic #{token}"}
93
+ end
94
+
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,80 @@
1
+ module Freshdesk
2
+ module Api
3
+ module Client
4
+
5
+ class Solution
6
+
7
+ CATEGORIES = "solution/categories".freeze
8
+ FOLDERS = "solution/folders".freeze
9
+ ARTICLES = "solution/articles".freeze
10
+
11
+ #Initialize with your freshdesk account url and api key.
12
+ def initialize base_url, api_key
13
+ @connection = Freshdesk::Api::Client::Request.new base_url, api_key
14
+ end
15
+
16
+ #Create category by passing its id. Please refer https://freshdesk.com/api#solution-category
17
+ def create_category payload
18
+ Freshdesk::Api::Client.convert_to_hash( @connection.post CATEGORIES, payload )
19
+ end
20
+
21
+
22
+ #Create folder by passing its id. Please refer https://freshdesk.com/api#solution-category
23
+ def create_folder category_id, payload
24
+ Freshdesk::Api::Client.convert_to_hash( @connection.post FOLDERS, payload, category_id, nil, "create_folder" )
25
+ end
26
+
27
+ #Create article by passing its id. Please refer https://freshdesk.com/api#solution-category
28
+ def create_article category_id, topic_id, payload
29
+ Freshdesk::Api::Client.convert_to_hash( @connection.post ARTICLES, payload, category_id, topic_id, "create_article" )
30
+ end
31
+
32
+ #Get category by its id.
33
+ def get_category id
34
+ Freshdesk::Api::Client.convert_to_hash( @connection.get CATEGORIES, id )
35
+ end
36
+
37
+ #Get folder by its id.
38
+ def get_folder id
39
+ Freshdesk::Api::Client.convert_to_hash( @connection.get FOLDERS, id )
40
+ end
41
+
42
+ #Get article by its id.
43
+ def get_article id
44
+ Freshdesk::Api::Client.convert_to_hash( @connection.get ARTICLES, id )
45
+ end
46
+
47
+ #List all articles by passing the category id and folder id together.
48
+ def list_articles category_id, folder_id
49
+ Freshdesk::Api::Client.convert_to_hash( @connection.get ARTICLES, category_id, nil, folder_id, "list_articles" )
50
+ end
51
+
52
+ #List all folders by it category id.
53
+ def list_folders category_id
54
+ Freshdesk::Api::Client.convert_to_hash( @connection.get FOLDERS, category_id, nil, nil, "list_folders" )
55
+ end
56
+
57
+ #List all categories.
58
+ def list_categories
59
+ Freshdesk::Api::Client.convert_to_hash( @connection.get CATEGORIES )
60
+ end
61
+
62
+ #Delete category by its id.
63
+ def delete_category id
64
+ Freshdesk::Api::Client.delete_status_wrapper do
65
+ ( @connection.delete CATEGORIES, id, "delete_category" ).code
66
+ end
67
+ end
68
+
69
+ #Delete folder by category id and folder id.
70
+ def delete_folder category_id, id
71
+ Freshdesk::Api::Client.delete_status_wrapper do
72
+ ( @connection.delete FOLDERS, category_id, id, "delete_folder" ).code
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,63 @@
1
+ module Freshdesk
2
+ module Api
3
+ module Client
4
+ class Ticket
5
+ TICKETS = "tickets".freeze
6
+ NOTES = "conversations/note".freeze
7
+
8
+ #Initialize with your freshdesk account url and api key.
9
+ def initialize base_url, api_key
10
+ @connection = Freshdesk::Api::Client::Request.new base_url, api_key
11
+ end
12
+
13
+ #Create ticket with payload. Please refer https://freshdesk.com/api#ticket
14
+ def create_ticket payload
15
+ Freshdesk::Api::Client.convert_to_hash( @connection.post TICKETS, payload )
16
+ end
17
+
18
+ #Update a ticket by passing its id and payload. Please refer https://freshdesk.com/api#ticket
19
+ def update_ticket id, payload
20
+ Freshdesk::Api::Client.convert_to_hash( @connection.put TICKETS, id, payload )
21
+ end
22
+
23
+ #Delete a ticket by its id.
24
+ def delete_ticket id
25
+ Freshdesk::Api::Client.delete_status_wrapper do
26
+ ( @connection.delete TICKETS, id ).code
27
+ end
28
+ end
29
+
30
+ #Get a ticket by its id.
31
+ def get_ticket id
32
+ Freshdesk::Api::Client.convert_to_hash( @connection.get TICKETS, id )
33
+ end
34
+
35
+ #List all tickets.
36
+ def list_tickets
37
+ Freshdesk::Api::Client.convert_to_hash( @connection.get TICKETS )
38
+ end
39
+
40
+ #Check is ticket id is available by passing the id.
41
+ def is_ticket_id_available? id
42
+ begin
43
+ return false if id.to_i.zero?
44
+ get_ticket id.to_i
45
+ return false
46
+ rescue Freshdesk::Api::InvalidEndpointError
47
+ true
48
+ end
49
+ end
50
+
51
+ #Add note by passing the ticket id and payload. Please refer Please refer https://freshdesk.com/api#ticket
52
+ def add_note ticket_id, payload
53
+ begin
54
+ Freshdesk::Api::Client.convert_to_hash( @connection.post "tickets/#{ticket_id}/#{NOTES}", payload )
55
+ rescue Freshdesk::Api::InvalidEndpointError
56
+ false
57
+ end
58
+ end
59
+
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,87 @@
1
+ module Freshdesk
2
+ module Api
3
+ module Client
4
+ class User
5
+ USERS = "contacts".freeze
6
+ AGENTS = "agents".freeze
7
+
8
+ #Initialize with your freshdesk account url and api key.
9
+ def initialize base_url, api_key
10
+ @connection = Freshdesk::Api::Client::Request.new base_url, api_key
11
+ end
12
+
13
+ #Create an user with payload. Please refer https://freshdesk.com/api#user
14
+ def create_user payload
15
+ Freshdesk::Api::Client.convert_to_hash( @connection.post USERS, payload )
16
+ end
17
+
18
+ #Update an user by passing its id and payload. Please refer https://freshdesk.com/api#user
19
+ def update_user id, payload
20
+ (@connection.put USERS, id, payload).code
21
+ end
22
+
23
+ #Get user by passing an email.
24
+ def get_user_by_email email
25
+ Freshdesk::Api::Client.convert_to_hash( @connection.get USERS, nil, "query="+URI.encode("email is #{email}") )
26
+ end
27
+
28
+ #Get user by passing a valid user id.
29
+ def get_user_by_id id
30
+ Freshdesk::Api::Client.convert_to_hash( @connection.get USERS, id )
31
+ end
32
+
33
+ #Get agent by passing a valid agent id.
34
+ def get_agent_by_id id
35
+ Freshdesk::Api::Client.convert_to_hash( @connection.get AGENTS, id)
36
+ end
37
+
38
+ #Get agent by passing an email.
39
+ def get_agent_by_email email
40
+ Freshdesk::Api::Client.convert_to_hash( @connection.get AGENTS, nil, "query="+URI.encode("email is #{email}") )
41
+ end
42
+
43
+ #Get a user or an agent by simply passing the email.
44
+ def get_agent_or_user_by_email email
45
+ user = get_user_by_email email
46
+ unless user.nil? or user.empty?
47
+ { type: :user, response: user[0] }
48
+ else
49
+ agent = get_agent_by_email email
50
+ unless agent.nil? or agent.empty?
51
+ { type: :agent, response: agent[0] }
52
+ else
53
+ []
54
+ end
55
+ end
56
+ end
57
+
58
+ #Delete user by its id.
59
+ def delete_user id
60
+ Freshdesk::Api::Client.delete_status_wrapper do
61
+ ( @connection.delete USERS, id ).code
62
+ end
63
+ end
64
+
65
+ #Delete agent by its id.
66
+ def delete_agent id
67
+ begin
68
+ ( @connection.delete AGENTS, id ).code
69
+ rescue Freshdesk::Api::ServerError
70
+ 200
71
+ end
72
+ end
73
+
74
+ #List all users.
75
+ def list_users
76
+ Freshdesk::Api::Client.convert_to_hash( @connection.get USERS )
77
+ end
78
+
79
+ #List all agents.
80
+ def list_agents
81
+ Freshdesk::Api::Client.convert_to_hash( @connection.get AGENTS )
82
+ end
83
+
84
+ end
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freshdesk-api-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dineshprabu S
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.7
27
+ description: This client can be used for accessing Tickets, Users, Discussions and
28
+ Forums on Freshdesk.
29
+ email: dineshsprabu@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/freshdesk-api-client.rb
35
+ - lib/freshdesk/api/client/discussion.rb
36
+ - lib/freshdesk/api/client/helper.rb
37
+ - lib/freshdesk/api/client/request.rb
38
+ - lib/freshdesk/api/client/solution.rb
39
+ - lib/freshdesk/api/client/ticket.rb
40
+ - lib/freshdesk/api/client/user.rb
41
+ homepage: https://github.com/dineshsprabu/freshdesk-api-client
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ - lib/freshdesk/api/client
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.6.4
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Freshdesk API client for v1.
66
+ test_files: []