Class: ProxyCheapClient::Client

Inherits:
Base
  • Object
show all
Defined in:
lib/proxy_cheap_client/client.rb

Constant Summary collapse

API_BASE =
"https://api.proxy-cheap.com"

Instance Method Summary collapse

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.

Parameters:

  • api_key (String)

    API key (or set PROXY_CHEAP_API_KEY env var)

  • api_secret (String)

    API secret (or set PROXY_CHEAP_API_SECRET env var)

  • timeout (Integer) (defaults to: 10)

    Request timeout in seconds (default: 10)



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.options.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

Instance Method Details

#balanceBalance

Get user balance.

Returns:



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

#connObject



43
44
45
# File 'lib/proxy_cheap_client/client.rb', line 43

def conn
  @conn
end

#countriesObject

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.

Parameters:

  • params (Hash) (defaults to: {})

    Order parameters

Options Hash (params):

  • :country (String)

    2 letter country code (required)

  • :isp (String)

    ISP id

  • :proxyProtocol (String)

    HTTP, HTTPS, SOCKS5 (default: HTTP)

  • :authenticationType (String)

    USERNAME_PASSWORD or IP_WHITELIST

  • :quantity (Integer)

    number of proxies (default: 1)

Returns:



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

#proxiesArray<Proxy>

Get all user proxies.

Returns:



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