Module: Pingpp

Defined in:
lib/pingpp.rb,
lib/pingpp/util.rb,
lib/pingpp/event.rb,
lib/pingpp/charge.rb,
lib/pingpp/refund.rb,
lib/pingpp/customs.rb,
lib/pingpp/version.rb,
lib/pingpp/webhook.rb,
lib/pingpp/transfer.rb,
lib/pingpp/list_object.rb,
lib/pingpp/api_resource.rb,
lib/pingpp/batch_refund.rb,
lib/pingpp/red_envelope.rb,
lib/pingpp/wx_pub_oauth.rb,
lib/pingpp/pingpp_object.rb,
lib/pingpp/batch_transfer.rb,
lib/pingpp/identification.rb,
lib/pingpp/errors/api_error.rb,
lib/pingpp/api_operations/list.rb,
lib/pingpp/errors/pingpp_error.rb,
lib/pingpp/errors/channel_error.rb,
lib/pingpp/api_operations/create.rb,
lib/pingpp/api_operations/delete.rb,
lib/pingpp/api_operations/update.rb,
lib/pingpp/api_operations/request.rb,
lib/pingpp/singleton_api_resource.rb,
lib/pingpp/errors/rate_limit_error.rb,
lib/pingpp/errors/api_connection_error.rb,
lib/pingpp/errors/authentication_error.rb,
lib/pingpp/errors/invalid_request_error.rb

Defined Under Namespace

Modules: APIOperations, Util, Webhook, WxPubOauth Classes: APIConnectionError, APIError, APIResource, AuthenticationError, BatchRefund, BatchTransfer, ChannelError, Charge, Customs, Event, Identification, InvalidRequestError, ListObject, PingppError, PingppObject, RateLimitError, RedEnvelope, Refund, SingletonAPIResource, Transfer

Constant Summary collapse

DEFAULT_CA_BUNDLE_PATH =
File.dirname(__FILE__) + '/data/ca-certificates.crt'
RETRY_EXCEPTIONS =
[
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::ETIMEDOUT,
  RestClient::Conflict,
  RestClient::RequestTimeout,
].freeze
HEADERS_TO_PARSE =
[:pingpp_one_version, :pingpp_sdk_version]
VERSION =
'2.1.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



77
78
79
# File 'lib/pingpp.rb', line 77

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



77
78
79
# File 'lib/pingpp.rb', line 77

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



77
78
79
# File 'lib/pingpp.rb', line 77

def api_version
  @api_version
end

.app_idObject

Returns the value of attribute app_id.



77
78
79
# File 'lib/pingpp.rb', line 77

def app_id
  @app_id
end

.initial_network_retry_delayObject (readonly)

Returns the value of attribute initial_network_retry_delay.



80
81
82
# File 'lib/pingpp.rb', line 80

def initial_network_retry_delay
  @initial_network_retry_delay
end

.max_network_retry_delayObject (readonly)

Returns the value of attribute max_network_retry_delay.



80
81
82
# File 'lib/pingpp.rb', line 80

def max_network_retry_delay
  @max_network_retry_delay
end

.open_timeoutObject

Returns the value of attribute open_timeout.



77
78
79
# File 'lib/pingpp.rb', line 77

def open_timeout
  @open_timeout
end

.parsed_headersObject

Returns the value of attribute parsed_headers.



77
78
79
# File 'lib/pingpp.rb', line 77

def parsed_headers
  @parsed_headers
end

.private_keyObject

Returns the value of attribute private_key.



77
78
79
# File 'lib/pingpp.rb', line 77

def private_key
  @private_key
end

.pub_keyObject

Returns the value of attribute pub_key.



77
78
79
# File 'lib/pingpp.rb', line 77

def pub_key
  @pub_key
end

.timeoutObject

Returns the value of attribute timeout.



77
78
79
# File 'lib/pingpp.rb', line 77

def timeout
  @timeout
end

.verify_ssl_certsObject

Returns the value of attribute verify_ssl_certs.



77
78
79
# File 'lib/pingpp.rb', line 77

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

.api_url(url = '', api_base_url = nil) ⇒ Object



83
84
85
# File 'lib/pingpp.rb', line 83

def self.api_url(url='', api_base_url=nil)
  (api_base_url || @api_base) + url
end

.max_network_retriesObject



157
158
159
# File 'lib/pingpp.rb', line 157

def self.max_network_retries
  @max_network_retries
end

.max_network_retries=(val) ⇒ Object



161
162
163
# File 'lib/pingpp.rb', line 161

def self.max_network_retries=(val)
  @max_network_retries = val.to_i
end

.parse_headers(headers) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pingpp.rb', line 87

def self.parse_headers(headers)
  @parsed_headers = {}
  if headers && headers.respond_to?("each")
    headers.each do |k, v|
      k = k[0, 5] == 'HTTP_' ? k[5..-1] : k
      header_key = k.gsub(/-/, '_').to_s.downcase.to_sym
      if HEADERS_TO_PARSE.include?(header_key)
        if v.is_a?(String)
          @parsed_headers[header_key] = v
        elsif v.is_a?(Array)
          @parsed_headers[header_key] = v[0]
        end
      end
    end
  end
end

.request(method, url, api_key, params = {}, headers = {}, api_base_url = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pingpp.rb', line 104

def self.request(method, url, api_key, params={}, headers={}, api_base_url=nil)
  api_base_url = api_base_url || @api_base

  unless api_key ||= @api_key
    raise AuthenticationError.new('No API key provided. ' +
      'Set your API key using "Pingpp.api_key = <API-KEY>". ' +
      'You can generate API keys from the Pingpp web interface. ' +
      'See https://pingxx.com/document/api for details.')
  end

  if api_key =~ /\s/
    raise AuthenticationError.new('Your API key is invalid, as it contains ' +
      'whitespace. (HINT: You can double-check your API key from the ' +
      'Pingpp web interface. See https://pingxx.com/document/api for details.)')
  end

  if verify_ssl_certs
    request_opts = {:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
                    :ssl_ca_file => @ca_bundle_path,
                    :ssl_version => 'TLSv1'}
  else
    request_opts = {:verify_ssl => false,
                    :ssl_version => 'TLSv1'}
    unless @verify_ssl_warned
      @verify_ssl_warned = true
      $stderr.puts("WARNING: Running without SSL cert verification. " \
        "You should never do this in production. " \
        "Execute 'Pingpp.verify_ssl_certs = true' to enable verification.")
    end
  end

  params = Util.objects_to_ids(params)
  url = api_url(url, api_base_url)

  method_sym = method.to_s.downcase.to_sym
  case method_sym
  when :get, :head, :delete
    # Make params into GET parameters
    url += "#{URI.parse(url).query ? '&' : '?'}#{Util.encode_parameters(params)}" if params && params.any?
    payload = nil
  else
    payload = JSON.generate(params)
  end

  request_opts.update(:headers => request_headers(api_key, method_sym, payload, url).update(headers),
                      :method => method, :open_timeout => open_timeout,
                      :payload => payload, :url => url, :timeout => timeout)

  response = execute_request_with_rescues(request_opts, api_base_url)

  [parse(response), api_key]
end