Module: ExpressPigeon::API
- Defined in:
- lib/expresspigeon-ruby.rb
Class Method Summary collapse
- .auto_responders ⇒ Object
- .campaigns ⇒ Object
- .contacts ⇒ Object
- .lists ⇒ Object
- .messages ⇒ Object
- .templates ⇒ Object
Instance Method Summary collapse
-
#auth_key(auth_key) ⇒ Object
Override environment variable in code.
- #del(path, params = {}) ⇒ Object
- #get(path, params = {}, &block) ⇒ Object
- #get_auth_key ⇒ Object
- #http(path, method, params = {}) ⇒ Object
- #open_timeout=(timeout) ⇒ Object
- #post(path, params = {}) ⇒ Object
- #read_timeout=(timeout) ⇒ Object
- #root(root) ⇒ Object
Class Method Details
.auto_responders ⇒ Object
124 125 126 |
# File 'lib/expresspigeon-ruby.rb', line 124 def self.auto_responders AutoResponders.new end |
.campaigns ⇒ Object
108 109 110 |
# File 'lib/expresspigeon-ruby.rb', line 108 def self.campaigns Campaigns.new end |
.contacts ⇒ Object
116 117 118 |
# File 'lib/expresspigeon-ruby.rb', line 116 def self.contacts Contacts.new end |
.lists ⇒ Object
112 113 114 |
# File 'lib/expresspigeon-ruby.rb', line 112 def self.lists Lists.new end |
Instance Method Details
#auth_key(auth_key) ⇒ Object
Override environment variable in code.
17 18 19 20 |
# File 'lib/expresspigeon-ruby.rb', line 17 def auth_key(auth_key) @auth_key = auth_key self end |
#del(path, params = {}) ⇒ Object
104 105 106 |
# File 'lib/expresspigeon-ruby.rb', line 104 def del(path, params = {}) http path, 'Delete', params end |
#get(path, params = {}, &block) ⇒ Object
96 97 98 |
# File 'lib/expresspigeon-ruby.rb', line 96 def get(path, params = {}, &block) http path, 'Get', params, &block end |
#get_auth_key ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/expresspigeon-ruby.rb', line 88 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
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 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/expresspigeon-ruby.rb', line 35 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 parsed = JSON.parse(resp.body) if parsed.kind_of? Hash MetaResponse.new parsed else parsed end end end |
#open_timeout=(timeout) ⇒ Object
22 23 24 |
# File 'lib/expresspigeon-ruby.rb', line 22 def open_timeout=(timeout) @open_timeout = timeout end |
#post(path, params = {}) ⇒ Object
100 101 102 |
# File 'lib/expresspigeon-ruby.rb', line 100 def post(path, params = {}) http path, 'Post', params end |
#read_timeout=(timeout) ⇒ Object
26 27 28 |
# File 'lib/expresspigeon-ruby.rb', line 26 def read_timeout=(timeout) @read_timeout = timeout end |