Sha256: f380d1f45c92dda92b7ae7acd9c1c3519f24345f5a0a334e690059af79c2bff3

Contents?: true

Size: 1.99 KB

Versions: 12

Compression:

Stored size: 1.99 KB

Contents

module ZendeskAPI
  # Creates put, post, delete class methods for custom resource methods.
  module Verbs
    class << self
      private

      # @macro [attach] container.create_verb
      #   @method $1(method)
      #   Executes a $1 using the passed in method as a path.
      #   Reloads the resource's attributes if any are in the response body.
      #
      #   Created method takes an optional options hash. Valid options to be passed in to the created method: reload (for caching, default: false)
      def create_verb(method_verb)
        define_method method_verb do |method|
          define_method "#{method}!" do |*method_args|
            opts = method_args.last.is_a?(Hash) ? method_args.pop : {}

            if method_verb == :any
              verb = opts.delete(:verb)
              raise(ArgumentError, ":verb required for method defined as :any") unless verb
            else
              verb = method_verb
            end

            @response = @client.connection.send(verb, "#{path}/#{method}") do |req|
              req.body = opts
            end

            return false unless @response.success?
            return false unless @response.body

            resource = nil

            if @response.body.is_a?(Hash)
              resource = @response.body[self.class.singular_resource_name]
              resource ||= @response.body.fetch(self.class.resource_name, []).detect { |res| res["id"] == id }
            end

            @attributes.replace @attributes.deep_merge(resource || {})
            @attributes.clear_changes
            clear_associations

            true
          end

          define_method method do |*method_args|
            send("#{method}!", *method_args)
          rescue ZendeskAPI::Error::RecordInvalid => e
            @errors = e.errors
            false
          rescue ZendeskAPI::Error::ClientError
            false
          end
        end
      end
    end

    create_verb :put
    create_verb :post
    create_verb :delete
    create_verb :any
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
zendesk_api-3.1.1 lib/zendesk_api/verbs.rb
zendesk_api-3.1.0 lib/zendesk_api/verbs.rb
zendesk_api-3.0.5 lib/zendesk_api/verbs.rb
zendesk_api-3.0.4 lib/zendesk_api/verbs.rb
zendesk_api-3.0.2 lib/zendesk_api/verbs.rb
zendesk_api-3.0.1 lib/zendesk_api/verbs.rb
zendesk_api-3.0.0 lib/zendesk_api/verbs.rb
zendesk_api-3.0.0.rc1 lib/zendesk_api/verbs.rb
zendesk_api-2.0.1 lib/zendesk_api/verbs.rb
zendesk_api-2.0.0 lib/zendesk_api/verbs.rb
zendesk_api-1.38.0.rc2 lib/zendesk_api/verbs.rb
zendesk_api-1.38.0.rc1 lib/zendesk_api/verbs.rb