Sha256: db6274a53543b1e20b388013050c27c9f01467388fa6b6100ed5db39b8206844

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 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
    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|
        response = call(:get, url_pst_status(account, status))
        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)
      "#{url_pst(account)}/#{status}"
    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

3 entries across 3 versions & 1 rubygems

Version Path
mxhero-api-1.1.1 lib/pst-converter.rb
mxhero-api-1.1.0 lib/pst-converter.rb
mxhero-api-1.0.5 lib/pst-converter.rb