ticket_sharing 0.0 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -1 +1,2 @@
1
- jish was here.
1
+
2
+ Ticket sharing
@@ -0,0 +1,13 @@
1
+ require 'ticket_sharing/base'
2
+
3
+ module TicketSharing
4
+ class Actor < Base
5
+
6
+ fields :uuid, :name, :role
7
+
8
+ def agent?
9
+ role == 'agent'
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,41 @@
1
+ require 'ticket_sharing/base'
2
+ require 'ticket_sharing/client'
3
+ require 'ticket_sharing/actor'
4
+
5
+ module TicketSharing
6
+ class Agreement < Base
7
+
8
+ fields :receiver_url, :sender_url, :status, :uuid, :access_key, :name,
9
+ :current_actor, :sync_tags, :allows_public_comments
10
+
11
+ def self.parse(json)
12
+ attributes = JsonSupport.decode(json)
13
+ agreement = new(attributes)
14
+ if agreement.current_actor
15
+ agreement.current_actor = TicketSharing::Actor.new(agreement.current_actor)
16
+ end
17
+ agreement
18
+ end
19
+
20
+ def send_to(url)
21
+ client = Client.new(url)
22
+ client.post(relative_url, self.to_json)
23
+ client.success?
24
+ end
25
+
26
+ def update_partner(url)
27
+ client = Client.new(url, authentication_token)
28
+ client.put(relative_url, self.to_json)
29
+ client.success?
30
+ end
31
+
32
+ def relative_url
33
+ '/agreements/' + uuid.to_s
34
+ end
35
+
36
+ def authentication_token
37
+ "#{uuid}:#{access_key}"
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,9 @@
1
+ require 'ticket_sharing/base'
2
+
3
+ module TicketSharing
4
+ class Attachment < Base
5
+
6
+ fields :url, :filename, :content_type
7
+
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ require 'ticket_sharing/json_support'
2
+
3
+ module TicketSharing
4
+ class Base
5
+
6
+ def self.fields(*args)
7
+ @fields = args
8
+ attr_accessor *args
9
+ end
10
+
11
+ def self.field_list
12
+ @fields || []
13
+ end
14
+
15
+ def field_list
16
+ self.class.field_list
17
+ end
18
+
19
+ def initialize(attrs = {})
20
+ field_list.each do |attribute|
21
+ self.send("#{attribute}=", attrs[attribute.to_s]) if attrs.has_key?(attribute.to_s)
22
+ end
23
+ end
24
+
25
+ def to_json
26
+ attributes = field_list.inject({}) do |attrs, field|
27
+ attrs[field.to_s] = send(field)
28
+ attrs
29
+ end
30
+
31
+ JsonSupport.encode(attributes)
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,62 @@
1
+ require 'ticket_sharing/error'
2
+ require 'ticket_sharing/request'
3
+
4
+ module TicketSharing
5
+ class Client
6
+
7
+ attr_reader :response, :code
8
+
9
+ def initialize(base_url, credentials=nil)
10
+ @base_url = base_url
11
+ @credentials = credentials
12
+ end
13
+
14
+ def post(path, body)
15
+ send_request(Net::HTTP::Post, path, body)
16
+ end
17
+
18
+ def put(path, body)
19
+ send_request(Net::HTTP::Put, path, body)
20
+ end
21
+
22
+ def delete(path)
23
+ send_request(Net::HTTP::Delete, path, '')
24
+ end
25
+
26
+ def success?
27
+ raise "No call made to determine success" unless @success
28
+ @success
29
+ end
30
+
31
+ private
32
+
33
+ def send_request(request_class, path, body)
34
+ request = TicketSharing::Request.new(request_class, @base_url + path, body)
35
+
36
+ if @credentials
37
+ request.set_header('X-Ticket-Sharing-Token', @credentials)
38
+ end
39
+
40
+ request.send!
41
+
42
+ handle_response(request)
43
+ end
44
+
45
+ def handle_response(request)
46
+ @response = request.raw_response
47
+ @code = response.code.to_i
48
+
49
+ case response
50
+ when Net::HTTPSuccess
51
+ @success = true
52
+ response
53
+ when Net::HTTPMovedPermanently, Net::HTTPFound
54
+ request.follow_redirect!
55
+ handle_response(request)
56
+ else
57
+ raise TicketSharing::Error.new(%Q{#{response.code} "#{response.message}"\n\n#{response.body}})
58
+ end
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,41 @@
1
+ require 'ticket_sharing/base'
2
+ require 'ticket_sharing/actor'
3
+ require 'ticket_sharing/time'
4
+ require 'ticket_sharing/attachment'
5
+
6
+ module TicketSharing
7
+ class Comment < Base
8
+
9
+ fields :uuid, :author, :body, :authored_at, :public, :attachments
10
+
11
+ def initialize(params={})
12
+ self.public = true
13
+
14
+ super(params)
15
+
16
+ if Hash === author
17
+ self.author = Actor.new(author)
18
+ end
19
+ end
20
+
21
+ def self.parse(json)
22
+ attributes = JsonSupport.decode(json)
23
+ new(attributes)
24
+ end
25
+
26
+ def authored_at=(value)
27
+ @authored_at = TicketSharing::Time.new(value)
28
+ end
29
+
30
+ def attachments=(attachments)
31
+ @attachments = attachments && attachments.map do |attachment|
32
+ attachment.is_a?(Attachment) ? attachment : Attachment.new(attachment)
33
+ end
34
+ end
35
+
36
+ def public?
37
+ public
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ module TicketSharing
2
+ class Error < StandardError
3
+ end
4
+
5
+ class TooManyRedirects < TicketSharing::Error
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ require 'yajl'
2
+
3
+ module TicketSharing
4
+ class JsonSupport
5
+
6
+ def self.encode(attributes)
7
+ Yajl::Encoder.encode(attributes)
8
+ end
9
+
10
+ def self.decode(json)
11
+ Yajl::Parser.new.parse(json)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,52 @@
1
+ module TicketSharing
2
+ class Request
3
+
4
+ attr_reader :raw_response
5
+
6
+ MAX_REDIRECTS = 1
7
+
8
+ def initialize(request_class, uri, body)
9
+ @redirects = 0
10
+ @uri = URI.parse(uri)
11
+ @request_class = request_class
12
+ @body = body
13
+ @raw_request = new_raw_request
14
+ end
15
+
16
+ def set_header(key, value)
17
+ @raw_request[key] = value
18
+ end
19
+
20
+ def send!
21
+ @raw_request.body = @body
22
+
23
+ http = Net::HTTP.new(@uri.host, @uri.port)
24
+ http.use_ssl = true if @uri.scheme == 'https'
25
+
26
+ @raw_response = http.start do |http|
27
+ http.request(@raw_request)
28
+ end
29
+ end
30
+
31
+ def follow_redirect!
32
+ if @redirects >= MAX_REDIRECTS
33
+ raise TicketSharing::TooManyRedirects
34
+ end
35
+
36
+ @uri = URI.parse(@raw_response['Location'])
37
+ @raw_request = new_raw_request
38
+
39
+ self.send!
40
+
41
+ @redirects += 1
42
+ end
43
+
44
+ def new_raw_request
45
+ raw_request = @request_class.new(@uri.path)
46
+ raw_request['Accept'] = 'application/json'
47
+
48
+ raw_request
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,69 @@
1
+ require 'ticket_sharing/base'
2
+ require 'ticket_sharing/client'
3
+ require 'ticket_sharing/actor'
4
+ require 'ticket_sharing/comment'
5
+ require 'ticket_sharing/agreement'
6
+ require 'ticket_sharing/time'
7
+
8
+ module TicketSharing
9
+ class Ticket < Base
10
+
11
+ fields :uuid, :subject, :requested_at, :status, :requester, :comments,
12
+ :current_actor, :tags, :original_id, :custom_fields
13
+
14
+ attr_accessor :agreement
15
+
16
+ def self.parse(json)
17
+ attributes = JsonSupport.decode(json)
18
+ ticket = new(attributes)
19
+
20
+ if ticket.requester
21
+ ticket.requester = Actor.new(ticket.requester)
22
+ end
23
+
24
+ if ticket.current_actor
25
+ ticket.current_actor = Actor.new(ticket.current_actor)
26
+ end
27
+
28
+ if ticket.comments
29
+ ticket.comments = ticket.comments.map { |comment| Comment.new(comment) }
30
+ end
31
+
32
+ ticket
33
+ end
34
+
35
+ # TSTODO make all of these setters behave this way, not like they do in parse
36
+ def requested_at=(val)
37
+ @requested_at = TicketSharing::Time.new(val)
38
+ end
39
+
40
+ def comments
41
+ @comments ||= []
42
+ end
43
+
44
+ def send_to(url)
45
+ raise "Agreement not present" unless agreement
46
+
47
+ client = Client.new(url, agreement.authentication_token)
48
+ client.post(relative_url, self.to_json)
49
+ client.success?
50
+ end
51
+
52
+ def update_partner(url)
53
+ client = Client.new(url, agreement.authentication_token)
54
+ client.put(relative_url, self.to_json)
55
+ client.success?
56
+ end
57
+
58
+ def unshare(base_url)
59
+ client = Client.new(base_url, agreement.authentication_token)
60
+ client.delete(relative_url)
61
+ client.success?
62
+ end
63
+
64
+ def relative_url
65
+ "/tickets/#{uuid}"
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,29 @@
1
+ require 'time'
2
+
3
+ module TicketSharing
4
+ class Time
5
+
6
+ attr_reader :value
7
+
8
+ def initialize(value)
9
+ case value
10
+ when ::Time, nil
11
+ @value = value
12
+ when String
13
+ @value = ::Time.parse(value.dup)
14
+ else
15
+ raise "Invalid value provided for requested_at"
16
+ end
17
+ end
18
+
19
+ def to_json
20
+ JsonSupport.encode(@value ? @value.strftime('%Y-%m-%d %H:%M:%S %z') : nil)
21
+ end
22
+
23
+ # Support method to play well with active record
24
+ def to_time
25
+ value
26
+ end
27
+
28
+ end
29
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ticket_sharing
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 1
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
- version: "0.0"
8
+ - 5
9
+ version: "0.5"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Josh Lubaway
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-01 00:00:00 -08:00
17
+ date: 2011-06-07 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: Jish was here
21
+ description:
22
22
  email: josh@zendesk.com
23
23
  executables: []
24
24
 
@@ -27,9 +27,20 @@ extensions: []
27
27
  extra_rdoc_files:
28
28
  - Readme.md
29
29
  files:
30
+ - lib/ticket_sharing/actor.rb
31
+ - lib/ticket_sharing/agreement.rb
32
+ - lib/ticket_sharing/attachment.rb
33
+ - lib/ticket_sharing/base.rb
34
+ - lib/ticket_sharing/client.rb
35
+ - lib/ticket_sharing/comment.rb
36
+ - lib/ticket_sharing/error.rb
37
+ - lib/ticket_sharing/json_support.rb
38
+ - lib/ticket_sharing/request.rb
39
+ - lib/ticket_sharing/ticket.rb
40
+ - lib/ticket_sharing/time.rb
30
41
  - Readme.md
31
42
  has_rdoc: true
32
- homepage: http://example.com
43
+ homepage:
33
44
  licenses: []
34
45
 
35
46
  post_install_message: