module ExpressPigeon::API

Public Class Methods

autoresponders() click to toggle source
# File lib/expresspigeon-ruby.rb, line 131
def self.autoresponders
  AutoResponders.new
end
campaigns() click to toggle source
# File lib/expresspigeon-ruby.rb, line 115
def self.campaigns
  Campaigns.new
end
contacts() click to toggle source
# File lib/expresspigeon-ruby.rb, line 123
def self.contacts
  Contacts.new
end
lists() click to toggle source
# File lib/expresspigeon-ruby.rb, line 119
def self.lists
  Lists.new
end
messages() click to toggle source
# File lib/expresspigeon-ruby.rb, line 127
def self.messages
  Messages.new
end
templates() click to toggle source
# File lib/expresspigeon-ruby.rb, line 135
def self.templates
  Templates.new
end

Public Instance Methods

auth_key(auth_key) click to toggle source

Override environment variable in code.

# File lib/expresspigeon-ruby.rb, line 19
def auth_key(auth_key)
  @auth_key = auth_key
  self
end
del(path, params = {}) click to toggle source
# File lib/expresspigeon-ruby.rb, line 111
def del(path, params = {})
  http path, 'Delete', params
end
get(path, params = {}, &block) click to toggle source
# File lib/expresspigeon-ruby.rb, line 103
def get(path, params = {}, &block)
  http path, 'Get', params, &block
end
get_auth_key() click to toggle source
# File lib/expresspigeon-ruby.rb, line 95
def get_auth_key
  unless AUTH_KEY ||  @auth_key
    raise("Must set authentication key either using environment variable EXPRESSPIGEON_AUTH_KEY, or using auth_key() method in code")
  end

  @auth_key ? @auth_key : AUTH_KEY
end
http(path, method, params = {}) { |seg| ... } click to toggle source
# File lib/expresspigeon-ruby.rb, line 37
def http(path, method, params = {})
  root = @root ? @root : ROOT

  if params and !params.empty? and method == 'Get'
    query = URI.encode_www_form(params)
    path = "#{path}?#{query}"
  end


  uri = URI.parse "#{root}#{path}"
  req = Net::HTTP.const_get("#{method}").new "#{ROOT}#{path}"

  req['X-auth-key'] = get_auth_key
  if params
    if method != 'Get'
      req.body = params.to_json
      req['Content-type'] = 'application/json'
    end
  end

  if block_given?
    Net::HTTP.start(
      uri.host,
      uri.port,
      :use_ssl => USE_SSL,
      :read_timeout => @read_timeout,
      :open_timeout => @open_timeout,
    ) do |http|
      http.request req do |res|
        res.read_body do |seg|
          yield seg
        end
      end
    end
  else
    resp = Net::HTTP.start(
      uri.host,
      uri.port,
      :use_ssl => USE_SSL,
      :read_timeout => @read_timeout,
      :open_timeout => @open_timeout,
    ) do |http|
      http.request req
    end

    if resp.content_type == 'application/json'
      parsed = JSON.parse(resp.body)
      if parsed.kind_of? Hash
        MetaResponse.new parsed
      else
        parsed
      end
    else
      resp.body
    end
  end
end
open_timeout=(timeout) click to toggle source
# File lib/expresspigeon-ruby.rb, line 24
def open_timeout=(timeout)
  @open_timeout = timeout
end
post(path, params = {}) click to toggle source
# File lib/expresspigeon-ruby.rb, line 107
def post(path, params = {})
  http path, 'Post', params
end
read_timeout=(timeout) click to toggle source
# File lib/expresspigeon-ruby.rb, line 28
def read_timeout=(timeout)
  @read_timeout = timeout
end
root(root) click to toggle source
# File lib/expresspigeon-ruby.rb, line 32
def root(root)
  @root = root
  self
end