Class: UseragentApi::Client

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

Overview

A class which wraps calls to UseragentAPI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    UseragentAPI API key

Raises:

  • (ArgumentError)

    when api_key is invalid

See Also:



13
14
15
16
17
# File 'lib/useragent_api/client.rb', line 13

def initialize(api_key)
  raise ArgumentError, 'invalid API key' if api_key.empty?
  @api_key = api_key.freeze
  freeze
end

Instance Attribute Details

#api_keyString (readonly)

Returns the api key

Returns:

  • (String)

    Returns the api key



20
21
22
# File 'lib/useragent_api/client.rb', line 20

def api_key
  @api_key
end

Instance Method Details

#parse(useragent) ⇒ Hash

Parse an useragent using UseragenAPI

Parameters:

  • useragent (String)

    an useragent

Returns:

  • (Hash)

    a Hash generated by parsing the JSON returned from the API call, just ‘{}` on parsing failure



27
28
29
30
# File 'lib/useragent_api/client.rb', line 27

def parse(useragent)
  response = request(useragent)
  parse_as_json(response.body)
end