Sha256: 28e2f2aafdd874096dec3ec9708b0b308bf9923a1635f802cea4721fa017ea49

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

# encoding: utf-8
require_relative 'communication'
require_relative 'urls'

require 'open-uri'
require 'delegate'

module MxHero::API


  class PSTConverter
    include Communication
    include Urls

    def initialize(config = {})
      @service_url = config[:api_url]
      @username    = config[:username]
      @password    = config[:password]
      @verbose     = config[:verbose] || false
      @as_user     = config[:as_user]
    end

    # Retrieve all tasks for {account} in status {status}
    #
    # @param [String] account The account to search tasks for
    # @return [MxHero::API::Response] response
    ['active', 'inactive'].each do |status|
      define_method("#{status}_tasks") do |account, limit = nil|
        response = call(:get, url_pst_status(account, status, limit))
        parse_response(response, on_empty: [])
      end
    end

    def delete(account, id)
      response = call(:delete, url_pst_id(account, id))
      response.status == 200
    end

    def task(account, id)
      response = call(:get, url_pst_id(account, id))
      parse_response(response)
    end

    def save(account, task)
      response = call(:post, url_pst_post(account), task.to_json, throw_exception: false)
      parsed = parse_response(response)
      return parsed.msg[:id] if parsed.success?
      nil
    end

    private

    def url_pst_post(account)
      url_pst(account) + "/"
    end

    def url_pst(account)
      "#{service_url}/pst/#{account}"
    end

    def url_pst_id(account, id)
      "#{url_pst(account)}/#{id}"
    end

    def url_pst_status(account, status, limit)
      "#{url_pst(account)}/#{status}" + (limit ? "?limit=#{limit}" : "")
    end

    # @return [MxHero::API::Response] a response object
    def parse_response(response, opts = { on_empty: nil })
      json = response.content
      hash = json.nil? || json.empty? ? opts[:on_empty] : json_parse(json)
      Response.new(response.code, hash)
    end


  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mxhero-api-1.2.9 lib/pst-converter.rb
mxhero-api-1.2.8 lib/pst-converter.rb
mxhero-api-1.2.7 lib/pst-converter.rb
mxhero-api-1.2.6 lib/pst-converter.rb
mxhero-api-1.2.5 lib/pst-converter.rb
mxhero-api-1.2.4 lib/pst-converter.rb