Permalink
Browse files

Moved StringHelpers into LitmosClient::API, added ability to parse AS…

…P.NET date strings, refactored parsing into parse_response method
  • Loading branch information...
1 parent 7332296 commit 8bacfe927f07f03c7282e6718f6f1225eb95fe52 @kennon committed Sep 13, 2012
Showing with 58 additions and 11 deletions.
  1. +58 −11 lib/litmos_client.rb
View
@@ -34,18 +34,28 @@ def initialize(api_key, source, config = {})
end
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
- return response.blank? ? true : StringHelpers.convert_hash_keys(JSON.parse(response))
-
+ 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)
@@ -63,6 +73,8 @@ 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,
@@ -74,7 +86,16 @@ def post(path, params={}, query_params={})
when 200, 201
# 200 Success. User/Course etc updated, deleted or retrieved
# 201 Success. User/Course etc created
- return response.blank? ? true : StringHelpers.convert_hash_keys(JSON.parse(response))
+
+ 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)
@@ -90,17 +111,29 @@ def post(path, params={}, query_params={})
end
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
- return response.blank? ? true : StringHelpers.convert_hash_keys(JSON.parse(response))
+
+ 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)
@@ -113,27 +146,40 @@ def delete(path, params={})
end
end
- end
+ end
+
+ protected
+
+ ASP_DATE_REGEXP=/\/Date\(([0-9]+)\+[0-9]+\)\//
+
+ def parse_asp_date(asp_date)
+ DateTime.strptime(asp_date.gsub(ASP_DATE_REGEXP, '\1'), '%Q')
+ end
- end
-
- module StringHelpers
# for de-camelCasing the result keys
# from: http://stackoverflow.com/questions/8706930/converting-nested-hash-keys-from-camelcase-to-snake-case-in-ruby
- def self.underscore(string)
+ 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
- def self.underscore_key(k)
+ def underscore_key(k)
underscore(k.to_s).to_sym
end
- def self.convert_hash_keys(value)
+ def parse_response(response)
+ convert_hash_keys(JSON.parse(response))
+ end
+
+ 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) }
@@ -144,6 +190,7 @@ def self.convert_hash_keys(value)
value
end
end
+
end
end

0 comments on commit 8bacfe9

Please sign in to comment.