Class: Squab::Client
- Inherits:
-
Object
- Object
- Squab::Client
- Defined in:
- lib/squab-client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#api_version_prefix ⇒ Object
Returns the value of attribute api_version_prefix.
-
#events_prefix ⇒ Object
Returns the value of attribute events_prefix.
-
#source ⇒ Object
Returns the value of attribute source.
-
#ssl_verify ⇒ Object
Returns the value of attribute ssl_verify.
-
#uid ⇒ Object
Returns the value of attribute uid.
Instance Method Summary collapse
- #get(max = 5, since = nil) ⇒ Object
- #get_api(url) ⇒ Object
- #get_from_event(event_num) ⇒ Object
- #get_from_source(source) ⇒ Object
- #get_from_user(username) ⇒ Object
- #get_my_source(source = nil) ⇒ Object
- #get_my_user(username = nil) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
Opts accepts api => The API URL to hit user => The user to report source => the source to report.
- #list_sources ⇒ Object
- #list_urls ⇒ Object
- #list_users ⇒ Object
- #parse_config(file = nil) ⇒ Object
- #search(search_params) ⇒ Object
- #send(event, url = nil) ⇒ Object
- #simple_search(search_val) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Opts accepts api => The API URL to hit user => The user to report source => the source to report
19 20 21 22 23 24 25 26 27 |
# File 'lib/squab-client.rb', line 19 def initialize(opts={}) @source = get_my_source(opts[:source]) @uid = get_my_user(opts[:user]) parse_config @api_url = get_api(opts[:api] || @config[:api]) @ssl_verify = @config['ssl_verify'] || true @api_version_prefix = @config['api_prefix'] || '/api/v1' @events_prefix = [@api_version_prefix, 'events'].join('/') end |
Instance Attribute Details
#api_url ⇒ Object
Returns the value of attribute api_url.
13 14 15 |
# File 'lib/squab-client.rb', line 13 def api_url @api_url end |
#api_version_prefix ⇒ Object
Returns the value of attribute api_version_prefix.
14 15 16 |
# File 'lib/squab-client.rb', line 14 def api_version_prefix @api_version_prefix end |
#events_prefix ⇒ Object
Returns the value of attribute events_prefix.
14 15 16 |
# File 'lib/squab-client.rb', line 14 def events_prefix @events_prefix end |
#source ⇒ Object
Returns the value of attribute source.
13 14 15 |
# File 'lib/squab-client.rb', line 13 def source @source end |
#ssl_verify ⇒ Object
Returns the value of attribute ssl_verify.
13 14 15 |
# File 'lib/squab-client.rb', line 13 def ssl_verify @ssl_verify end |
#uid ⇒ Object
Returns the value of attribute uid.
13 14 15 |
# File 'lib/squab-client.rb', line 13 def uid @uid end |
Instance Method Details
#get(max = 5, since = nil) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/squab-client.rb', line 65 def get(max=5, since=nil) req = if since make_event_request("since/#{since.to_i}") else make_event_request("limit/#{max}") end get_req(req) end |
#get_api(url) ⇒ Object
100 101 102 103 104 |
# File 'lib/squab-client.rb', line 100 def get_api(url) url ? URI.parse(url) : URI.parse( "http://squab/" ) end |
#get_from_event(event_num) ⇒ Object
74 75 76 77 |
# File 'lib/squab-client.rb', line 74 def get_from_event(event_num) req = make_event_request("starting/#{event_num}") get_req(req) end |
#get_from_source(source) ⇒ Object
84 85 86 87 |
# File 'lib/squab-client.rb', line 84 def get_from_source(source) req = make_event_request("source/#{source}") get_req(req) end |
#get_from_user(username) ⇒ Object
79 80 81 82 |
# File 'lib/squab-client.rb', line 79 def get_from_user(username) req = make_event_request("user/#{username}") get_req(req) end |
#get_my_source(source = nil) ⇒ Object
117 118 119 |
# File 'lib/squab-client.rb', line 117 def get_my_source(source=nil) source || File.basename($PROGRAM_NAME) end |
#get_my_user(username = nil) ⇒ Object
121 122 123 |
# File 'lib/squab-client.rb', line 121 def get_my_user(username=nil) username || Etc.getpwuid(Process.uid).name end |
#list_sources ⇒ Object
50 51 52 53 |
# File 'lib/squab-client.rb', line 50 def list_sources req = make_request('sources') get_req(req) end |
#list_urls ⇒ Object
60 61 62 63 |
# File 'lib/squab-client.rb', line 60 def list_urls req = make_request('urls') get_req(req) end |
#list_users ⇒ Object
55 56 57 58 |
# File 'lib/squab-client.rb', line 55 def list_users req = make_request('users') get_req(req) end |
#parse_config(file = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/squab-client.rb', line 106 def parse_config(file=nil) # Default config_file = file || '/etc/squab.yaml' # Instance override if File.exist?(config_file) @config = YAML.load(File.open(config_file).read) else @config = {} end end |
#search(search_params) ⇒ Object
94 95 96 97 98 |
# File 'lib/squab-client.rb', line 94 def search(search_params) req = Net::HTTP::Post.new([ @events_prefix, 'search' ].join('/')) req.body = JSON.dump(search_params) get_req(req) end |
#send(event, url = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/squab-client.rb', line 29 def send(event, url=nil) payload = { "uid" => @uid, "source" => @source, "value" => event, "url" => url, }.to_json header = {'Content-Type' => 'application/json'} req = Net::HTTP::Post.new(@events_prefix, initheader = header) req.body = payload try_count = 1 begin http = Net::HTTP.new(@api_url.host, @api_url.port) http = setup_ssl(http) if @api_url.scheme == "https" response = http.request(req) rescue EOFError, Errno::ECONNREFUSED => e raise SendEventFailed, "Could not reach the Squab server" end response end |
#simple_search(search_val) ⇒ Object
89 90 91 92 |
# File 'lib/squab-client.rb', line 89 def simple_search(search_val) req = make_event_request("search/value/#{search_val}/limit/5") get_req(req) end |