Module: SmartSMS::Request

Defined in:
lib/smart_sms/request.rb

Overview

Module that manage requests

Class Method Summary collapse

Class Method Details

.base_urlObject

Base uri for yunpian API



42
43
44
# File 'lib/smart_sms/request.rb', line 42

def base_url
  "http://yunpian.com/#{SmartSMS.config.api_version}/"
end

.get(api, options = {}) ⇒ Object

Method that use Net::HTTP.get to perform GET action



19
20
21
22
23
# File 'lib/smart_sms/request.rb', line 19

def get(api, options = {})
  options[:apikey] = SmartSMS.config.api_key
  uri = URI.join(base_url, api)
  result Net::HTTP.get(uri, options)
end

.post(api, options = {}) ⇒ Object

Method that use Net::HTTP.post_form to perform POST action



10
11
12
13
14
15
# File 'lib/smart_sms/request.rb', line 10

def post(api, options = {})
  options[:apikey] = SmartSMS.config.api_key
  uri = URI.join(base_url, api)
  res = Net::HTTP.post_form(uri, options)
  result res.body
end

.result(body) ⇒ Object

Method that parse JSON to Hash



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/smart_sms/request.rb', line 29

def result(body)
  begin
    ActiveSupport::JSON.decode body
  rescue => e
    {
      code: 502,
      msg: '内容解析错误',
      detail: e.to_s
    }
  end
end