teamsupport 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ require 'teamsupport/base'
2
+ require 'teamsupport/creatable'
3
+ require 'teamsupport/identity'
4
+
5
+ module Teamsupport
6
+ class Product < Teamsupport::Identity
7
+ include Teamsupport::Creatable
8
+
9
+ # @return [Integer]
10
+ attr_reader :ID, :CreatorID, :ModifierID
11
+ # @return [String]
12
+ attr_reader :Name, :Description
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'teamsupport/rest/customers'
2
+ require 'teamsupport/rest/products'
3
+ require 'teamsupport/rest/tickets'
4
+
5
+ module Teamsupport
6
+ module REST
7
+ module API
8
+ # TODO: include Teamsupport::REST::Assets
9
+ # TODO: include Teamsupport::REST::Contacts
10
+ include Teamsupport::REST::Customers
11
+ # TODO: include Teamsupport::REST::Groups
12
+ include Teamsupport::REST::Products
13
+ # TODO: include Teamsupport::REST::Properties
14
+ include Teamsupport::REST::Tickets
15
+ # TODO: include Teamsupport::REST::Users
16
+ # TODO: include Teamsupport::REST::Wiki
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ require 'teamsupport/client'
2
+ require 'teamsupport/rest/api'
3
+ require 'teamsupport/rest/request'
4
+ require 'teamsupport/rest/utils'
5
+
6
+ module Teamsupport
7
+ module REST
8
+ class Client < Teamsupport::Client
9
+ include Teamsupport::REST::API
10
+
11
+ # Checks for the existence of an authentication hash on the Client
12
+ #
13
+ # @example
14
+ # teamsupport_client = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
15
+ # teamsupport_client.auth?
16
+ #
17
+ # @return [Boolean]
18
+ #
19
+ # @api public
20
+ def auth?
21
+ super
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,156 @@
1
+ require 'teamsupport/error'
2
+ require 'teamsupport/customer'
3
+ require 'teamsupport/rest/utils'
4
+ require 'teamsupport/utils'
5
+
6
+ module Teamsupport
7
+ module REST
8
+ module Customers
9
+ include Teamsupport::REST::Utils
10
+ include Teamsupport::Utils
11
+
12
+ # Returns all available customers for the TeamSupport organization
13
+ #
14
+ # @see http://help.teamsupport.com/1/en/topic/api
15
+ #
16
+ # @example
17
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
18
+ # teamsupport_api.customers()
19
+ #
20
+ # @authentication Requires Basic Authentication
21
+ #
22
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied API credentials are not valid.
23
+ #
24
+ # @return [Array<Teamsupport::Customer>]
25
+ #
26
+ # @param options [Hash] A customizable set of options.
27
+ #
28
+ # @option options [Integer] :count Specifies the number of records to retrieve.
29
+ #
30
+ # @api public
31
+ def customers(options = {})
32
+ perform_get_with_objects_from_collection('/api/json/customers.json', options, Teamsupport::Customer, :Customers)
33
+ end
34
+
35
+ # Returns a customer
36
+ #
37
+ # @example
38
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
39
+ # teamsupport_api.customer('1')
40
+ #
41
+ # @authentication Requires Basic Authentication
42
+ #
43
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
44
+ #
45
+ # @return [Teamsupport::Customer] The requested customer.
46
+ #
47
+ # @param id [Integer] A customer ID.
48
+ # @param options [Hash] A customizable set of options.
49
+ #
50
+ # @api public
51
+ def customer(id, options = {})
52
+ perform_get_with_object_from_collection("/api/json/customers/#{id}.json", options, Teamsupport::Customer, :Customer)
53
+ end
54
+
55
+ # Create a customer
56
+ #
57
+ # @example
58
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
59
+ # teamsupport_api.create_customer(Name: 'New Customer')
60
+ #
61
+ # @authentication Requires Basic Authentication
62
+ #
63
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
64
+ #
65
+ # @return [Teamsupport::Customer] The new customer.
66
+ #
67
+ # @param options [Hash] A customizable set of options.
68
+ #
69
+ # @api public
70
+ def create_customer(options = {})
71
+ perform_post_with_object_from_collection('/api/json/customers.json', options, Teamsupport::Customer, :Customer)
72
+ end
73
+
74
+ # Updates the customer
75
+ #
76
+ # @example
77
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
78
+ # teamsupport_api.update_customer('1', Name: 'Updated Customer Name')
79
+ #
80
+ # @authentication Requires Basic Authentication
81
+ #
82
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
83
+ #
84
+ # @return [Teamsupport::Customer] The updated customer.
85
+ #
86
+ # @param id [Integer] A customer ID.
87
+ # @param options [Hash] A customizable set of options.
88
+ #
89
+ # @api public
90
+ def update_customer(id, options = {})
91
+ customer_hash = customer(id).to_h
92
+ perform_put_with_object_from_collection("/api/json/customers/#{id}.json", customer_hash.merge(options), Teamsupport::Customer, :Customer)
93
+ end
94
+
95
+ # Deletes the customer
96
+ #
97
+ # @example
98
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
99
+ # teamsupport_api.delete_customer('1')
100
+ #
101
+ # @authentication Requires Basic Authentication
102
+ #
103
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
104
+ #
105
+ # @param id [Integer] A customer ID.
106
+ # @param options [Hash] A customizable set of options.
107
+ #
108
+ # @api public
109
+ def delete_customer(id, options = {})
110
+ perform_delete("/api/json/customers/#{id}.json", options)
111
+ end
112
+
113
+ # Returns the products for a customer
114
+ #
115
+ # @example
116
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
117
+ # teamsupport_api.customer_products('1')
118
+ #
119
+ # @authentication Requires Basic Authentication
120
+ #
121
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
122
+ #
123
+ # @return [Array<Teamsupport::Product>]
124
+ #
125
+ # @param id [Integer] A customer ID.
126
+ # @param options [Hash] A customizable set of options.
127
+ #
128
+ # @api public
129
+ def customer_products(id, options = {})
130
+ options = options.dup
131
+ perform_get_with_objects_from_collection("/api/json/customers/#{id}/products.json", options, Teamsupport::CustomerProduct, :OrganizationProducts)
132
+ end
133
+
134
+ # Returns the tickets for a customer
135
+ #
136
+ # @example
137
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
138
+ # teamsupport_api.customer_tickets('1')
139
+ #
140
+ # @authentication Requires Basic Authentication
141
+ #
142
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
143
+ #
144
+ # @return [Array<Teamsupport::Ticket>]
145
+ #
146
+ # @param id [Integer] A customer ID.
147
+ # @param options [Hash] A customizable set of options.
148
+ #
149
+ # @api public
150
+ def customer_tickets(id, options = {})
151
+ options = options.dup
152
+ perform_get_with_objects_from_collection("/api/json/customers/#{id}/tickets.json", options, Teamsupport::Ticket, :Tickets)
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,113 @@
1
+ require 'teamsupport/error'
2
+ require 'teamsupport/product'
3
+ require 'teamsupport/rest/utils'
4
+ require 'teamsupport/utils'
5
+
6
+ module Teamsupport
7
+ module REST
8
+ module Products
9
+ include Teamsupport::REST::Utils
10
+ include Teamsupport::Utils
11
+
12
+ # Returns all available products for the TeamSupport organization
13
+ #
14
+ # @see http://help.teamsupport.com/1/en/topic/api
15
+ #
16
+ # @example
17
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
18
+ # teamsupport_api.products()
19
+ #
20
+ # @authentication Requires Basic Authentication
21
+ #
22
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied API credentials are not valid.
23
+ #
24
+ # @return [Array<Teamsupport::Product>]
25
+ #
26
+ # @param options [Hash] A customizable set of options.
27
+ # @option options [Integer] :count Specifies the number of records to retrieve.
28
+ #
29
+ # @api public
30
+ def products(options = {})
31
+ perform_get_with_objects_from_collection('/api/json/products.json', options, Teamsupport::Product, :Products)
32
+ end
33
+
34
+ # Returns a product
35
+ #
36
+ # @example
37
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
38
+ # teamsupport_api.product('1')
39
+ #
40
+ # @authentication Requires Basic Authentication
41
+ #
42
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
43
+ #
44
+ # @return [Teamsupport::Product] The requested product.
45
+ #
46
+ # @param id [Integer] A product ID.
47
+ # @param options [Hash] A customizable set of options.
48
+ #
49
+ # @api public
50
+ def product(id, options = {})
51
+ perform_get_with_object_from_collection("/api/json/products/#{id}.json", options, Teamsupport::Product, :Product)
52
+ end
53
+
54
+ # Create a product
55
+ #
56
+ # @example
57
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
58
+ # teamsupport_api.create_product(Name: 'New Product')
59
+ #
60
+ # @authentication Requires Basic Authentication
61
+ #
62
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
63
+ #
64
+ # @return [Teamsupport::Product] The new product.
65
+ #
66
+ # @param options [Hash] A customizable set of options.
67
+ #
68
+ # @api public
69
+ def create_product(options = {})
70
+ perform_post_with_object_from_collection('/api/json/products.json', options, Teamsupport::Product, :Product)
71
+ end
72
+
73
+ # Updates the product
74
+ #
75
+ # @example
76
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
77
+ # teamsupport_api.update_product('1', Name: 'Updated Product Name')
78
+ #
79
+ # @authentication Requires Basic Authentication
80
+ #
81
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
82
+ #
83
+ # @return [Teamsupport::Product] The updated product.
84
+ #
85
+ # @param id [Integer] A product ID.
86
+ # @param options [Hash] A customizable set of options.
87
+ #
88
+ # @api public
89
+ def update_product(id, options = {})
90
+ product_hash = product(id).to_h
91
+ perform_put_with_object_from_collection("/api/json/products/#{id}.json", product_hash.merge(options), Teamsupport::Product, :Product)
92
+ end
93
+
94
+ # Deletes the product
95
+ #
96
+ # @example
97
+ # teamsupport_api = Teamsupport::REST::Client.new(api_key: 'AK', api_secret: 'AS')
98
+ # teamsupport_api.delete_product('1')
99
+ #
100
+ # @authentication Requires Basic Authentication
101
+ #
102
+ # @raise [Teamsupport::Error::Unauthorized] Error raised when supplied user credentials are not valid.
103
+ #
104
+ # @param id [Integer] A product ID.
105
+ # @param options [Hash] A customizable set of options.
106
+ #
107
+ # @api public
108
+ def delete_product(id, options = {})
109
+ perform_delete("/api/json/products/#{id}.json", options)
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,116 @@
1
+ require 'addressable/uri'
2
+ require 'http'
3
+ require 'http/form_data'
4
+ require 'json'
5
+ require 'openssl'
6
+ require 'teamsupport/error'
7
+ require 'teamsupport/headers'
8
+
9
+ module Teamsupport
10
+ module REST
11
+ class Request
12
+ BASE_URL = 'https://app.teamsupport.com'.freeze
13
+ attr_accessor :client, :headers, :options, :path, :request_method, :uri
14
+ alias verb request_method
15
+
16
+ # Initialize a new REST Request object
17
+ #
18
+ # @param client [Teamsupport::Client]
19
+ # @param request_method [String, Symbol]
20
+ # @param path [String]
21
+ # @param options [Hash]
22
+ #
23
+ # @return [Teamsupport::REST::Request]
24
+ #
25
+ # @api private
26
+ def initialize(client, request_method, path, options = {})
27
+ @client = client
28
+ @uri = Addressable::URI.parse(path.start_with?('http') ? path : BASE_URL + path)
29
+ @request_method = request_method
30
+ @headers = Teamsupport::Headers.new(@client, @request_method, @uri, options).request_headers
31
+ @path = uri.path
32
+ @options = options
33
+ end
34
+
35
+ # Perform an HTTP REST request and return response body or raise the error
36
+ #
37
+ # @example
38
+ # Teamsupport::REST::Request.new(self, request_method, path, options).perform
39
+ #
40
+ # @return [Array, Hash]
41
+ #
42
+ # @api public
43
+ def perform
44
+ options_key = @request_method == :get ? :params : :body
45
+ # For non-GET requests need to send the options as JSON in the request body
46
+ response = http_client.headers(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
47
+ response_body = response.body.empty? ? '' : symbolize_keys!(response.parse)
48
+ response_headers = response.headers
49
+ fail_or_return_response_body(response.code, response_body, response_headers)
50
+ end
51
+
52
+ private
53
+
54
+ # Check response and raise an error or return the response body if successful
55
+ #
56
+ # @param code [Strin]
57
+ # @param body [String]
58
+ # @param headers [String]
59
+ #
60
+ # @raise [Teamsupport::Error] Error raised based on response code/body/headers
61
+ #
62
+ # @return [Array, Hash]
63
+ #
64
+ # @api private
65
+ def fail_or_return_response_body(code, body, headers)
66
+ error = error(code, body, headers)
67
+ raise(error) if error
68
+ body
69
+ end
70
+
71
+ # Check response code/body/headers for errors and return Teamsupport::Error
72
+ #
73
+ # @param code [Strin]
74
+ # @param body [String]
75
+ # @param headers [String]
76
+ #
77
+ # @return [Teamsupport::Error, nil]
78
+ #
79
+ # @api private
80
+ def error(code, body, headers)
81
+ klass = Teamsupport::Error::ERRORS[code]
82
+
83
+ klass.from_response(body, headers) unless klass.nil?
84
+ end
85
+
86
+ # Convert array/hash keys to symbols
87
+ #
88
+ # @param object [Array, Hash]
89
+ #
90
+ # @return [Hash, Array]
91
+ #
92
+ # @api private
93
+ def symbolize_keys!(object)
94
+ if object.is_a?(Array)
95
+ object.each_with_index do |val, index|
96
+ object[index] = symbolize_keys!(val)
97
+ end
98
+ elsif object.is_a?(Hash)
99
+ object.keys.each do |key|
100
+ object[key.to_sym] = symbolize_keys!(object.delete(key))
101
+ end
102
+ end
103
+ object
104
+ end
105
+
106
+ # Returns the HTTP Client object
107
+ #
108
+ # @return [HTTP::Client, HTTP]
109
+ #
110
+ # @api private
111
+ def http_client
112
+ HTTP
113
+ end
114
+ end
115
+ end
116
+ end