Class: MCMPhoneNumberGenerator::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mcm_phone_number_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_uri = 'https://www.mycountrymobile.com/api/v1') ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/mcm_phone_number_generator.rb', line 9

def initialize(base_uri = 'https://www.mycountrymobile.com/api/v1')
  self.class.base_uri(base_uri)
  @headers = { 'Content-Type' => 'application/json' } # Removed Authorization header
end

Instance Method Details

#generate_phone_number(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mcm_phone_number_generator.rb', line 14

def generate_phone_number(params = {})
  # Validate required parameters
  validate_params!(params)

  # Log the request for debugging purposes (optional)
  puts "Requesting phone number generation with params: #{params}"

  # Make the POST request
  response = self.class.post('/phone-number-generator', headers: @headers, body: params.to_json)

  # Raise an error if the response is unsuccessful
  raise APIError, "Error: #{response.code} - #{response.message}" unless response.success?

  # Parse and return the JSON response
  JSON.parse(response.body)
rescue HTTParty::Error => e
  raise APIError, "Network error occurred: #{e.message}"
rescue JSON::ParserError => e
  raise APIError, "Response parsing error: #{e.message}"
end