Class: ProxyCheapClient::Client
- Defined in:
- lib/proxy_cheap_client/client.rb
Constant Summary collapse
- API_BASE =
"https://api.proxy-cheap.com"
Instance Method Summary collapse
-
#balance ⇒ Balance
Get user balance.
- #conn ⇒ Object
-
#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_static_residential_proxy(params = {}) ⇒ Order
Convenience method to order a static residential proxy.
-
#proxies ⇒ Array<Proxy>
Get all user proxies.
Methods inherited from Base
#extract_error_body, #extract_error_message, #parse_response, #request
Constructor Details
#initialize(api_key:, api_secret:, timeout: 10) ⇒ Client
Initialize the client with API credentials.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/proxy_cheap_client/client.rb', line 19 def initialize( api_key:, api_secret:, timeout: 10 ) @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.}" end |
Instance Method Details
#balance ⇒ Balance
Get user balance.
62 63 64 65 |
# File 'lib/proxy_cheap_client/client.rb', line 62 def balance resp = request(:get, "account/balance") Balance.new(resp, self) end |
#conn ⇒ Object
43 44 45 |
# File 'lib/proxy_cheap_client/client.rb', line 43 def conn @conn end |
#countries ⇒ Object
retrieve the array of countries with number of available proxies per country
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/proxy_cheap_client/client.rb', line 48 def countries config = self.configuration( networkType: "RESIDENTIAL_STATIC", ipVersion: "IPv4", country: "US", proxyProtocol: "HTTP", authenticationType: "USERNAME_PASSWORD", quantity: 1 ) config.data["supportedCountries"] end |
#order_static_residential_proxy(params = {}) ⇒ Order
Convenience method to order a static residential proxy.
85 86 87 88 89 90 91 |
# File 'lib/proxy_cheap_client/client.rb', line 85 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.
69 70 71 72 73 74 75 |
# File 'lib/proxy_cheap_client/client.rb', line 69 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 Array(proxy_list).map { |p| Proxy.new(p, self) } end |