Module: ExpressPigeon::API

Included in:
AutoResponders, Campaigns, Contacts, Lists, Messages, Templates
Defined in:
lib/expresspigeon-ruby.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.autorespondersObject



111
112
113
# File 'lib/expresspigeon-ruby.rb', line 111

def self.autoresponders
  AutoResponders.new
end

.campaignsObject



95
96
97
# File 'lib/expresspigeon-ruby.rb', line 95

def self.campaigns
  Campaigns.new
end

.contactsObject



103
104
105
# File 'lib/expresspigeon-ruby.rb', line 103

def self.contacts
  Contacts.new
end

.listsObject



99
100
101
# File 'lib/expresspigeon-ruby.rb', line 99

def self.lists
  Lists.new
end

.messagesObject



107
108
109
# File 'lib/expresspigeon-ruby.rb', line 107

def self.messages
  Messages.new
end

.templatesObject



115
116
117
# File 'lib/expresspigeon-ruby.rb', line 115

def self.templates
  Templates.new
end

Instance Method Details

#auth_key(auth_key) ⇒ Object

Override environment variable in code.



19
20
21
22
# File 'lib/expresspigeon-ruby.rb', line 19

def auth_key(auth_key)
  @auth_key = auth_key
  self
end

#del(path, params = {}) ⇒ Object



91
92
93
# File 'lib/expresspigeon-ruby.rb', line 91

def del(path, params = {})
  http path, 'Delete', params
end

#get(path, params = {}, &block) ⇒ Object



83
84
85
# File 'lib/expresspigeon-ruby.rb', line 83

def get(path, params = {}, &block)
  http path, 'Get', params, &block
end

#get_auth_keyObject



75
76
77
78
79
80
81
# File 'lib/expresspigeon-ruby.rb', line 75

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 = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/expresspigeon-ruby.rb', line 29

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) 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) 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

#post(path, params = {}) ⇒ Object



87
88
89
# File 'lib/expresspigeon-ruby.rb', line 87

def post(path, params = {})
  http path, 'Post', params
end

#root(root) ⇒ Object



24
25
26
27
# File 'lib/expresspigeon-ruby.rb', line 24

def root(root)
  @root = root
  self
end