Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

payload-translator

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

payload-translator

  • 0.4.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Payload translator

Using a yaml configuration you can transform a payload to another payload. Example:

# config.yaml
payload:
  id:
    $field: _id
    $formatter: to_integer
  user_name:
    $field: name
  login_type:
    $field: login_provider
    $map:
      google: GOOGLE
      twitter: TWITTER
      auth0: APP
  country:
    $fnc: get_country

Input:

// internal_payload.json

{
  "_id": 1,
  "name": "Jhon Doe",
  "login_provider": "auth0"
}
translator = PayloadTranslator::Service.new(config)
external_payload = translator.translate(internal_payload)

Return:

// external_payload.json

{
  "id": "1",
  "user_name": "Jhon Doe",
  "login_type": "APP"
}

Configure handlers

Resolve the field output with a custom function.

PayloadTranslator.configure do |config|
  config.handlers = {
    get_country: ->(payload) { payload['name'] },
  }
end

Or per service

PayloadTranslator::Service(config, handlers: get_country: ->(payload) { payload['name'] })

Configure formatters

Apply different formatters to the output.

PayloadTranslator.configure do |config|
  config.formatters = {
    uppercase: ->(value) { value.upcase },
    to_integer: ->(value) { value.to_i },
  }
end

Or formatter per service

PayloadTranslator::Service(config, formatters: to_integer: ->(value) { value.to_i })

Configure adapters configuration

Store adapter configuration

PayloadTranslator.configure do |config|
  config.adapters_configurations = {
    internal_to_external: {
      "payload" => {
        "id" => { "$field" => "_id" }
      }
    }
  }
end
translator = PayloadTranslator::Service.new(:internal_to_external)

Multiple translator

translator = PayloadTranslator::ServiceMultiple.new([:internal_to_external, :external_to_internal])

translated_response = translator.translate({"user_id" => 4 }) do |translated_payload|
  response = Net::HTTP.post_form(
    URI("https://jsonplaceholder.typicode.com/todos"),
    translated_payload
  ).body

  JSON.parse(response)
end

Complex adapters

Translate all array items, use $field_for_all_items special propery of the first elemtn of the array to apply the same translation from the same field Config:

payload:
  countries:
    - $field_for_all_items: countries
      name:
        $field: 'name'

Input:

{
  "countries": [
    {
      "name": "US",
      "code": "US_CODE"
    },
    {
      "name": "AU",
      "code": "AU_CODE"
    }
  ]
}

Result:

{
  "countries": [
    {
      "name": "US"
    },
    {
      "name": "AU"
    }
  ]
}

FAQs

Package last updated on 20 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc