class MxHero::API::EmailSync

Public Class Methods

new(config = {}) click to toggle source

@param [Hash] config the options of configuration @option config [String] :api_url The URL to consume the API @option config [String] :username The username for access the API @option config [String] :password The password for the user that access the API @option config [Boolean] :verbose (false) If true puts information about http operations @option config [String] :as_user Send to the API to indentify the end user (app user email)

# File lib/email-sync.rb, line 20
def initialize(config = {})
        @service_url = config[:api_url]
        @username    = config[:username]
        @password    = config[:password]
        @verbose     = config[:verbose] || false
        @as_user     = config[:as_user]
end

Public Instance Methods

all(domain, params = {}) click to toggle source
# File lib/email-sync.rb, line 28
def all(domain, params = {})
        wrap_response_from call(:get, url(domain, params))
end
delete(domain, id) click to toggle source
# File lib/email-sync.rb, line 37
def delete(domain, id)
        return if id.nil? || id.empty?
        wrap_response_from call(:delete, "#{url(domain)}#{id}")
end
request(domain, method, path = '/', more = {}) click to toggle source

Make a request in the endpoin /emailsync/#{domain} @param method [Symbol] indicate the HTTP verb (:get, :post, :put, etc) @param path [String] the path after the base URL (base url: #{api_url}/emailsync/#{domain}) @param [Hash] more @option more [Hash] :params the URL parameters to add in the URL (ex. {limit: 10} => ?limit=10) @option more [Hash] :body the body content to send in POST or PUT

@return [HTTP::Message](www.rubydoc.info/gems/httpclient/HTTP/Message)

Useful methods: status, content
# File lib/email-sync.rb, line 51
def request(domain, method, path = '/', more = {})
        url = "#{@service_url}/emailsync/#{domain}#{path}#{parse_params(more[:params])}"
        call(method, url, more[:body])
end
task_by_id(domain, id) click to toggle source
# File lib/email-sync.rb, line 32
def task_by_id(domain, id)
        return if id.nil? || id.empty?
        wrap_response_from call(:get, "#{url(domain)}#{id}")
end

Private Instance Methods

blank?(value) click to toggle source
# File lib/email-sync.rb, line 73
def blank?(value)
        value.nil? || value.empty?
end
default_content(content) click to toggle source
# File lib/email-sync.rb, line 77
def default_content(content)
        blank?(content) ? '{}' : content
end
parse_params(params = {}) click to toggle source
# File lib/email-sync.rb, line 62
def parse_params(params = {})
        return if blank?(params)
        '?' << params.map do |key, value|
                "#{URI::encode(key.to_s)}=#{URI::encode(value.to_s)}"
        end.join('&')
end
url(domain, params = {}) click to toggle source
# File lib/email-sync.rb, line 58
def url(domain, params = {})
        "#{@service_url}/emailsync/#{domain}/#{parse_params(params)}"
end
wrap_response_from(response) click to toggle source
# File lib/email-sync.rb, line 69
def wrap_response_from(response)
        Response.new(response.status, json_parse(default_content(response.content)))
end