Sha256: 8da39ac1dd301d352750616d56815e7557bba585530015dc0640fb9fa7c2a9f3

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 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

        if totalCount = env[:body]['totalCount']
          env[:body]['totalCount'] = totalCount.to_i
        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

3 entries across 3 versions & 1 rubygems

Version Path
kintone-client-0.1.6 lib/kintone/client/middleware/form.rb
kintone-client-0.1.5 lib/kintone/client/middleware/form.rb
kintone-client-0.1.4 lib/kintone/client/middleware/form.rb