From ca9f929dcca26c04d28a925210ce8b34252f7854 Mon Sep 17 00:00:00 2001 From: saxaathlete Date: Wed, 7 Jun 2017 09:59:56 +0200 Subject: [PATCH 1/2] PD-3554: Add webhook api --- lib/seoshop-api/api/catalog.rb | 2 +- lib/seoshop-api/api/webhook.rb | 33 +++++++++++++++++++++++++ lib/seoshop-api/client.rb | 2 ++ lib/seoshop-api/core/response_parser.rb | 2 ++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 lib/seoshop-api/api/webhook.rb diff --git a/lib/seoshop-api/api/catalog.rb b/lib/seoshop-api/api/catalog.rb index d412ebf..66b8266 100644 --- a/lib/seoshop-api/api/catalog.rb +++ b/lib/seoshop-api/api/catalog.rb @@ -5,7 +5,7 @@ module Seoshop module Catalog def get_catalog(catalog_id = nil) response = catalog_id.nil? ? get("#{@shop_language}/catalog.json") : get("#{@shop_language}/catalog/#{catalog_id}.json") - response.body ? response.body['products'] : false + response.body ? response.body[catalog_id.nil? ? 'products' : 'product'] : false end def get_catalog_count diff --git a/lib/seoshop-api/api/webhook.rb b/lib/seoshop-api/api/webhook.rb new file mode 100644 index 0000000..49508cd --- /dev/null +++ b/lib/seoshop-api/api/webhook.rb @@ -0,0 +1,33 @@ +module Seoshop + module Webhook + def get_webhooks_count(params = {}) + response = get("#{@shop_language}/webhooks/count.json", params) + response.body ? response.body['count'].to_i : 0 + end + + def get_webhooks(params = {}) + response = get("#{@shop_language}/webhooks.json", params) + response.body ? response.body['webhooks'] || [] : [] + end + + def create_webhook(params, options = {}) + response = post("#{@shop_language}/webhooks.json", { "webhook" => params }) + response.status == 201 + end + + def update_webhook(id, params) + response = put("#{@shop_language}/webhooks/#{id}.json", { "webhook" => params } ) + response.body ? response.body['webhook'] : {} + end + + def get_webhook(id) + response = get("#{@shop_language}/webhooks/#{id}.json") + response.body ? response.body['webhook'] : nil + end + + def delete_webhook(id) + response = delete("#{@shop_language}/webhooks/#{id}.json") + true + end + end +end diff --git a/lib/seoshop-api/client.rb b/lib/seoshop-api/client.rb index f7f5f50..a980fd9 100644 --- a/lib/seoshop-api/client.rb +++ b/lib/seoshop-api/client.rb @@ -16,6 +16,7 @@ require 'seoshop-api/api/shop_script' require 'seoshop-api/api/shop_tracking' require 'seoshop-api/api/catalog' +require 'seoshop-api/api/webhook' require 'seoshop-api/fetch_resource_helper' module Seoshop @@ -32,6 +33,7 @@ class Client include Seoshop::Tax include Seoshop::Catalog include Seoshop::FetchResourceHelper + include Seoshop::Webhook attr_accessor :api_key attr_accessor :api_secret diff --git a/lib/seoshop-api/core/response_parser.rb b/lib/seoshop-api/core/response_parser.rb index b1bd12a..35ae6c4 100644 --- a/lib/seoshop-api/core/response_parser.rb +++ b/lib/seoshop-api/core/response_parser.rb @@ -9,6 +9,8 @@ def call(env) body = false if env[:status] == 200 body = env[:response].body.response || env[:response].body + elsif env[:status] == 204 + body = env[:response].body elsif env[:status] == 401 raise Seoshop::ResponseParser::HTTPUnauthorized.new 'invalid Seoshop credentials' elsif env[:status] == 403 From 66d39685f8735ec739f6bacd41812bb1060e7a89 Mon Sep 17 00:00:00 2001 From: saxaathlete Date: Wed, 7 Jun 2017 12:03:09 +0200 Subject: [PATCH 2/2] PD-3554: Fix webhook destroy method --- lib/seoshop-api/api/webhook.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/seoshop-api/api/webhook.rb b/lib/seoshop-api/api/webhook.rb index 49508cd..cc77364 100644 --- a/lib/seoshop-api/api/webhook.rb +++ b/lib/seoshop-api/api/webhook.rb @@ -27,7 +27,7 @@ def get_webhook(id) def delete_webhook(id) response = delete("#{@shop_language}/webhooks/#{id}.json") - true + response.status == 204 end end end