module RealPush::API::Base

Public Class Methods

included(base) click to toggle source
# File lib/realpush/api/base.rb, line 8
def self.included(base)
  base.class_eval do

    attr_reader :token

    attr_accessor :connect_timeout, :send_timeout, :receive_timeout, :keep_alive_timeout

    def initialize(token)
      raise ConfigurationError, "Invalid token format: #{token}" if /^[-=\w]{43}={0,2}$/.match(token).nil?
      @token = token
      @params_accept = []

      # Default timeouts
      @connect_timeout = 5
      @send_timeout = 5
      @receive_timeout = 5
      @keep_alive_timeout = 30
    end

    protected

    # Sends a request to the specified URL.
    #
    # @param method [String | Symbol]
    #     HTTP method to be sent.  method.to_s.upcase is used.
    #
    # @param uri [String | URI]
    #     HTTP method to be sent.  method.to_s.upcase is used.
    #
    # @param query [Hash]
    #     a Hash of query part of URL.
    #     e.g. { "a" => "b" } => 'http://host/part?a=b'
    #
    # @param body [Hash]
    #      a Hash of body part. e.g.
    #      { "a" => "b" } => 'a=b'
    #
    # @param header [Hash]
    #     a Hash of extra headers.  e.g.
    #     { 'Accept' => 'text/html' }.
    #
    # @return [ HTTP::Message ]
    def execute(method, uri, query={}, body={}, header={})
      begin
        response = httpclient.request(method, uri, query, body, header)
      rescue HTTPClient::BadResponseError,
          HTTPClient::TimeoutError,
          SocketError,
          Errno::ECONNREFUSED => e
        raise RealPush::HTTPError, "#{e.message} (#{e.class})"
      end
    end

    # TODO: nodoc
    # @return [HTTPClient]
    def httpclient
      @client ||= begin
        require 'httpclient'

        HTTPClient.new(default_header: {'X-RealPush-Token' => token}).tap do |c|
          c.connect_timeout = connect_timeout
          c.send_timeout = send_timeout
          c.receive_timeout = receive_timeout
          c.keep_alive_timeout = keep_alive_timeout
        end
      end
    end

    # Capture the contents of the request and makes the JSON parse inside the BODY content.
    #
    # @param content [ HTTP::Message ]
    #   Returns of self.httpclient
    #
    # @return [ Hash ]
    def parse_content(content)
      MultiJson.decode(content.body)
    end

    # Prepare the URL to the request, it contains the url pattern for API Version 1.0.
    #
    # :example:
    #   url()                //=> https://app.realpush.cc/api/v1/
    #   url('apps/123.json') //=> https://app.realpush.cc/api/v1/apps/123.json
    #
    # @param path [String]
    #   Added in path of URL
    #
    # @return [URI]
    def url(path='')
      path = "/#{path}" unless path.start_with? '/'
      URI::Generic.build({
                             :scheme => 'https',
                             :host   => 'app.realpush.cc',
                             :port   => 443,
                             :path   => "/api/#{RealPush::API_VERSION_BE}#{path}"
                         })
    end

    # Validate a params accepted
    #
    # @param params [Hash]
    #
    # @return [TrueClass]
    def valid_params?(params)
      params.keys.each do |key|
        unless RealPush::API::App.params_accept.include? key.to_sym
          raise ConfigurationError, "Invalid parameter! ( #{RealPush::API::App.params_accept.join(', ')} )"
        end
      end
      true
    end

  end
end
new(token) click to toggle source
# File lib/realpush/api/base.rb, line 15
def initialize(token)
  raise ConfigurationError, "Invalid token format: #{token}" if /^[-=\w]{43}={0,2}$/.match(token).nil?
  @token = token
  @params_accept = []

  # Default timeouts
  @connect_timeout = 5
  @send_timeout = 5
  @receive_timeout = 5
  @keep_alive_timeout = 30
end

Public Instance Methods

execute(method, uri, query={}, body={}, header={}) click to toggle source

Sends a request to the specified URL.

@param method [String | Symbol]

HTTP method to be sent.  method.to_s.upcase is used.

@param uri [String | URI]

HTTP method to be sent.  method.to_s.upcase is used.

@param query [Hash]

a Hash of query part of URL.
e.g. { "a" => "b" } => 'http://host/part?a=b'

@param body [Hash]

a Hash of body part. e.g.
{ "a" => "b" } => 'a=b'

@param header [Hash]

a Hash of extra headers.  e.g.
{ 'Accept' => 'text/html' }.

@return [ HTTP::Message ]

# File lib/realpush/api/base.rb, line 50
def execute(method, uri, query={}, body={}, header={})
  begin
    response = httpclient.request(method, uri, query, body, header)
  rescue HTTPClient::BadResponseError,
      HTTPClient::TimeoutError,
      SocketError,
      Errno::ECONNREFUSED => e
    raise RealPush::HTTPError, "#{e.message} (#{e.class})"
  end
end
httpclient() click to toggle source

TODO: nodoc @return [HTTPClient]

# File lib/realpush/api/base.rb, line 63
def httpclient
  @client ||= begin
    require 'httpclient'

    HTTPClient.new(default_header: {'X-RealPush-Token' => token}).tap do |c|
      c.connect_timeout = connect_timeout
      c.send_timeout = send_timeout
      c.receive_timeout = receive_timeout
      c.keep_alive_timeout = keep_alive_timeout
    end
  end
end
parse_content(content) click to toggle source

Capture the contents of the request and makes the JSON parse inside the BODY content.

@param content [ HTTP::Message ]

Returns of self.httpclient

@return [ Hash ]

# File lib/realpush/api/base.rb, line 82
def parse_content(content)
  MultiJson.decode(content.body)
end
url(path='') click to toggle source

Prepare the URL to the request, it contains the url pattern for API Version 1.0.

:example:

url()                //=> https://app.realpush.cc/api/v1/
url('apps/123.json') //=> https://app.realpush.cc/api/v1/apps/123.json

@param path [String]

Added in path of URL

@return [URI]

# File lib/realpush/api/base.rb, line 96
def url(path='')
  path = "/#{path}" unless path.start_with? '/'
  URI::Generic.build({
                         :scheme => 'https',
                         :host   => 'app.realpush.cc',
                         :port   => 443,
                         :path   => "/api/#{RealPush::API_VERSION_BE}#{path}"
                     })
end
valid_params?(params) click to toggle source

Validate a params accepted

@param params [Hash]

@return [TrueClass]

# File lib/realpush/api/base.rb, line 111
def valid_params?(params)
  params.keys.each do |key|
    unless RealPush::API::App.params_accept.include? key.to_sym
      raise ConfigurationError, "Invalid parameter! ( #{RealPush::API::App.params_accept.join(', ')} )"
    end
  end
  true
end