Class: LitmosClient::API

Inherits:
Object
  • Object
show all
Includes:
Courses, Teams, Users
Defined in:
lib/litmos_client.rb

Constant Summary collapse

API_VERSION =

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

"1"

Instance Method Summary collapse

Methods included from Courses

#courses, #find_course_by_id, #find_courses_by_user_id

Methods included from Teams

#add_user_to_team, #find_team_by_id, #find_users_by_team_id, #remove_user_from_team, #teams

Methods included from Users

#create_user, #find_user_by_id, #users

Constructor Details

#initialize(api_key, source, config = {}) ⇒ API

Initialize with an API key and config options

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/litmos_client.rb', line 22

def initialize(api_key, source, 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

Instance Method Details

#delete(path, params = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/litmos_client.rb', line 113

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 = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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 = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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