Class: Unloq::Client

Inherits:
Object
  • Object
show all
Includes:
Achievements, Activity, Events, Ratings, Scoreboards
Defined in:
lib/unloq/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Activity

#activity

Methods included from Ratings

#lookup_author_ratings

Methods included from Scoreboards

#scoreboards

Methods included from Achievements

#create_achievement

Methods included from Events

#create_event, #lookup_event

Constructor Details

#initialize(api_key: nil, namespace: nil) ⇒ Client

Initialize the client class that will be used for all Unloq interactions.

Parameters:

  • api_key (String) (defaults to: nil)

    Your Unloq api key.

  • namespace (String) (defaults to: nil)

    The namespace under which Unloq events should be created, e.g. appname-dev



17
18
19
20
21
22
23
24
# File 'lib/unloq/client.rb', line 17

def initialize api_key: nil, namespace: nil
  unless api_key && namespace
    raise ArgumentError.new("You must include both an api_key and namespace, e.g. Unloq::Client.new(api_key: 'foo', namespace: 'bar')")
  end

  @api_key   = api_key
  @namespace = namespace
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/unloq/client.rb', line 10

def api_key
  @api_key
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/unloq/client.rb', line 10

def namespace
  @namespace
end

Instance Method Details

#extract_entity_details(entity = nil, entity_type = nil) ⇒ Object

Extract entity type and ID from inclusion of either an Unloq::Entity or a *_type argument



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/unloq/client.rb', line 68

def extract_entity_details entity = nil, entity_type = nil
  if entity
    validate_author(entity)
    return [entity.type, entity.id]
  else
    unless entity_type
      raise ArgumentError.new("Must include an author/recipient as an Unloq::Entity instance or include an author_type or recipient_type")
    end
    return [entity_type, nil]
  end
end

#get(base_endpoint, resource_scope = nil, additional_options: {}) ⇒ Object

Make a get request to the Unloq API

Parameters:

  • base_endpoint (String)

    The base resource endpoint, e.g. /events

  • resource_scope (String) (defaults to: nil)

    The scope of the resource endpoint, e.g. /User/1/login/User/1



47
48
49
50
51
52
53
54
# File 'lib/unloq/client.rb', line 47

def get base_endpoint, resource_scope = nil, additional_options: {}

  response = connection.get do |req|
    req.url "#{base_endpoint}/#{api_key}/#{namespace}#{resource_scope}", additional_options
  end

  format_response_or_raise_error(response)
end

#post(endpoint, body = {}) ⇒ Object

Make a post request to the Unloq API

Parameters:

  • endpoint (String)

    The resource endpoint, e.g. /events

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

    The body payload that should be POSTed



31
32
33
34
35
36
37
38
39
40
# File 'lib/unloq/client.rb', line 31

def post endpoint, body = {}
  body.merge!(api_key: api_key, namespace: namespace)

  response = connection.post do |req|
    req.url endpoint
    req.body = body.to_json
  end

  format_response_or_raise_error(response)
end

#validate_author(author) ⇒ Object

Validate that any author used is of the appropriate type

Raises:

  • (ArgumentError)


58
59
60
# File 'lib/unloq/client.rb', line 58

def validate_author author
  raise ArgumentError.new("Author must be an Unloq::Author or Unloq::Recipient") unless author.is_a?(Unloq::Entity)
end

#validate_recipient(recipient) ⇒ Object

Validate that any recipient used is of the appropriate type

Raises:

  • (ArgumentError)


63
64
65
# File 'lib/unloq/client.rb', line 63

def validate_recipient recipient
  raise ArgumentError.new("Recipient must be an Unloq::Author or Unloq::Recipient") unless recipient.is_a?(Unloq::Entity)
end