Class: LitmosApiClient::API

Inherits:
Object
  • Object
show all
Includes:
Courses, Teams, Users
Defined in:
lib/litmos_api_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, #reset_user_course

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, #delete_user, #find_user_by_id, #update_user, #users

Constructor Details

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

Returns a new instance of API.

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/litmos_api_client.rb', line 20

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/litmos_api_client.rb', line 153

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



34
35
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
# File 'lib/litmos_api_client.rb', line 34

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



70
71
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
# File 'lib/litmos_api_client.rb', line 70

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



111
112
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
150
151
# File 'lib/litmos_api_client.rb', line 111

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
      puts response.code
      # 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