class Badgeapi::BadgeapiObject

Public Class Methods

all(params = {}) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 106
def all params = {}
  request "get", "#{Badgeapi.api_url}/#{collection_path}", params
end
attribute_a_badge_object?(name) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 94
def attribute_a_badge_object? name
  if object_classes.has_key?(name) || object_classes.has_key?(name.singularize)
    true
  else
    false
  end
end
collection_name() click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 5
def collection_name
  name.demodulize.pluralize.underscore
end
Also aliased as: collection_path
collection_path()
Alias for: collection_name
create(params = {}) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 110
def create params = {}
  request "post", "#{Badgeapi.api_url}/#{collection_path}", member_name => params
end
destroy(id) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 118
def destroy(id)
  request "delete", "#{Badgeapi.api_url}/#{collection_path}/#{id}"
end
find(id, params = {}) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 102
def find id, params = {}
  request "get", "#{Badgeapi.api_url}/#{collection_path}/#{id}", params
end
from_response(response) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 21
def from_response response
  attributes = JSON.parse(response.body)

  if attributes.include?("error")
    handle_api_error attributes
  else
    if attributes.class == Array
      attributes.map do |subattributes|
        map_json_to_object(subattributes)
      end
    else
      map_json_to_object attributes
    end
  end
end
handle_api_error(error) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 149
def self.handle_api_error error
  error_object = error['error']

  case error_object["type"]
  when "invalid_request_error"
    raise InvalidRequestError.new(error_object["message"], error_object["status"], error)
  when "api_error"
    raise APIError.new(error_object["message"], error_object["status"], error)
  else
    raise APIError.new("Unknown error tyep #{error_object['type']}", error_object["status"], error)
  end
end
map_instant_variables_to_child_records(attributes, record) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 81
def map_instant_variables_to_child_records attributes, record
  attributes.each do |name, value|
    if value.class == Array && value.count > 0
      if attribute_a_badge_object? name
        child = map_related_object object_classes.fetch(name.singularize), value
        record.instance_variable_set "@#{name}", child
      end
    else
      record.instance_variable_set "@#{name}", value
    end
  end
end
map_instant_variables_to_record(attributes, record) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 57
def map_instant_variables_to_record attributes, record
  attributes.each do |name, value|
    if attribute_a_badge_object? name
      child = map_related_object object_classes.fetch(name.singularize), value
      record.instance_variable_set "@#{name}", child
    else
      record.instance_variable_set "@#{name}", value
    end
  end
end
map_json_to_object(attributes) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 46
def map_json_to_object attributes
  if attributes['object'].nil?
    record = new
  else
    record = object_classes.fetch(attributes['object'].singularize).new
  end

  map_instant_variables_to_record attributes, record
  record
end
member_name() click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 10
def member_name
  name.demodulize.underscore
end
object_classes() click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 37
def object_classes
  @object_classes ||= {
    'recipient'      => Recipient,
    'collection'     => Collection,
    'badge'          => Badge,
    'required_badge' =>   Badge
  }
end
request(method, url, params = {}) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 14
def request method, url, params = {}
  connection = Faraday.new(ssl: { ca_file: Badgeapi.ssl_ca_cert })

  connection.token_auth(Badgeapi.api_key)
  from_response connection.send(method, url, params)
end
update(id, params = {}) click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 114
def update id, params = {}
  request "patch", "#{Badgeapi.api_url}/#{collection_path}/#{id}", member_name => params
end

Public Instance Methods

inspect() click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 123
def inspect
  id_as_string = (respond_to?(:id) && !id.nil?) ? " id=#{id}" : ""
  "#<#{self.class}:0x#{object_id.to_s(16)}#{id_as_string}> JSON: " + to_json
end
remove_read_only_params() click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 128
def remove_read_only_params
  # Remove params that cannot be saved as they are not permitted through strong_params on api
  params = JSON.parse(to_json)

  params.delete("id")
  params.delete("created_at")
  params.delete("updated_at")
  params.delete("points")
  params.delete("total_points_available")
  params.delete("badge_count")
  params.delete("object")
end
save() click to toggle source
# File lib/badgeapi/badgeapi_object.rb, line 141
def save
  # Remove params that cannot be saved as they are not permitted through strong_params on api
  params = remove_read_only_params

  self.class.request "patch", "#{Badgeapi.api_url}/#{self.class.collection_path}/#{id}",
                     self.class.member_name => params
end