module Bluekai
  class Rule < Client
    ####
    #### Classification Rules
    #### API definition can be found here
    #### https://kb.bluekai.com/display/PD/Self-Classification+Rule+API
    ####

    # Public: List the self-classification rules in your private
    # taxonomy
    #
    # sort_by:string - Enter 'status', 'id', 'created_at', 'updated_at',
    #                  or 'type' to sort the returned self-classification
    #                  rules by the specified option.
    #
    # sorting_order:string  - Enter 'asc' or 'desc' to list the returned
    #                         self-classification rules in ascending or descending
    #                         order based on the specified sort option.
    #
    # ids:integer - Returns the self-classification rule matching the specified
    #               rule ID, or returns all the self-classification rules matching
    #               the specified list of rule IDs. Syntax for passing multiple rule IDs:
    #               ruleId1&id=ruleId2 Example: 123&id=125
    #
    # type:enum  - Enter 'phint' or 'url' to return only the phint or
    #              URL-based self-classification rules.
    #
    # site_ids:string  - Returns all the self-classification rules under the
    #                    specified site ID or list of site IDs. Syntax for
    #                    passing multiple site IDs: site_id_1&site_ids=site_id_2
    #                    Example: 1234&site_ids=1235
    #
    # category_ids:string  - Returns all the self-classification rules used
    #                        to map the specified catgeory ID or list of category IDs.
    #                        Syntax for passing multiple category
    #                        IDs: category_id_1&category_ids=category_id_2
    #                        Example: 1234&category_ids=1235
    #
    # offset:integer - Specify the starting index from which to return the
    #                  self-classification rules.
    #
    # size:integer - Specify the maximum number of rules to be included in
    #                the response. This filter requires the offset filter
    #                to be specified.
    #
    # created_date_range:string - Returns all the self-classification rules
    #                             created within the specified list of dates.
    #                             Syntax: YYYY-MM-DD&created_date_range=YYYY-MM-DD
    #                             Example: 2014-01-01&created_date_range=2014-31-01
    #
    # updated_date_range:string  - Returns all the self-classification rules updated
    #                              within the specified list of dates.
    #                              Syntax: YYYY-MM-DD&updated_date_range=YYYY-MM-DD
    #                              Example: 2014-01-01&updated_date_range=2014-31-01
    #
    # status:string - Enter 'Active' or 'Creating' to return the
    #                 self-classification rules based on the specified status referrer
    #                 boolean  Returns all URL-based self-classification rules that
    #                 classify the site URL (False) or the referrer URL (True) in
    #                 the collected URL.
    #
    # exact:boolean - Returns all URL-based self-classification rules that classify
    #                 an exact URL (True) or a top-level URL (False) in the collected URL.
    # Returns: hash of Bluekai rules
    def rule_list(query)
      request('GET', '/Services/WS/classificationRules', query)[:rules]
    end

    # Public: Reads a self-classification rule
    #
    # rule_id:integer - The unique ID assigned to the
    # self-classification rule to be retrieved
    #
    # Returns: hash of Blukkai rule parameters
    def rule_read(rule_id)
      request('GET', "/Services/WS/classificationRules/#{rule_id}", {})
    end

    # Public: Creates a new self-classification rule
    #
    # name:string - Enter a string specifying the name of the self-classification rule.
    #
    # type:string - {'phint', 'url'} Specify the type of classification rule.
    #
    # phints:[{phint}] - If you are creating a phint-based rule,
    #                    enter a list of your phint definitions.
    #                    Each phint requires the following properties:
    #                    key - The phint key operator - The criteria
    #                    used for determining how the phint value
    #                    is applied ('is' or 'contains')
    #                    value - The full or partial phint value,
    #                    depending on the specified operator.
    #
    # urls:[string(s)] - Provide a list of your URL definitions
    #                    if you are creating for URL-based rules.
    #
    # referrer:string - {'True','False'} If you are creating a
    #                   URL-based rule, specify whether the URL to
    #                   be classified is the site URL (False) or
    #                   the referrer URL (True).
    #
    # exact:string - {'True','False'} If you are creating a
    #                URL-based rule, specify whether the URL collected
    #                from your site must match the URL in your
    #                rule (True) or match a top-level URL (False) so
    #                that you can target users visiting the child pages
    #                without specifying them.
    #
    # partner_id:integer - Enter the unique ID assigned to your BlueKai DMP seat.
    #
    # site_ids (optional):[interger(s)] - Enter a list of containers/site IDs to which
    #                                     the self-classification rule applies. If
    #                                     you do not include this parameter, the
    #                                     rule is applicable to ALL the
    #                                     container/site IDs in your seat.
    #
    # category_ids:[integer(s)] - a list of category IDs to which
    #                             the self-classification rule applies.
    #
    # JSON example for Phint-based self-classification rule
    # {
    # "name": "Phint Example",
    # "type": "phint",
    # "phints": [
    # {
    # "key": "x",
    # "value": "123",
    # "operator": "is"
    # }
    # ],
    # "partner_id": 123,
    # "site_ids": [1234],
    # "category_ids": [12345]
    # }
    #
    # JSON example for URL-based self-classiifcation rule
    # {
    # "name": "URL Example",
    # "type": "url",
    # "urls": ["http://shop.yoursite.com"],
    # "referrer": false,
    # "exact": false,
    # "partner_id": 123,
    # "site_ids": [1234],
    # "category_ids": [123456]
    # }
    # Returns: hash of created self-classification rule
    def rule_create(body)
      body = { partner_id: @partner_id }.merge(body)
      request('POST', '/Services/WS/classificationRules', {}, body)
    end

    # Public: Update a self-classification rule
    #
    # rule_id:integer (MANDATORY) - id of classification rule to be updated
    #
    # for other parameters refer to documentation of rule_create(body)
    #
    # Returns: hash of updated self-classification rule
    def rule_update(rule_id, body)
      body = { partner_id: @partner_id }.merge(body)
      request('PUT', "/Services/WS/classificationRules/#{rule_id}", {}, body)
    end
  end
end