Sha256: 3e9a479b93eaffc233d483c7738c1b004dc3fcb3cbfd6d38c8136909516cd250

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'uri'
require 'rest-client'

module Xdelivery
  module API
    class Base
      attr_accessor :merchant_no, :access_key

      BASE_URL = 'https://api.xdelivery.io'

      def initialize(merchant_no='', access_key='')
        self.merchant_no = merchant_no
        self.access_key = access_key
      end

      protected

      def post(path)
        RestClient.post(uri(path).to_s, post_data)
      rescue RestClient::ExceptionWithResponse => e
        e.response
      end

      def get(path)
        RestClient.get(uri(path).to_s)
      rescue RestClient::ExceptionWithResponse => e
        e.response
      end

      # [GET] query string params
      def params
        {}
      end

      # [POST] 
      def post_data
        {}
      end

      private

      def uri(path)
        uri = URI.parse("#{BASE_URL}#{path}").tap { |u| u.query = query_auth_params }
      end

      def query_auth_params
        URI.encode_www_form(auth_params.merge(params))
      end

      def auth_params
        { merchant_no: merchant_no, access_key: access_key }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xdelivery-0.1.0 lib/xdelivery/api/base.rb