module SmartSMS::Request

Module that manage requests

Public Class Methods

base_url() click to toggle source

Base uri for yunpian API

# File lib/smart_sms/request.rb, line 42
def base_url
  "http://yunpian.com/#{SmartSMS.config.api_version}/"
end
get(api, options = {}) click to toggle source

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

# 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 = {}) click to toggle source

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

# 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) click to toggle source

Method that parse JSON to Hash

# 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

Private Instance Methods

base_url() click to toggle source

Base uri for yunpian API

# File lib/smart_sms/request.rb, line 42
def base_url
  "http://yunpian.com/#{SmartSMS.config.api_version}/"
end
get(api, options = {}) click to toggle source

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

# 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 = {}) click to toggle source

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

# 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) click to toggle source

Method that parse JSON to Hash

# 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