<commit_before>require 'uri'

module Agents
  class AftershipAgent < Agent

    API_URL = 'https://api.aftership.com/v4'

    description <<-MD
      The Aftership agent allows you to track your shipment from aftership and emit them into events.

      To be able to use the Aftership API, you need to generate an `API Key`. You need a paying plan to use their tracking feature.

      You can use this agent to retrieve tracking data. You have to provide a specific `get` request and its associated option.
 
      To get all trackings for your packages please enter `get` for key and `/trackings` for the option.
      To get tracking for a specific tracking number, add the extra options `slug`, `tracking_number` and set `single_tracking_request` to true.

      To get all tracking results for backup purpose set key to `get` and option to `/trackings/export`.

      To get the last checkpoint of a package set key to `get` and option to `/last_checkpoint` plus provide `slug` and `tracking_number`

      `slug` is a unique courier code. 

      You can get a list of courier information here `https://www.aftership.com/courier`

      Required Options:

      * `Content-Type` application/json
      * `api_key` - YOUR_API_KEY.
      * `get/delete and its associated options`
    MD

    event_description <<-MD
      A typical tracking event has 3 objects (attributes, tracking, and checkpoint) and it looks like this
          {
        "meta": {
            "code": 200
        },
        "data": {
            "page": 1,
            "limit": 100,
            "count": 3,
            "keyword": "",
            "slug": "",
            "origin": [],
            "destination": [],
            "tag": "",
            "fields": "",
            "created_at_min": "2014-03-27T07:36:14+00:00",
            "created_at_max": "2014-06-25T07:36:14+00:00",
            "trackings": [
                {
                    "id": "53aa7b5c415a670000000021",
                    "created_at": "2014-06-25T07:33:48+00:00",
                    "updated_at": "2014-06-25T07:33:55+00:00",
                    "tracking_number": "123456789",
                    "tracking_account_number": null,
                    "tracking_postal_code": null,
                    "tracking_ship_date": null,
                    "slug": "dhl",
                    "active": false,
                    "custom_fields": {
                        "product_price": "USD19.99",
                        "product_name": "iPhone Case"
                    },
                    "customer_name": null,
                    "destination_country_iso3": null,
                    "emails": [
                        "<EMAIL>",
                        "<EMAIL>"
                    ],
                    "expected_delivery": null,
                    "note": null,
                    "order_id": "ID 1234",
                    "order_id_path": "http://www.aftership.com/order_id=1234",
                    "origin_country_iso3": null,
                    "shipment_package_count": 0,
                    "shipment_type": null,
                    "signed_by": "raul",
                    "smses": [],
                    "source": "api",
                    "tag": "Delivered",
                    "title": "Title Name",
                    "tracked_count": 1,
                    "unique_token": "<KEY>",
                    "checkpoints": [
                        {
                            "slug": "dhl",
                            "city": null,
                            "created_at": "2014-06-25T07:33:53+00:00",
                            "country_name": "VALENCIA - SPAIN",
                            "message": "Awaiting collection by recipient as requested",
                            "country_iso3": null,
                            "tag": "InTransit",
                            "checkpoint_time": "2014-05-12T12:02:00",
                            "coordinates": [],
                            "state": null,
                            "zip": null
                       },
                        ...
                    ]
                },
                ...
            ]
        }
     }
    MD

    def default_options
      { 'api_key' => 'YOUR_API_KEY',
        'Content_Type' => 'application/json',
        'get' => '/trackings'
      }
    end

    def single_tracking_request?
      boolify(interpolated[:single_tracking_request])
    end

    def last_checkpoint?
      boolify(interpolated[:last_checkpoint_request])
    end

    def working?
      true
    end

    def validate_options
      errors.add(:base, "You need to specify a api key") unless options['api_key'].present?
      errors.add(:base, "Content-Type must be set to application/json") unless options['Content_Type'].present? && options['Content_Type'] == 'application/json'
    end

    def check
      if single_tracking_request? || last_checkpoint?
        response = HTTParty.get(single_or_checkpoint_tracking_url, request_options)
      else
        response = HTTParty.get(event_url, request_options)
      end
      events = JSON.parse response.body
      create_event :payload => events
    end
<commit_msg>no errors in working method<commit_after>require 'uri'

