Class: MessageMediaLookups::LookupsController

Inherits:
BaseController show all
Defined in:
lib/message_media_lookups/controllers/lookups_controller.rb

Overview

LookupsController

Class Attribute Summary collapse

Attributes inherited from BaseController

#http_call_back, #http_client

Instance Method Summary collapse

Methods inherited from BaseController

#apply_authentication, #execute_request, #initialize, #validate_parameters, #validate_response

Constructor Details

This class inherits a constructor from MessageMediaLookups::BaseController

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



8
9
10
# File 'lib/message_media_lookups/controllers/lookups_controller.rb', line 8

def instance
  @instance
end

Instance Method Details

#get_lookup_a_phone_number(phone_number, options = nil) ⇒ Object

Use the Lookups API to find information about a phone number. A request to the lookups API has the following format: “‘/v1/lookups/phone/phone_number?options=carrier,type“` The `phone_number` parameter is a required field and should be set to the phone number to be looked up. The options query parameter can also be used to request additional information about the phone number. By default, a request will only return the `country_code` and `phone_number` properties in the response. To request details about the the carrier, include `carrier` as a value of the options parameter. To request details about the type, include `type` as a value of the options parameter. To pass multiple values to the options parameter, use a comma separated list, i.e. `carrier,type`. A successful request to the lookups endpoint will return a response body as follows: “`json {

"country_code": "AU",
"phone_number": "+61491570156",
"type": "mobile",
"carrier": {
  "name": "Telstra"
}

} “‘ Each property in the response body is defined as follows:

  • “‘country_code“` ISO ALPHA 2 country code of the phone number

  • “‘phone_number“` E.164 formatted phone number

  • “‘type“` The type of number. This can be “`“mobile”“` or

“‘“landline”“`

  • “‘carrier“` Holds information about the specific carrier (if

available)

- ```name``` The carrier's name as reported by the network

looked up

Parameters:

  • phone_number (String)

    Required parameter: The phone number to be

  • options (String) (defaults to: nil)

    Optional parameter: Example:

Returns:

  • LookupAPhoneNumberResponse response from the API call



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/message_media_lookups/controllers/lookups_controller.rb', line 53

def get_lookup_a_phone_number(phone_number,
                              options = nil)
  begin
    @logger.info("get_lookup_a_phone_number called.")

    request_url = '/v1/lookups/phone/{phone_number}'
    request_url = APIHelper.append_url_with_template_parameters(
        request_url,
        'phone_number' => phone_number
    )

    # Prepare query url.
    @logger.info("Preparing query URL for get_lookup_a_phone_number.")
    _query_builder = Configuration.base_uri.dup
    _query_builder << '/v1/lookups/phone/{phone_number}'
    _query_builder = APIHelper.append_url_with_template_parameters(
      _query_builder,
      'phone_number' => phone_number
    )
    _query_builder = APIHelper.append_url_with_query_parameters(
      _query_builder,
      {
        'options' => options
      },
      array_serialization: Configuration.array_serialization
    )
    _query_url = APIHelper.clean_url _query_builder

    # Prepare headers.
    @logger.info("Preparing headers for get_lookup_a_phone_number.")
    _headers = {
      'accept' => 'application/json',
      'user-agent' => 'messagemedia-lookups-ruby-sdk-1.1.0'
    }

    # Prepare and execute HttpRequest.
    @logger.info('Preparing and executing HttpRequest for get_lookup_a_phone_number.')
    _request = @http_client.get(
      _query_url,
      headers: _headers
    )

    apply_authentication(_request, request_url)
    # BasicAuth.apply(_request)

    _context = execute_request(_request, name: 'get_lookup_a_phone_number')

    # Validate response against endpoint and global error codes.
    @logger.info("Validating response for get_lookup_a_phone_number.")
    if _context.response.status_code == 404
      raise APIException.new(
        'Number was invalid',
        _context
      )
    end
    validate_response(_context)

    # Return appropriate response type.
    @logger.info("Returning response for get_lookup_a_phone_number.")
    decoded = APIHelper.json_deserialize(_context.response.raw_body)
    LookupAPhoneNumberResponse.from_hash(decoded)

  rescue Exception => e
    @logger.error(e)
    raise e
  end
end

#instanceObject



11
12
13
# File 'lib/message_media_lookups/controllers/lookups_controller.rb', line 11

def instance
  self.class.instance
end