class Luminati::Client

Constants

API_URL

Attributes

api_token[RW]

Public Class Methods

new(api_token = nil) click to toggle source

Initializes a new Client object. @param api_token [String] @return [Luminati::Client]

# File lib/luminati/client.rb, line 20
def initialize(api_token = nil)
  instance_variable_set("@api_token", api_token) if api_token
end

Public Instance Methods

cities(country_code) click to toggle source

Returns cities for a given country @see luminati.io/doc/api#others_get_cities @param country_code [String] A country code like `US`. @return [Hash]

# File lib/luminati/client.rb, line 36
def cities(country_code)
  request(:get, "/api/cities?country=#{country_code}")
end
network_status(network_type = :all) click to toggle source

Returns the current service status for a given network type @see luminati.io/doc/api#account_api_stat @param network_type [Symbol] A network type, one of `:all`, `:res`, `:dc`, `:mobile`. @return [Hash]

# File lib/luminati/client.rb, line 28
def network_status(network_type = :all)
  request(:get, "/api/network_status/#{network_type}")
end

Private Instance Methods

conn() click to toggle source
# File lib/luminati/client.rb, line 42
def conn
  headers = {
    "Content-Type": "application/json"
  }
  headers = headers.merge({ "Authorization": "Bearer #{@api_token}" }) if @api_token
  @conn ||= Faraday.new(
    API_URL,
    request: { timeout: 5 },
    headers: headers
  )
end
request(method, path, options = nil) click to toggle source
# File lib/luminati/client.rb, line 54
def request(method, path, options = nil)
  res = if method == :delete
          conn.delete(path) { |req| req.body = options }
        else
          conn.public_send(method, path, options)
        end
  if res.status == 200
    begin
      Oj.load(res.body)
    rescue Oj::ParseError
      # when the body isn't JSON (e.g. GET /api/zone/route_ips)
      res.body
    end
  else
    { "status_code" => res.status, "status_message" => res.reason_phrase, "response_body" => res.body }
  end
end