Module: RGigya
- Included in:
- SigUtils
- Defined in:
- lib/rgigya/sig_utils.rb,
lib/rgigya/base.rb
Overview
if you think about it as a namespace the include RGigya below doesn’t seem weird at all
Defined Under Namespace
Classes: SigUtils
Constant Summary collapse
- JSONParseError =
Class.new(JSON::ParserError)
- @@valid_methods =
List of Available API methods
[:socialize, :gm, :comments, :accounts, :reports, :chat, :ds]
- @@base_signature_string =
Used to compare when we get a bad signature, mainly for debugging but helpful
""
- @@signature =
""
Class Method Summary collapse
-
.build_url(method, http_method, options = {}) ⇒ String
builds the url to be sent to the api.
-
.check_for_errors(results) ⇒ String
Error handling of the results.
-
.config(config_data) ⇒ Object
Sets the config data to be used in the api call.
-
.log(log_str) ⇒ Object
Custom log method, if we are in rails we should log any errors for debugging purposes.
-
.method_missing(sym, *args) ⇒ Object
Override method_missing so we don’t have to write all the dang methods.
-
.params_with_signature(request_uri, params) ⇒ Hash
Adds Timestamp, nonce and signatures to the params hash.
-
.parse_results(method, options = {}) ⇒ Hash
sends the api call to gigya and parses the result with appropriate method.
-
.parse_results_secure(method, options) ⇒ Hash
sends the https call to gigya and parses the result This is used for https get requests.
-
.parse_results_with_signature(method, options) ⇒ Hash
sends the http call with signature to gigya and parses the result This is for http post requests.
-
.prepare_for_signature(h) ⇒ Hash
Orders the params hash for the signature Changes boolean values to their string equivalent.
-
.required_parameters ⇒ Object
Adds the required params for all api calls.
-
.respond_to?(sym, include_private = false) ⇒ Boolean
Override respond_to? We can’t really give an accurate return here I am only allowing those methods that start with the methods listed in the @@valid_methods array.
-
.verify_config_data ⇒ Object
Validates that we have required config data.
Class Method Details
.build_url(method, http_method, options = {}) ⇒ String
builds the url to be sent to the api
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rgigya/base.rb', line 96 def build_url(method, http_method, = {}) if && .has_key?(:uid) && [:uid].nil? raise RGigya::UIDParamIsNil, "" end if && .has_key?(:siteUID) && [:siteUID].nil? raise RGigya::SiteUIDParamIsNil, "" end method_type,method_name = method.split(".") if(http_method == "GET") url = "https://#{method_type}.#{@@domain}.gigya.com/#{method}?#{required_parameters}" if() .each do |key,value| url += "&#{key}=#{CGI.escape(value.to_s)}" end end else url = "http://#{method_type}.#{@@domain}.gigya.com/#{method}" end url end |
.check_for_errors(results) ⇒ String
Error handling of the results
TODO: Shouldn’t fail so hard. If there is a temporary connectivity problem we should fail more gracefully. You can find a list of response codes at developers.gigya.com/037_API_reference/zz_Response_Codes_and_Errors
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/rgigya/base.rb', line 263 def check_for_errors(results) case results['errorCode'].to_s when '0' return results when '400124' #Limit Reached error - don't fail so bad when '400002' raise RGigya::BadParamsOrMethodName, results['errorDetails'] when '403003' log("RGigya returned Error code #{results['errorCode']}.\n\nError Message: #{results['errorMessage']}\n\nError Details: #{results['errorDetails']}\n\n") log("Rgigya base_signature_string = #{@@base_signature_string}\n\n") log("Gigya base_signature_string = #{results['baseString']}\n\n\n") log("Rgigya signature = #{@@signature}\n\n") log("Gigya signature = #{results['expectedSignature']}\n\n") raise RGigya::ErrorCodeReturned, "returned Error code #{results['errorCode']}: #{results['errorMessage']}" else log("RGigya returned Error code #{results['errorCode']}.\n\nError Message: #{results['errorMessage']}\n\nError Details: #{results['errorDetails']}") raise RGigya::ErrorCodeReturned, "returned Error code #{results['errorCode']}: #{results['errorMessage']}" end end |
.config(config_data) ⇒ Object
Sets the config data to be used in the api call
57 58 59 60 61 62 63 64 |
# File 'lib/rgigya/base.rb', line 57 def config(config_data) @@api_key = config_data[:api_key] @@api_secret = config_data[:api_secret] @@use_ssl = config_data[:use_ssl] || false @@domain = config_data[:domain] || "us1" verify_config_data end |
.log(log_str) ⇒ Object
Custom log method, if we are in rails we should log any errors for debugging purposes
332 333 334 335 336 337 338 |
# File 'lib/rgigya/base.rb', line 332 def log(log_str) if Object.const_defined?('Rails') Rails.logger.info(log_str) else puts log_str end end |
.method_missing(sym, *args) ⇒ Object
Override method_missing so we don’t have to write all the dang methods
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/rgigya/base.rb', line 291 def method_missing(sym, *args) method = sym.to_s.gsub("_",".") method_type,method_name = method.split(".") if(@@valid_methods.include?(method_type.to_sym)) results = parse_results(method, args.first) else results = false end if results return check_for_errors(results) else super end end |
.params_with_signature(request_uri, params) ⇒ Hash
Adds Timestamp, nonce and signatures to the params hash
with timestamp, nonce and signature added
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/rgigya/base.rb', line 181 def params_with_signature(request_uri,params) = Time.now.utc.strftime("%s") nonce = SigUtils::current_time_in_milliseconds() params = {} if params.nil? params[:format] = "json" params[:timestamp] = params[:nonce] = nonce params[:apiKey] = @@api_key normalized_url = CGI.escape(request_uri) query_string = CGI.escape(prepare_for_signature(params).to_query) # signature_string = SECRET + request_uri + timestamp @@base_signature_string = "POST&#{normalized_url}&#{query_string}" digest = SigUtils::calculate_signature(@@base_signature_string,@@api_secret) @@signature = digest.to_s params[:sig] = @@signature return params end |
.parse_results(method, options = {}) ⇒ Hash
sends the api call to gigya and parses the result with appropriate method
246 247 248 249 |
# File 'lib/rgigya/base.rb', line 246 def parse_results(method, = {}) verify_config_data return @@use_ssl ? parse_results_secure(method,) : parse_results_with_signature(method,) end |
.parse_results_secure(method, options) ⇒ Hash
sends the https call to gigya and parses the result This is used for https get requests
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rgigya/base.rb', line 129 def parse_results_secure(method,) # options = {} if options.is_a?(String) && options.blank? begin response = HTTParty.get(build_url(method, "GET", ),{:timeout => 10}) rescue SocketError,Timeout::Error => e raise RGigya::ResponseError, e. end return false if response.nil? || response.body == "Bad Request" begin doc = JSON(response.body) rescue JSON::ParserError => e raise RGigya::JSONParseError, e. end doc end |
.parse_results_with_signature(method, options) ⇒ Hash
sends the http call with signature to gigya and parses the result This is for http post requests
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/rgigya/base.rb', line 216 def parse_results_with_signature(method, ) request_uri = build_url(method, "POST", ) begin response = HTTParty.post(request_uri, { :body => params_with_signature(request_uri,) }) rescue URI::InvalidURIError # need to treat this like method missing return false rescue SocketError,Timeout::Error => e raise RGigya::ResponseError, e. end begin doc = JSON(response.body) rescue JSON::ParserError => e raise RGigya::JSONParseError, e. end doc end |
.prepare_for_signature(h) ⇒ Hash
Orders the params hash for the signature Changes boolean values to their string equivalent
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rgigya/base.rb', line 156 def prepare_for_signature(h) ordered_hash = {} #insert order with hash is preserved since ruby 1.9.2 h = h.inject({}){|p,(k,v)| p[k.to_sym] = v; p} h.keys.sort.each do |key| value = h[key] if(!!value == value) #duck typeing.......quack ordered_hash[key] = value.to_s else ordered_hash[key] = value end end return ordered_hash end |
.required_parameters ⇒ Object
Adds the required params for all api calls
81 82 83 84 85 |
# File 'lib/rgigya/base.rb', line 81 def required_parameters params = "apiKey=#{CGI.escape(@@api_key)}" params += "&secret=#{CGI.escape(@@api_secret)}" params += "&format=json" end |
.respond_to?(sym, include_private = false) ⇒ Boolean
Override respond_to? We can’t really give an accurate return here I am only allowing those methods that start with the methods listed in the @@valid_methods array
318 319 320 321 322 |
# File 'lib/rgigya/base.rb', line 318 def respond_to?(sym, include_private = false) method = sym.to_s.gsub("_",".") method_type,method_name = method.split(".") return @@valid_methods.include?(method_type.to_sym) end |
.verify_config_data ⇒ Object
Validates that we have required config data
69 70 71 72 73 74 75 76 |
# File 'lib/rgigya/base.rb', line 69 def verify_config_data if(!defined?(@@api_key)) raise RGigya::MissingApiKey, "Please provide a Gigya api key in the config data" end if(!defined?(@@api_secret)) raise RGigya::MissingApiSecret, "Please provide a Gigya api secret in the config data" end end |