Class: PushWoosher::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/push_woosher/base.rb

Direct Known Subclasses

Device::Base, Push

Constant Summary collapse

BASE_HOST =
'cp.pushwoosh.com'
PROTOCOL =
'https'
BASE_PATH =
'/json/1.3'

Instance Method Summary collapse

Instance Method Details

#configObject



32
33
34
35
36
37
# File 'lib/push_woosher/base.rb', line 32

def config
  {
    application: PushWoosher.configuration.application_code,
    auth: PushWoosher.configuration.api_token
  }
end

#connectionObject



9
10
11
12
13
14
15
# File 'lib/push_woosher/base.rb', line 9

def connection
  @connection = Faraday.new(url: "#{PROTOCOL}://#{BASE_HOST}") do |faraday|
    faraday.request :url_encoded             # form-encode POST params
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter Faraday.default_adapter  # make requests with Net::HTTP
  end
end

#post_action(opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/push_woosher/base.rb', line 17

def post_action(opts = {})
  response = connection.post do |req|
    req.url "#{BASE_PATH}/#{opts[:path]}"
    req.headers['Content-Type'] = 'application/json'
    req.body = { request: opts[:request_hash].merge(config) }.to_json
  end

  case response.status
  when 200
    true
  else
    { status: response.status, message: response.body }
  end
end