Class: Commands
- Inherits:
-
Object
- Object
- Commands
- Defined in:
- lib/commands.rb
Instance Method Summary collapse
- #alltickets ⇒ Object
- #get_this_user(userid) ⇒ Object
-
#initialize(baseurl, username, password) ⇒ Commands
constructor
A new instance of Commands.
- #localinfo ⇒ Object
- #makerequest(type = 'Get', body = '') ⇒ Object
- #me ⇒ Object
- #myworking ⇒ Object
- #showticket(ticketid) ⇒ Object
- #ticketcomments(ticketid) ⇒ Object
- #ticketinfo(ticketid) ⇒ Object
- #updateticket(ticketid, comment, status = 'open', is_public = 'false') ⇒ Object
- #userelement(field) ⇒ Object
Constructor Details
#initialize(baseurl, username, password) ⇒ Commands
Returns a new instance of Commands.
2 3 4 5 6 7 8 |
# File 'lib/commands.rb', line 2 def initialize(baseurl, username, password) @uri = URI.parse("https://#{baseurl}/") @username = username @password = password @uri.path = '/api/v2/users/me.json' @current_user_info = JSON.parse(makerequest) end |
Instance Method Details
#alltickets ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/commands.rb', line 61 def alltickets @uri.path = '/api/v2/tickets.json' @response = JSON.parse(makerequest) alltickets = @response['tickets'] pagecount = 1 while @response['next_page'] != nil pagecount += 1 @uri.query = 'page=' + pagecount.to_s @response = JSON.parse(makerequest) alltickets.concat(@response['tickets']) end unsolved_tickets = alltickets.reject { |element| element['status'] == 'closed' } puts "Total Tickets: #{alltickets.length.to_s}" puts "Unsolved Tickets: #{unsolved_tickets.length.to_s}" end |
#get_this_user(userid) ⇒ Object
35 36 37 38 39 |
# File 'lib/commands.rb', line 35 def get_this_user(userid) @uri.path = "/api/v2/users/#{userid}.json" @uri.query = '' return JSON.parse(makerequest)['user'] end |
#localinfo ⇒ Object
10 11 12 13 14 |
# File 'lib/commands.rb', line 10 def localinfo puts @uri puts @username puts @password end |
#makerequest(type = 'Get', body = '') ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/commands.rb', line 16 def makerequest(type='Get', body='') @http = Net::HTTP.new(@uri.host, @uri.port) @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if type == 'Get' @request = Net::HTTP::Get.new(@uri.request_uri) elsif type == 'Put' @request = Net::HTTP::Put.new(@uri.request_uri) @request['Content-Type'] = 'application/json' @request.body = body else raise ArgumentError.new('Unrecognized HTTP request type.') end @request.basic_auth(@username, @password) return @response = @http.request(@request).body end |
#me ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/commands.rb', line 41 def me def userelement(field) @current_user_info['user'][field] end user_name = userelement('name') user_email = userelement('email') user_role = userelement('role') user_created = userelement('created_at') user_last_login = userelement('last_login_at') user_time_zone = userelement('time_zone') puts "Name: #{user_name}" puts "Email: #{user_email}" puts "Role: #{user_role}" puts "Created: #{user_created}" puts "Last Login: #{user_last_login}" puts "Time Zone: #{user_time_zone}" end |
#myworking ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/commands.rb', line 81 def myworking @uri.path = '/api/v2/search.json' @uri.query = URI.encode("query=status<solved+assignee:#{@username}+type:ticket") @my_working_tickets = JSON.parse(makerequest)['results'] @my_working_tickets.each do |ticket| requester = get_this_user(ticket['requester_id'])['name'] puts ticket['id'].to_s + ': ' + ticket['subject'] puts "Requester: #{requester}" puts 'Status: ' + ticket['status'] puts '' end puts "Total Working Tickets: #{@my_working_tickets.length}" end |
#showticket(ticketid) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/commands.rb', line 106 def showticket(ticketid) info = ticketinfo(ticketid) comments = ticketcomments(ticketid) puts "Ticket ID: #{info['ticket']['id']}" puts "Status: #{info['ticket']['status']}" puts "Last Updated: #{info['ticket']['updated_at']}" puts "Subject: #{info['ticket']['subject']}" puts "" comments['comments'].reverse.each do |comment| puts "Timestamp: #{comment['created_at']}" puts "Comment: #{comment['body']}" puts "########################" end end |
#ticketcomments(ticketid) ⇒ Object
101 102 103 104 |
# File 'lib/commands.rb', line 101 def ticketcomments(ticketid) @uri.path = "/api/v2/tickets/#{ticketid.to_s}/comments.json" JSON.parse(makerequest) end |
#ticketinfo(ticketid) ⇒ Object
96 97 98 99 |
# File 'lib/commands.rb', line 96 def ticketinfo(ticketid) @uri.path = "/api/v2/tickets/#{ticketid.to_s}.json" JSON.parse(makerequest) end |
#updateticket(ticketid, comment, status = 'open', is_public = 'false') ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/commands.rb', line 122 def updateticket(ticketid, comment, status='open', is_public='false') # https://support.puppetlabs.com/api/v2/tickets/3717.json @uri.path = "/api/v2/tickets/#{ticketid.to_s}.json" updatearray = { 'ticket' => { 'status' => status, 'comment' => { 'body' => comment, 'public' => is_public } } } updatearray_json = updatearray.to_json response = makerequest('Put', updatearray_json) end |
#userelement(field) ⇒ Object
42 43 44 |
# File 'lib/commands.rb', line 42 def userelement(field) @current_user_info['user'][field] end |