Class: ProxyCheapClient::Client
- Defined in:
- lib/proxy_cheap_client/client.rb
Constant Summary collapse
- API_BASE =
"https://api.proxy-cheap.com"
Class Method Summary collapse
Instance Method Summary collapse
-
#balance ⇒ Balance
Get user balance.
-
#countries ⇒ Object
retrieve the array of countries with number of available proxies per country.
-
#initialize(api_key:, api_secret:, timeout: 10) ⇒ Client
constructor
Initialize the client with API credentials.
-
#order_proxies(order_id) ⇒ Array<Proxy>
Get order proxies by order ID.
-
#order_static_residential_proxy(params = {}) ⇒ Order
Convenience method to order a static residential proxy.
-
#proxies ⇒ Array<Proxy>
Get all user proxies.
Methods inherited from Base
#conn, extract_error_body, extract_error_message, parse_response, request, #request
Constructor Details
#initialize(api_key:, api_secret:, timeout: 10) ⇒ Client
Initialize the client with API credentials.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/proxy_cheap_client/client.rb', line 23 def initialize( api_key:, api_secret:, timeout: 10 ) raise "Client already initialized" if defined?(@@conn) && @@conn @api_key = api_key @api_secret = api_secret raise AuthenticationError, "API key missing" unless @api_key && !@api_key.strip.empty? raise AuthenticationError, "API secret missing" unless @api_secret && !@api_secret.strip.empty? @@conn = Faraday.new(url: API_BASE) do |f| f.request :json f.response :raise_error f..timeout = timeout f.headers["X-Api-Key"] = @api_key f.headers["X-Api-Secret"] = @api_secret f.headers["Content-Type"] = "application/json" f.adapter Faraday.default_adapter end rescue Faraday::Error => e raise RequestError, "Connection failure: #{e.message}" end |
Class Method Details
.reset_connection ⇒ Object
15 16 17 |
# File 'lib/proxy_cheap_client/client.rb', line 15 def self.reset_connection @@conn = nil end |
Instance Method Details
#balance ⇒ Balance
Get user balance.
64 65 66 67 |
# File 'lib/proxy_cheap_client/client.rb', line 64 def balance resp = request(:get, "account/balance") Balance.new(resp) end |
#countries ⇒ Object
retrieve the array of countries with number of available proxies per country
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/proxy_cheap_client/client.rb', line 50 def countries config = self.configuration( networkType: "RESIDENTIAL_STATIC", ipVersion: "IPv4", country: "US", proxyProtocol: "HTTP", authenticationType: "USERNAME_PASSWORD", quantity: 1 ) config.data["supportedCountries"] end |
#order_proxies(order_id) ⇒ Array<Proxy>
Get order proxies by order ID.
72 73 74 75 76 77 78 79 |
# File 'lib/proxy_cheap_client/client.rb', line 72 def order_proxies(order_id) resp = request(:get, "orders/#{order_id}/proxies") arr = [] resp.each do |p| arr << ProxyCheapClient::Proxy.new(p) end arr end |
#order_static_residential_proxy(params = {}) ⇒ Order
Convenience method to order a static residential proxy.
100 101 102 103 104 105 106 |
# File 'lib/proxy_cheap_client/client.rb', line 100 def order_static_residential_proxy(params = {}) order_params = { networkType: "RESIDENTIAL_STATIC", ipVersion: "IPv4" }.merge(params) order(order_params) end |
#proxies ⇒ Array<Proxy>
Get all user proxies.
83 84 85 86 87 88 89 90 |
# File 'lib/proxy_cheap_client/client.rb', line 83 def proxies resp = request(:get, "proxies") # API may return proxies directly as an array or wrapped in a "data" key proxy_list = resp.is_a?(Hash) && resp.key?("data") ? resp["data"] : resp proxy_list = resp.is_a?(Hash) && resp.key?("proxies") ? resp["proxies"] : resp proxy_list.select! { |h| h["connection"] && h["connection"]["publicIp"] } # static residential proxies always have publicIp Array(proxy_list).map { |p| Proxy.new(p) } end |