Class: SocialPlus::WebApi::Client

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

Overview

A class which wraps calls to Social Plus Web API

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    A Social Plus API key

Raises:

  • (ArgumentError)

    when ‘api_key` is invalid



23
24
25
26
27
# File 'lib/social_plus/web_api/client.rb', line 23

def initialize(api_key)
  raise ArgumentError, 'invalid API key' unless API_KEY_RE =~ api_key
  @api_key = api_key.freeze
  freeze
end

Instance Method Details

#execute(method, parameters = {}) ⇒ Hash

Executes a Social Plus Web API

Parameters:

  • method (String, Symbol)

    An API method name

  • parameters (Hash) (defaults to: {})

    Parameters to API except ‘key`

Options Hash (parameters):

  • :via (Symbol)

    HTTP method(default ‘:get`)

Returns:

  • (Hash)

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

Raises:

  • (ApiError)

    when the API returns a status other than 200 OK



37
38
39
40
41
42
43
44
45
46
# File 'lib/social_plus/web_api/client.rb', line 37

def execute(method, parameters={})
  parameters = parameters.with_indifferent_access
  http_method = parameters.delete(:via) || :get
  response = request(http_method, method, parameters.merge(key: @api_key))
  result = parse_as_json(response.body)

  ApiError.exception_from_api_result(response, result) unless response.is_a?(Net::HTTPOK)

  result.except('status')
end