lib/moip2/webhooks_api.rb in moip2-1.0.0 vs lib/moip2/webhooks_api.rb in moip2-1.1.0

- old
+ new

@@ -4,14 +4,30 @@ def initialize(client) @client = client end - def base_path - "/v2/webhooks" + def find_all(limit: nil, offset: nil, resource_id: nil, event: nil) + # `URI.encode...` will accept nil params, but they will pollute the URI + params = { + limit: limit, + offset: offset, + resourceId: resource_id, + event: event, + }.reject { |_, value| value.nil? } + + query_string = URI.encode_www_form(params) + path = "#{base_path}?#{query_string}" + response = client.get(path) + + # We need to transform raw JSON in Webhooks objects + response.webhooks.map! { |webhooks| Resource::Webhooks.new client, webhooks } + Resource::Webhooks.new client, response end - def show - Resource::Webhooks.new(client, client.get(base_path.to_s)) + private + + def base_path + "/v2/webhooks" end end end