class LitmosClient::API

Constants

API_VERSION

Litmos Developer API Documentation: help.litmos.com/developer-api/

ASP_DATE_REGEXP

Public Class Methods

new(api_key, source = nil, config = {}) click to toggle source

Initialize with an API key and config options

# File lib/litmos_client.rb, line 22
def initialize(api_key, source = nil, config = {})
  raise ArgumentError.new('Your need to specify your api key') unless api_key
  raise ArgumentError.new('You need to specify a source website') unless source

  defaults = {
    :api_version        => API_VERSION
  }

  @config = defaults.merge(config).freeze
  @api_key = api_key
  @source = source
  @litmosURL = "https://api.litmos.com/v#{@config[:api_version]}.svc/"
end

Public Instance Methods

delete(path, params={}) click to toggle source
# File lib/litmos_client.rb, line 154
def delete(path, params={})
  dont_parse_response = params.delete(:dont_parse_response)

  options = {
    :content_type => :json, 
    :accept => :json, 
    :params => params.merge(:apikey => @api_key, :source => @source)
  }

  RestClient.delete("#{@litmosURL}/#{path}", options) do |response, request, result|
    case response.code
    when 200, 201 
      # 200 Success. User/Course etc updated, deleted or retrieved
      # 201 Success. User/Course etc created

      if response.blank?
        true
      else
        if dont_parse_response
          response
        else
          parse_response(response)
        end
      end

    when 404 # 404 Not Found. The User/Course etc that you requested does not exist
      raise NotFound.new(response)

    else
      # 400 Bad Request. Check that your Uri and request body is well formed
      # 403 Forbidden. Check your API key, HTTPS setting, Source parameter etc
      # 409 Conflict. Often occurs when trying to create an item that already exists
      raise ApiError.new(response)

    end        
  end
end
get(path, params={}) click to toggle source
# File lib/litmos_client.rb, line 36
def get(path, params={})
  dont_parse_response = params.delete(:dont_parse_response)

  options = {
    :content_type => :json, 
    :accept => :json, 
    :params => params.merge(:apikey => @api_key, :source => @source)
  }

  RestClient.get("#{@litmosURL}/#{path}", options) do |response, request, result|
    case response.code
    when 200, 201 
      # 200 Success. User/Course etc updated, deleted or retrieved
      # 201 Success. User/Course etc created
      if response.blank?
        true
      else
        if dont_parse_response
          response
        else
          parse_response(response)
        end
      end
    when 404 # 404 Not Found. The User/Course etc that you requested does not exist
      raise NotFound.new(response)

    else
      # 400 Bad Request. Check that your Uri and request body is well formed
      # 403 Forbidden. Check your API key, HTTPS setting, Source parameter etc
      # 409 Conflict. Often occurs when trying to create an item that already exists
      raise ApiError.new(response)

    end        
  end
end
post(path, params={}, query_params={}) click to toggle source
# File lib/litmos_client.rb, line 72
def post(path, params={}, query_params={})
  query_params = query_params.merge(:apikey => @api_key, :source => @source)
  query_string = query_params.collect { |k,v| "#{k}=#{CGI::escape(v)}" }.join('&')
  query_string = "?#{query_string}" unless query_string.blank?

  dont_parse_response = params.delete(:dont_parse_response)
  
  options = {
    :content_type => :json, 
    :accept => :json, 
  }

  RestClient.post("#{@litmosURL}/#{path}#{query_string}", params.to_json, options) do |response, request, result|
    case response.code
    when 200, 201 
      # 200 Success. User/Course etc updated, deleted or retrieved
      # 201 Success. User/Course etc created

      if response.blank?
        true
      else
        if dont_parse_response
          response
        else
          parse_response(response)
        end
      end

    when 404 # 404 Not Found. The User/Course etc that you requested does not exist
      raise NotFound.new(response)

    else
      # 400 Bad Request. Check that your Uri and request body is well formed
      # 403 Forbidden. Check your API key, HTTPS setting, Source parameter etc
      # 409 Conflict. Often occurs when trying to create an item that already exists
      raise ApiError.new(response)

    end        
  end
end
put(path, params={}, query_params={}) click to toggle source
# File lib/litmos_client.rb, line 113
def put(path, params={}, query_params={})
  query_params = query_params.merge(:apikey => @api_key, :source => @source)
  query_string = query_params.collect { |k,v| "#{k}=#{CGI::escape(v)}" }.join('&')
  query_string = "?#{query_string}" unless query_string.blank?

  dont_parse_response = params.delete(:dont_parse_response)
  
  options = {
    :content_type => :json, 
    :accept => :json, 
  }

  RestClient.put("#{@litmosURL}/#{path}#{query_string}", params.to_json, options) do |response, request, result|
    case response.code
    when 200, 201 
      # 200 Success. User/Course etc updated, deleted or retrieved
      # 201 Success. User/Course etc created

      if response.blank?
        true
      else
        if dont_parse_response
          response
        else
          parse_response(response)
        end
      end

    when 404 # 404 Not Found. The User/Course etc that you requested does not exist
      raise NotFound.new(response)

    else
      # 400 Bad Request. Check that your Uri and request body is well formed
      # 403 Forbidden. Check your API key, HTTPS setting, Source parameter etc
      # 409 Conflict. Often occurs when trying to create an item that already exists
      raise ApiError.new(response)

    end        
  end
end

Protected Instance Methods

convert_hash_keys(value) click to toggle source
# File lib/litmos_client.rb, line 219
def convert_hash_keys(value)
  if value.is_a?(String) and value =~ ASP_DATE_REGEXP
    return parse_asp_date(value)
  end

  case value
  when Array
    value.map { |v| convert_hash_keys(v) }
    # or `value.map(&method(:convert_hash_keys))`
  when Hash
    Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
  else
    value
   end
end
parse_asp_date(asp_date) click to toggle source
# File lib/litmos_client.rb, line 196
def parse_asp_date(asp_date)
  DateTime.strptime(asp_date.gsub(ASP_DATE_REGEXP, '\1'), '%Q')
end
parse_response(response) click to toggle source
# File lib/litmos_client.rb, line 215
def parse_response(response)
  convert_hash_keys(JSON.parse(response))
end
underscore(string) click to toggle source

for de-camelCasing the result keys from: stackoverflow.com/questions/8706930/converting-nested-hash-keys-from-camelcase-to-snake-case-in-ruby

# File lib/litmos_client.rb, line 203
def underscore(string)
  string.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end
underscore_key(k) click to toggle source
# File lib/litmos_client.rb, line 211
def underscore_key(k)
  underscore(k.to_s).to_sym
end