Sha256: c7dfc54e7c4d0f3e1e4760c8811b7483935fba4d47c7c07eaa677a41e68292bd

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require 'emaildirect'
require 'json'

module EmailDirect
  # Represents an OrderItem and associated functionality
  class OrderItem
    class << self
      def all(options = {})
        response = EmailDirect.get '/Orders/Items', :query => options
        Hashie::Mash.new(response)
      end

      def create(email, options = {})
        options.merge! :EmailAddress => email
        response = EmailDirect.post '/Orders', :body => options.to_json
        Hashie::Mash.new(response)
      end
      
      def import(orders)
        options = { :Orders => Array(orders) }
        response = EmailDirect.post '/Orders/Import', :body => options.to_json
        Hashie::Mash.new(response)
      end
    end

    attr_reader :item_id

    def initialize(item_id)
      @item_id = item_id
    end

    def details
      response = get
      Hashie::Mash.new(response)
    end

    def update(email, options)
      options.merge! :EmailAddress => email
      response = EmailDirect.put uri_for, :body => options.to_json
      Hashie::Mash.new(response)
    end

    def delete
      response = EmailDirect.delete uri_for, {}
    end

    private

    def post(action, options)
      EmailDirect.post uri_for(action), options
    end

    def get(action = nil, options = {})
      EmailDirect.get uri_for(action), options
    end

    def uri_for(action = nil)
      action = "/#{action}" if action
      "/Orders/Items/#{item_id}#{action}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
emaildirect-2.0.0 lib/emaildirect/order_item.rb
emaildirect-1.3.4 lib/emaildirect/order_item.rb
emaildirect-1.3.3 lib/emaildirect/order_item.rb