Class: Floodgate::Client
- Inherits:
-
Object
- Object
- Floodgate::Client
- Defined in:
- lib/floodgate/client.rb
Class Method Summary collapse
- .add_ip_address(ip_address) ⇒ Object
- .add_my_ip_address ⇒ Object
- .allowed_ip_addresses ⇒ Object
- .base_url ⇒ Object
- .close ⇒ Object
- .default_status ⇒ Object
- .delete(path, params = {}) ⇒ Object
- .get(path, params = {}) ⇒ Object
- .get_connection ⇒ Object
- .host_name ⇒ Object
- .my_ip_address ⇒ Object
- .open ⇒ Object
- .post(path, params = {}) ⇒ Object
- .put(path, params = {}) ⇒ Object
- .remove_ip_address(ip_address) ⇒ Object
- .remove_my_ip_address ⇒ Object
- .set_redirect_url(redirect_url) ⇒ Object
- .status ⇒ Object
- .user_agent ⇒ Object
- .verb_with_params_in_body(verb, path, params = {}) ⇒ Object
Class Method Details
.add_ip_address(ip_address) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/floodgate/client.rb', line 7 def self.add_ip_address(ip_address) return if ip_address.nil? || ip_address.empty? params = { ip_address: { ip_address: ip_address } } post('ip_addresses', params) end |
.add_my_ip_address ⇒ Object
15 16 17 |
# File 'lib/floodgate/client.rb', line 15 def self.add_my_ip_address add_ip_address(my_ip_address) end |
.allowed_ip_addresses ⇒ Object
23 24 25 |
# File 'lib/floodgate/client.rb', line 23 def self.allowed_ip_addresses status.allowed_ip_addresses end |
.base_url ⇒ Object
112 113 114 |
# File 'lib/floodgate/client.rb', line 112 def self.base_url "https://#{host_name}/apps/#{Config.app_id}" end |
.close ⇒ Object
27 28 29 30 31 |
# File 'lib/floodgate/client.rb', line 27 def self.close params = { app: { filter_traffic: true } } put('', params) end |
.default_status ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/floodgate/client.rb', line 60 def self.default_status Hashie::Mash.new( default_status: true, filter_traffic: false, allowed_ip_addresses: [], redirect_url: nil ) end |
.delete(path, params = {}) ⇒ Object
78 79 80 |
# File 'lib/floodgate/client.rb', line 78 def self.delete(path, params = {}) verb_with_params_in_body(:delete, path, params) end |
.get(path, params = {}) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/floodgate/client.rb', line 82 def self.get(path, params = {}) params.merge!(api_token: Config.api_token) response = get_connection.get(path, params) response.body end |
.get_connection ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/floodgate/client.rb', line 120 def self.get_connection conn = Faraday.new(url: base_url) do |builder| builder.use Faraday::Response::Mashify builder.use FaradayMiddleware::ParseJson, content_type: 'application/json' builder.use Faraday::Request::UrlEncoded builder.use Faraday::Response::RaiseError builder.use Faraday::Adapter::NetHttp end conn.headers[:accept] = 'application/vnd.floodgate-v1+json' conn.headers[:user_agent] = user_agent conn end |
.host_name ⇒ Object
108 109 110 |
# File 'lib/floodgate/client.rb', line 108 def self.host_name Config.test? ? 'staging-api.floodgate.io' : 'api.floodgate.io' end |
.my_ip_address ⇒ Object
33 34 35 36 37 38 |
# File 'lib/floodgate/client.rb', line 33 def self.my_ip_address conn = Faraday.new(url: 'http://ip.floodgate.io') response = conn.get('') value = response.body.strip IPAddr.new(value, Socket::AF_INET).to_s end |
.open ⇒ Object
40 41 42 43 44 |
# File 'lib/floodgate/client.rb', line 40 def self.open params = { app: { filter_traffic: false } } put('', params) end |
.post(path, params = {}) ⇒ Object
90 91 92 |
# File 'lib/floodgate/client.rb', line 90 def self.post(path, params = {}) verb_with_params_in_body(:post, path, params) end |
.put(path, params = {}) ⇒ Object
94 95 96 |
# File 'lib/floodgate/client.rb', line 94 def self.put(path, params = {}) verb_with_params_in_body(:put, path, params) end |
.remove_ip_address(ip_address) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/floodgate/client.rb', line 46 def self.remove_ip_address(ip_address) return if ip_address.nil? || ip_address.empty? params = { ip_address: { ip_address: ip_address } } delete('ip_addresses', params) end |
.remove_my_ip_address ⇒ Object
19 20 21 |
# File 'lib/floodgate/client.rb', line 19 def self.remove_my_ip_address remove_ip_address(my_ip_address) end |
.set_redirect_url(redirect_url) ⇒ Object
54 55 56 57 58 |
# File 'lib/floodgate/client.rb', line 54 def self.set_redirect_url(redirect_url) params = { app: { redirect_url: redirect_url } } put('', params) end |
.status ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/floodgate/client.rb', line 69 def self.status begin get('status') rescue Faraday::Error::ClientError => e # TODO: Log default_status end end |
.user_agent ⇒ Object
116 117 118 |
# File 'lib/floodgate/client.rb', line 116 def self.user_agent 'FloodgateAgent' end |
.verb_with_params_in_body(verb, path, params = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/floodgate/client.rb', line 98 def self.verb_with_params_in_body(verb, path, params = {}) params.merge!(api_token: Config.api_token) response = get_connection.public_send(verb, path) do |request| request.body = params end response.body end |