ソースを参照

Fix JSON error handling

Denis Odorcic 13 年 前
コミット
655051370a
2 ファイル変更22 行追加0 行削除
  1. 21 0
      lib/active_resource/json_errors.rb
  2. 1 0
      lib/shopify_api.rb

+ 21 - 0
lib/active_resource/json_errors.rb

@@ -0,0 +1,21 @@
+require 'active_resource/base'
+
+module ActiveResource
+  class Errors < ActiveModel::Errors
+    def from_hash(messages, save_cache = false)
+      clear unless save_cache
+
+      messages.each do |key,errors|
+        errors.each do |error|
+          add(key, error)
+        end
+      end
+    end
+
+    # Grabs errors from a json response.
+    def from_json(json, save_cache = false)
+      hash = ActiveSupport::JSON.decode(json)['errors'] || {} rescue {}
+      from_hash hash, save_cache
+    end
+  end
+end

+ 1 - 0
lib/shopify_api.rb

@@ -7,6 +7,7 @@ require 'base64'
 require 'active_resource/connection_ext'
 require 'shopify_api/limits'
 require 'shopify_api/json_format'
+require 'active_resource/json_errors'
 
 module ShopifyAPI
   include Limits