Class: Unloq::Client
- Inherits:
-
Object
- Object
- Unloq::Client
- Includes:
- Achievements, Activity, Events, Ratings, Scoreboards
- Defined in:
- lib/unloq/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
-
#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.
-
#get(base_endpoint, resource_scope = nil, additional_options: {}) ⇒ Object
Make a get request to the Unloq API.
-
#initialize(api_key: nil, namespace: nil) ⇒ Client
constructor
Initialize the client class that will be used for all Unloq interactions.
-
#post(endpoint, body = {}) ⇒ Object
Make a post request to the Unloq API.
-
#validate_author(author) ⇒ Object
Validate that any author used is of the appropriate type.
-
#validate_recipient(recipient) ⇒ Object
Validate that any recipient used is of the appropriate type.
Methods included from Activity
Methods included from Ratings
Methods included from Scoreboards
Methods included from Achievements
Methods included from Events
Constructor Details
#initialize(api_key: nil, namespace: nil) ⇒ Client
Initialize the client class that will be used for all Unloq interactions.
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_key ⇒ Object (readonly)
Returns the value of attribute api_key.
10 11 12 |
# File 'lib/unloq/client.rb', line 10 def api_key @api_key end |
#namespace ⇒ Object (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 (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
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}", end format_response_or_raise_error(response) end |
#post(endpoint, body = {}) ⇒ Object
Make a post request to the Unloq API
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
58 59 60 |
# File 'lib/unloq/client.rb', line 58 def raise ArgumentError.new("Author must be an Unloq::Author or Unloq::Recipient") unless .is_a?(Unloq::Entity) end |
#validate_recipient(recipient) ⇒ Object
Validate that any recipient used is of the appropriate type
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 |