Преглед изворни кода

Merge pull request #111 from Shopify/add_customer_search

Add convenience method Customer#search
Peter Schröder пре 11 година
родитељ
комит
ef104d7fc7
4 измењених фајлова са 75 додато и 1 уклоњено
  1. 1 0
      CHANGELOG
  2. 4 1
      lib/shopify_api/resources/customer.rb
  3. 10 0
      test/customer_test.rb
  4. 60 0
      test/fixtures/customers_search.json

+ 1 - 0
CHANGELOG

@@ -3,6 +3,7 @@
 * in Session::request_token params is no longer optional, you must pass all the params and the method will now extract the code
 * Add access to FulfillmentService endpoint
 * Fix JSON errors handling (#103)
+* Add convenience method Customer#search (#45)
 
 == Version 3.1.8
 

+ 4 - 1
lib/shopify_api/resources/customer.rb

@@ -3,8 +3,11 @@ module ShopifyAPI
     include Metafields
 
     def orders
-      Order.find(:all, :params => {:customer_id => self.id})
+      Order.find(:all, params: {customer_id: self.id})
     end
 
+    def self.search(params)
+      find(:all, from: :search, params: params)
+    end
   end
 end

+ 10 - 0
test/customer_test.rb

@@ -0,0 +1,10 @@
+require 'test_helper'
+
+class CustomerTest < Test::Unit::TestCase
+  def test_search
+    fake "customers/search.json?query=Bob+country%3AUnited+States", extension: false, body: load_fixture('customers_search')
+
+    results = ShopifyAPI::Customer.search(query: 'Bob country:United States')
+    assert_equal 'Bob', results.first.first_name
+  end
+end

+ 60 - 0
test/fixtures/customers_search.json

@@ -0,0 +1,60 @@
+{
+  "customers": [
+    {
+      "accepts_marketing": false,
+      "created_at": "2014-01-20T17:25:18-05:00",
+      "email": "bob.norman@hostmail.com",
+      "first_name": "Bob",
+      "id": 207119551,
+      "last_name": "Norman",
+      "last_order_id": null,
+      "multipass_identifier": null,
+      "note": null,
+      "orders_count": 0,
+      "state": "disabled",
+      "total_spent": "0.00",
+      "updated_at": "2014-01-20T17:25:18-05:00",
+      "verified_email": true,
+      "tags": "",
+      "last_order_name": null,
+      "default_address": {
+        "address1": "Chestnut Street 92",
+        "address2": "",
+        "city": "Louisville",
+        "company": null,
+        "country": "United States",
+        "first_name": null,
+        "id": 207119551,
+        "last_name": null,
+        "phone": "555-625-1199",
+        "province": "Kentucky",
+        "zip": "40202",
+        "name": null,
+        "province_code": "KY",
+        "country_code": "US",
+        "country_name": "United States",
+        "default": true
+      },
+      "addresses": [
+        {
+          "address1": "Chestnut Street 92",
+          "address2": "",
+          "city": "Louisville",
+          "company": null,
+          "country": "United States",
+          "first_name": null,
+          "id": 207119551,
+          "last_name": null,
+          "phone": "555-625-1199",
+          "province": "Kentucky",
+          "zip": "40202",
+          "name": null,
+          "province_code": "KY",
+          "country_code": "US",
+          "country_name": "United States",
+          "default": true
+        }
+      ]
+    }
+  ]
+}