Sha256: 4a28171ae3a35e65d69c081db5cf5c7cac27f1d37e1821a89490ba039aa4ecd9

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

module Kintone::Client::Middleware
  class Form < Faraday::Response::Middleware
    def initialize(app, options = nil)
      super(app)
    end

    def call(env)
      @app.call(env).on_complete do |env|
        if record = env[:body]['record']
          env[:body] = {
            'record' => parse_form(record)
          }
        elsif records = env[:body]['records']
          env[:body] = {
            'records' => records.map {|r| parse_form(r) }
          }
        end
      end
    end

    private

    def parse_form(form)
      parsed = {}

      form.each do |name, field|
        parsed[name] = parse_field(field)
      end

      parsed
    end

    def parse_field(field)
      field_type  = field['type']
      field_value = field['value']

      if field_type == 'SUBTABLE'
        subtable = {}

        field_value.each do |row|
          subtable[row['id']] = parse_form(row['value'])
        end

        subtable
      else
        field_value
      end
    end
  end
end

Faraday::Response.register_middleware :form => Kintone::Client::Middleware::Form

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kintone-client-0.1.3 lib/kintone/client/middleware/form.rb
kintone-client-0.1.2 lib/kintone/client/middleware/form.rb
kintone-client-0.1.1 lib/kintone/client/middleware/form.rb
kintone-client-0.1.0 lib/kintone/client/middleware/form.rb