Class: Papertrail::Connection
- Inherits:
-
Object
- Object
- Papertrail::Connection
- Extended by:
- Forwardable
- Defined in:
- lib/papertrail/connection.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #create_group(name, system_wildcard = nil) ⇒ Object
- #find_id_for_group(name) ⇒ Object
- #find_id_for_item(items, name_wanted) ⇒ Object
- #find_id_for_source(name) ⇒ Object
- #find_item_by_name(items, name_wanted) ⇒ Object
- #find_items_by_name(items, name_wanted) ⇒ Object
- #find_search(name, group_id = nil) ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #join_group(source_name, group_name) ⇒ Object
- #query(query = nil, options = {}) ⇒ Object
- #register_source(name, *args) ⇒ Object
- #show_group(name) ⇒ Object
- #show_source(name) ⇒ Object
- #unregister_source(name) ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
Returns a new instance of Connection.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/papertrail/connection.rb', line 15 def initialize() = { :verify => .fetch(:verify_ssl) { OpenSSL::SSL::VERIFY_PEER } } unless ([:username] && [:password]) || [:token] raise ArgumentError, "Must provide a username and password or a token" end # Make Ubuntu OpenSSL work # # From: https://bugs.launchpad.net/ubuntu/+source/openssl/+bug/396818 # "[OpenSSL] does not presume to select a set of CAs by default." if File.file?('/etc/ssl/certs/ca-certificates.crt') [:ca_file] = '/etc/ssl/certs/ca-certificates.crt' end @connection = Papertrail::HttpClient.new().tap do |conn| if [:username] && [:password] conn.basic_auth([:username], [:password]) else conn.token_auth([:token]) end end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
11 12 13 |
# File 'lib/papertrail/connection.rb', line 11 def connection @connection end |
Instance Method Details
#create_group(name, system_wildcard = nil) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/papertrail/connection.rb', line 94 def create_group(name, system_wildcard = nil) group = { :group => { :name => name } } if system_wildcard group[:group][:system_wildcard] = system_wildcard end @connection.post("groups.json", group) end |
#find_id_for_group(name) ⇒ Object
47 48 49 50 |
# File 'lib/papertrail/connection.rb', line 47 def find_id_for_group(name) response = @connection.get('groups.json') find_id_for_item(response.body, name) end |
#find_id_for_item(items, name_wanted) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/papertrail/connection.rb', line 85 def find_id_for_item(items, name_wanted) item = find_item_by_name(items, name_wanted) if item return item['id'] end return nil end |
#find_id_for_source(name) ⇒ Object
41 42 43 44 45 |
# File 'lib/papertrail/connection.rb', line 41 def find_id_for_source(name) response = @connection.get('systems.json') find_id_for_item(response.body, name) end |
#find_item_by_name(items, name_wanted) ⇒ Object
81 82 83 |
# File 'lib/papertrail/connection.rb', line 81 def find_item_by_name(items, name_wanted) find_items_by_name(items, name_wanted).first end |
#find_items_by_name(items, name_wanted) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/papertrail/connection.rb', line 67 def find_items_by_name(items, name_wanted) results = [] items.each do |item| results << item if item['name'] == name_wanted end items.each do |item| results << item if item['name'] =~ /#{Regexp.escape(name_wanted)}/i end results end |
#find_search(name, group_id = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/papertrail/connection.rb', line 52 def find_search(name, group_id = nil) response = @connection.get('searches.json') candidates = find_items_by_name(response.body, name) return nil if candidates.empty? candidates.each do |item| if !group_id || group_id == item['group_id'] return item end end return candidates.first end |
#join_group(source_name, group_name) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/papertrail/connection.rb', line 114 def join_group(source_name, group_name) source_id = find_id_for_source(source_name) group_id = find_id_for_group(group_name) if source_id && group_id @connection.post("systems/#{source_id}/join.json", :group_id => group_id) end end |
#query(query = nil, options = {}) ⇒ Object
160 161 162 |
# File 'lib/papertrail/connection.rb', line 160 def query(query = nil, = {}) Papertrail::SearchQuery.new(self, query, ) end |
#register_source(name, *args) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/papertrail/connection.rb', line 122 def register_source(name, *args) = args.last.is_a?(Hash) ? args.pop.dup : {} if ip_address = args.shift [:ip_address] = ip_address end if hostname = args.shift [:hostname] = hostname end request = { :system => { :name => name } } if [:ip_address] request[:system][:ip_address] = [:ip_address] end if [:hostname] request[:system][:hostname] = [:hostname] end if [:destination_port] request[:destination_port] = [:destination_port] end @connection.post("systems.json", request) end |
#show_group(name) ⇒ Object
102 103 104 105 106 |
# File 'lib/papertrail/connection.rb', line 102 def show_group(name) if id = find_id_for_group(name) @connection.get("groups/#{id}.json").body end end |
#show_source(name) ⇒ Object
108 109 110 111 112 |
# File 'lib/papertrail/connection.rb', line 108 def show_source(name) if id = find_id_for_source(name) @connection.get("systems/#{id}.json").body end end |
#unregister_source(name) ⇒ Object
154 155 156 157 158 |
# File 'lib/papertrail/connection.rb', line 154 def unregister_source(name) if source_id = find_id_for_source(name) @connection.delete("systems/#{source_id}.json") end end |