Class: SnapSearch::Client

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

Overview

The Client sends an authenticated HTTP request to the SnapChat API and returns the content field from the JSON response body.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Create a new Client instance.

Parameters:

  • options (Hash, #to_h) (defaults to: {})

    The options to create the Client with.

Options Hash (options):

  • :email (String, #to_s)

    The email to authenticate with.

  • :key (String, #to_s)

    The secret authentication key.

  • :parameters (Hash, #to_h) — default: {}

    The parameters to send with the HTTP request.

  • :api_url (String, #to_s) — default: https://snapsearch.io/api/v1/robot

    The URL to send the HTTP request to.

  • :ca_cert_file (String, #to_s) — default: ROOT/resources/cacert.pem

    The CA cert file to use with request.



22
23
24
# File 'lib/snap_search/client.rb', line 22

def initialize(options={})
    initialize_attributes(options)
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



12
13
14
# File 'lib/snap_search/client.rb', line 12

def api_url
  @api_url
end

#ca_cert_fileObject

Returns the value of attribute ca_cert_file.



12
13
14
# File 'lib/snap_search/client.rb', line 12

def ca_cert_file
  @ca_cert_file
end

#emailObject

Returns the value of attribute email.



12
13
14
# File 'lib/snap_search/client.rb', line 12

def email
  @email
end

#keyObject

Returns the value of attribute key.



12
13
14
# File 'lib/snap_search/client.rb', line 12

def key
  @key
end

#parametersObject

Returns the value of attribute parameters.



12
13
14
# File 'lib/snap_search/client.rb', line 12

def parameters
  @parameters
end

Instance Method Details

#request(url) ⇒ String

Send an authenticated HTTP request to the api_url and return the content field from the JSON response body.

Parameters:

  • url (String, #to_s)

    The url to send in the parameters to the api_url.

Returns:

  • (String)

    The content field from the JSON response body.

Raises:

  • (TypeError)


82
83
84
85
86
87
# File 'lib/snap_search/client.rb', line 82

def request(url)
    raise TypeError, 'url must be a String or respond_to #to_s' unless url.is_a?(String) || url.respond_to?(:to_s)
    @parameters['url'] = url.to_s # The URL must contain the entire URL with the _escaped_fragment_ parsed out
    
    content_from_response(send_request)
end