ticket_sharing 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,24 +3,21 @@ require 'ticket_sharing/request'
3
3
 
4
4
  module TicketSharing
5
5
  class Client
6
-
7
- attr_reader :response, :code
8
-
9
6
  def initialize(base_url, credentials=nil)
10
7
  @base_url = base_url
11
8
  @credentials = credentials
12
9
  end
13
10
 
14
- def post(path, body)
15
- send_request(Net::HTTP::Post, path, body)
11
+ def post(path, body, options={})
12
+ send_request(:post, path, body, options)
16
13
  end
17
14
 
18
- def put(path, body)
19
- send_request(Net::HTTP::Put, path, body)
15
+ def put(path, body, options={})
16
+ send_request(:put, path, body, options)
20
17
  end
21
18
 
22
- def delete(path)
23
- send_request(Net::HTTP::Delete, path, '')
19
+ def delete(path, options={})
20
+ send_request(:delete, path, '', options)
24
21
  end
25
22
 
26
23
  def success?
@@ -29,39 +26,24 @@ module TicketSharing
29
26
 
30
27
  private
31
28
 
32
- def send_request(request_class, path, body)
33
- request = TicketSharing::Request.new(request_class, @base_url + path, body)
34
-
35
- if @credentials
36
- request.set_header('X-Ticket-Sharing-Token', @credentials)
37
- end
29
+ def send_request(method, path, body, options)
30
+ headers = {'X-Ticket-Sharing-Token' => @credentials} if @credentials
31
+ options = options.merge(:body => body, :headers => headers)
32
+ response = TicketSharing::Request.new.request(method, @base_url + path, options)
38
33
 
39
- request.send!
40
-
41
- handle_response(request)
42
- end
43
-
44
- def handle_response(request)
45
- @response = request.raw_response
46
- @code = response.code.to_i
34
+ handle_response(response)
35
+ end
47
36
 
48
- case @code
49
- when (200..299)
50
- @success = true
51
- response
52
- when (300..399)
53
- request.follow_redirect!
54
- handle_response(request)
55
- when 403, 404, 405, 408, 410, 422
56
- @success = false
57
- response
58
- when (500..599)
59
- @success = false
60
- response
61
- else
62
- raise TicketSharing::Error.new(%Q{#{response.code} "#{response.message}"\n\n#{response.body}})
63
- end
37
+ def handle_response(response)
38
+ @success = case response.code.to_i
39
+ when (200..299)
40
+ true
41
+ when 401, 403, 404, 405, 408, 410, 422, 500..599
42
+ false
43
+ else
44
+ raise TicketSharing::Error.new(%Q{#{response.code} "#{response.message}"\n\n#{response.body}})
64
45
  end
65
-
46
+ response
47
+ end
66
48
  end
67
49
  end
@@ -1,64 +1,74 @@
1
+ require 'ticket_sharing/error'
2
+
1
3
  module TicketSharing
2
4
  class Request
3
-
4
- attr_reader :raw_response
5
-
6
- # this should very rarely have to go above 2, and definitely never any higher than 5
7
5
  MAX_REDIRECTS = 2
8
6
 
9
7
  CA_PATH = "/etc/ssl/certs"
10
8
 
11
- def initialize(request_class, uri, body)
12
- @redirects = 0
13
- @uri = URI.parse(uri)
14
- @request_class = request_class
15
- @body = body
16
- @raw_request = new_raw_request
17
- end
9
+ def request(method, url, options = {})
10
+ request_class = case method
11
+ when :get then Net::HTTP::Get
12
+ when :post then Net::HTTP::Post
13
+ when :put then Net::HTTP::Put
14
+ when :delete then Net::HTTP::Delete
15
+ else
16
+ raise ArgumentError, "Unsupported method: #{method.inspect}"
17
+ end
18
18
 
19
- def set_header(key, value)
20
- @raw_request[key] = value
21
- end
19
+ response = send_request(request_class, url, options)
22
20
 
23
- def send!
24
- @raw_request.body = @body
21
+ follow_redirects!(request_class, response, options)
22
+ end
25
23
 
26
- http = Net::HTTP.new(@uri.host, @uri.port)
24
+ private
27
25
 
28
- if @uri.scheme == 'https'
29
- http.use_ssl = true
30
- http.ca_path = CA_PATH if File.exist?(CA_PATH)
31
- end
32
-
33
- @raw_response = http.start do |http|
34
- http.request(@raw_request)
35
- end
26
+ def send_request(request_class, url, options)
27
+ uri = URI.parse(url)
28
+ request = build_request(request_class, uri, options)
29
+ send!(request, uri, options)
36
30
  end
37
31
 
38
- def follow_redirect!
39
- if @redirects >= MAX_REDIRECTS
40
- raise TicketSharing::TooManyRedirects
41
- end
32
+ def build_request(request_class, uri, options)
33
+ request = request_class.new(uri.path)
34
+ request['Accept'] = 'application/json'
35
+ request['Content-Type'] = 'application/json'
42
36
 
43
- @uri = URI.parse(@raw_response['Location'])
44
- @raw_request = new_raw_request
37
+ (options[:headers] || {}).each do |k, v|
38
+ request[k] = v
39
+ end
45
40
 
46
- self.send!
41
+ request.body = options[:body]
47
42
 
48
- @redirects += 1
43
+ request
49
44
  end
50
45
 
51
- def new_raw_request
52
- raw_request = @request_class.new(@uri.path)
53
- raw_request['Accept'] = 'application/json'
54
- raw_request['Content-Type'] = 'application/json'
46
+ def send!(request, uri, options)
47
+ http = Net::HTTP.new(uri.host, uri.port)
55
48
 
56
- if @raw_request && token = @raw_request['X-Ticket-Sharing-Token']
57
- raw_request['X-Ticket-Sharing-Token'] = token
49
+ if uri.scheme == 'https'
50
+ http.use_ssl = true
51
+ http.ca_path = CA_PATH if File.exist?(CA_PATH)
52
+
53
+ if options[:ssl] && options[:ssl][:verify] == false
54
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
55
+ end
58
56
  end
59
57
 
60
- raw_request
58
+ http.start { |http| http.request(request) }
61
59
  end
62
60
 
61
+ def follow_redirects!(request_class, response, options)
62
+ redirects = 0
63
+ while (300..399).include?(response.code.to_i)
64
+ if redirects >= MAX_REDIRECTS
65
+ raise TicketSharing::TooManyRedirects
66
+ else
67
+ redirects += 1
68
+ end
69
+ response = send_request(request_class, response['Location'], options)
70
+ end
71
+ response
72
+ end
63
73
  end
64
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ticket_sharing
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-14 00:00:00.000000000 Z
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A ruby implementation of the Networked Help Desk API
15
15
  email: josh@zendesk.com
@@ -45,6 +45,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
45
  - - ! '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
+ segments:
49
+ - 0
50
+ hash: 3138541096207578650
48
51
  required_rubygems_version: !ruby/object:Gem::Requirement
49
52
  none: false
50
53
  requirements:
@@ -53,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
56
  version: '0'
54
57
  requirements: []
55
58
  rubyforge_project:
56
- rubygems_version: 1.8.23
59
+ rubygems_version: 1.8.25
57
60
  signing_key:
58
61
  specification_version: 3
59
62
  summary: Ticket sharing