module Agents
  class AftershipAgent < Agent

    API_URL = 'https://api.aftership.com/v4'

    description <<-MD
      The Aftership agent allows you to track your shipment from aftership and emit them into events.

      To be able to use the Aftership API, you need to generate an `API Key`. You need a paying plan to use their tracking feature.

      You can use this agent to retrieve tracking data. You have to provide a specific `get` request and its associated option.
 
      To get all trackings for your packages please enter `get` for key and `/trackings` for the option.
      To get tracking for a specific tracking number, add the extra options `slug`, `tracking_number` and set `single_tracking_request` to true.

      To get all tracking results for backup purpose set key to `get` and option to `/trackings/export`.

      To get the last checkpoint of a package set key to `get` and option to `/last_checkpoint` plus provide `slug` and `tracking_number`

      `slug` is a unique courier code. 

      You can get a list of courier information here `https://www.aftership.com/courier`

      Required Options:

      * `Content-Type` application/json
      * `api_key` - YOUR_API_KEY.
      * `get/delete and its associated options`
    MD

    event_description <<-MD
      A typical tracking event has 3 objects (attributes, tracking, and checkpoint) and it looks like this
          {
        "meta": {
            "code": 200
        },
        "data": {
            "page": 1,
            "limit": 100,
            "count": 3,
            "keyword": "",
            "slug": "",
            "origin": [],
            "destination": [],
            "tag": "",
            "fields": "",
            "created_at_min": "2014-03-27T07:36:14+00:00",
            "created_at_max": "2014-06-25T07:36:14+00:00",
            "trackings": [
                {
                    "id": "53aa7b5c415a670000000021",
                    "created_at": "2014-06-25T07:33:48+00:00",
                    "updated_at": "2014-06-25T07:33:55+00:00",
                    "tracking_number": "123456789",
                    "tracking_account_number": null,
                    "tracking_postal_code": null,
                    "tracking_ship_date": null,
                    "slug": "dhl",
                    "active": false,
                    "custom_fields": {
                        "product_price": "USD19.99",
                        "product_name": "iPhone Case"
                    },
                    "customer_name": null,
                    "destination_country_iso3": null,
                    "emails": [
                        "<EMAIL>",
                        "<EMAIL>"
                    ],
                    "expected_delivery": null,
                    "note": null,
                    "order_id": "ID 1234",
                    "order_id_path": "http://www.aftership.com/order_id=1234",
                    "origin_country_iso3": null,
                    "shipment_package_count": 0,
                    "shipment_type": null,
                    "signed_by": "raul",
                    "smses": [],
                    "source": "api",
                    "tag": "Delivered",
                    "title": "Title Name",
                    "tracked_count": 1,
                    "unique_token": "xy_<PASSWORD>",
                    "checkpoints": [
                        {
                            "slug": "dhl",
                            "city": null,
                            "created_at": "2014-06-25T07:33:53+00:00",
                            "country_name": "VALENCIA - SPAIN",
                            "message": "Awaiting collection by recipient as requested",
                            "country_iso3": null,
                            "tag": "InTransit",
                            "checkpoint_time": "2014-05-12T12:02:00",
                            "coordinates": [],
                            "state": null,
                            "zip": null
                       },
                        ...
                    ]
                },
                ...
            ]
        }
     }
    MD

    def default_options
      { 'api_key' => 'YOUR_API_KEY',
        'Content_Type' => 'application/json',
        'get' => '/trackings'
      }
    end

    def single_tracking_request?
      boolify(interpolated[:single_tracking_request])
    end

    def last_checkpoint?
      boolify(interpolated[:last_checkpoint_request])
    end

    def working?
      !recent_error_logs?
    end

    def validate_options
      errors.add(:base, "You need to specify a api key") unless options['api_key'].present?
      errors.add(:base, "Content-Type must be set to application/json") unless options['Content_Type'].present? && options['Content_Type'] == 'application/json'
    end

    def check
      if single_tracking_request? || last_checkpoint?
        response = HTTParty.get(single_or_checkpoint_tracking_url, request_options)
      else
        response = HTTParty.get(event_url, request_options)
      end
      events = JSON.parse response.body
      create_event :payload => events
    end

{'fraction_non_alphanumeric': 0.10508116415329939, 'fraction_numerical': 0.03054461627125156, 'mean_word_length': 1.4539241102993166, 'pattern_counts': {'":': 112, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5091023', 'n_tokens_mistral': 3036, 'n_tokens_neox': 2696, 'n_words': 738}