Sha256: 337f4f43a6c09024371d713ea8fb545a8e3da581fb26dac11424e3ebcdec3f50

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module Moneta
  module Api
    class Service
      DEMO = Moneta::Constants::Settings::DEMO
      PRODUCTION = Moneta::Constants::Settings::PRODUCTION
      URL = Moneta::Constants::Settings::SERVICES_PATH

      include ServiceMethods

      def initialize(username, password, params = {})
        @username = username
        @password = password
        @connection = connection(Options.new(params))
      end

      def call_method(method_id, request)
        @connection.post do |req|
          req.url URL
          req.headers['Content-Type'] = 'application/json;charset=UTF-8'
          req.body = request_body(method_id, request).to_json
        end
      end

      private

      def connection(options)
        Faraday.new(url: endpoint(options.demo?)) do |faraday|
          if options.logger
            faraday.response :logger, options.logger, bodies: options.log_bodies? do | logger |
              logger.filter(/(#{ @password })/,'[FILTERED]')

              options.filter.each do |subject, replacement|
                logger.filter(/#{ subject }/, replacement)
              end
            end
          end

          faraday.adapter :net_http do |http|
            http.open_timeout = options.open_timeout
          end
        end
      end

      def endpoint(demo)
        demo ? DEMO : PRODUCTION
      end

      def request_body(method_id, request)
        {
          'Envelope' => {
            'Header' => {
              'Security' => {
                'UsernameToken' => {
                  'Username' => @username,
                  'Password' => @password
                }
              }
            },
            'Body' => {
              method_name(method_id) => request_hash(request)
            }
          }
        }
      end

      def method_name(method_id)
        "#{ method_id.to_s.camelize }Request"
      end

      def request_hash(request)
        request.respond_to?(:to_hash) ? request.to_hash : { value: request }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
moneta-api-2.1.0 lib/moneta/api/service.rb