Module: Pingpp

Defined in:
lib/pingpp.rb,
lib/pingpp/user.rb,
lib/pingpp/util.rb,
lib/pingpp/event.rb,
lib/pingpp/order.rb,
lib/pingpp/charge.rb,
lib/pingpp/coupon.rb,
lib/pingpp/refund.rb,
lib/pingpp/channel.rb,
lib/pingpp/customs.rb,
lib/pingpp/royalty.rb,
lib/pingpp/sub_app.rb,
lib/pingpp/version.rb,
lib/pingpp/webhook.rb,
lib/pingpp/recharge.rb,
lib/pingpp/transfer.rb,
lib/pingpp/agreement.rb,
lib/pingpp/card_info.rb,
lib/pingpp/withdrawal.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/balance_bonus.rb,
lib/pingpp/pingpp_object.rb,
lib/pingpp/wx_lite_oauth.rb,
lib/pingpp/batch_transfer.rb,
lib/pingpp/identification.rb,
lib/pingpp/settle_account.rb,
lib/pingpp/coupon_template.rb,
lib/pingpp/balance_transfer.rb,
lib/pingpp/batch_withdrawal.rb,
lib/pingpp/errors/api_error.rb,
lib/pingpp/royalty_template.rb,
lib/pingpp/app_based_resource.rb,
lib/pingpp/balance_settlement.rb,
lib/pingpp/royalty_settlement.rb,
lib/pingpp/api_operations/list.rb,
lib/pingpp/balance_transaction.rb,
lib/pingpp/errors/pingpp_error.rb,
lib/pingpp/royalty_transaction.rb,
lib/pingpp/user_based_resource.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/sub_app_based_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,
lib/pingpp/singleton_app_based_resource.rb

Defined Under Namespace

Modules: APIOperations, Util, Webhook, WxLiteOauth, WxPubOauth Classes: APIConnectionError, APIError, APIResource, Agreement, AppBasedResource, AuthenticationError, BalanceBonus, BalanceSettlement, BalanceTransaction, BalanceTransfer, BatchRefund, BatchTransfer, BatchWithdrawal, CardInfo, Channel, ChannelError, Charge, Coupon, CouponTemplate, Customs, Event, Identification, InvalidRequestError, ListObject, Order, PingppError, PingppObject, RateLimitError, Recharge, RedEnvelope, Refund, Royalty, RoyaltySettlement, RoyaltyTemplate, RoyaltyTransaction, SettleAccount, SingletonAPIResource, SingletonAppBasedResource, SubApp, SubAppBasedResource, Transfer, User, UserBasedResource, Withdrawal

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,
  RestClient::BadGateway,
].freeze
HEADERS_TO_PARSE =
[:pingpp_one_version, :pingpp_sdk_version]
VERSION =
'2.2.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



105
106
107
# File 'lib/pingpp.rb', line 105

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



105
106
107
# File 'lib/pingpp.rb', line 105

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



105
106
107
# File 'lib/pingpp.rb', line 105

def api_version
  @api_version
end

.app_idObject

Returns the value of attribute app_id.



105
106
107
# File 'lib/pingpp.rb', line 105

def app_id
  @app_id
end

.bad_gateway_matchObject

Returns the value of attribute bad_gateway_match.



105
106
107
# File 'lib/pingpp.rb', line 105

def bad_gateway_match
  @bad_gateway_match
end

.initial_network_retry_delayObject (readonly)

Returns the value of attribute initial_network_retry_delay.



108
109
110
# File 'lib/pingpp.rb', line 108

def initial_network_retry_delay
  @initial_network_retry_delay
end

.max_network_retry_delayObject (readonly)

Returns the value of attribute max_network_retry_delay.



108
109
110
# File 'lib/pingpp.rb', line 108

def max_network_retry_delay
  @max_network_retry_delay
end

.open_timeoutObject

Returns the value of attribute open_timeout.



105
106
107
# File 'lib/pingpp.rb', line 105

def open_timeout
  @open_timeout
end

.parsed_headersObject

Returns the value of attribute parsed_headers.



105
106
107
# File 'lib/pingpp.rb', line 105

def parsed_headers
  @parsed_headers
end

.private_keyObject

Returns the value of attribute private_key.



105
106
107
# File 'lib/pingpp.rb', line 105

def private_key
  @private_key
end

.pub_keyObject

Returns the value of attribute pub_key.



105
106
107
# File 'lib/pingpp.rb', line 105

def pub_key
  @pub_key
end

.timeoutObject

Returns the value of attribute timeout.



105
106
107
# File 'lib/pingpp.rb', line 105

def timeout
  @timeout
end

.verify_ssl_certsObject

Returns the value of attribute verify_ssl_certs.



105
106
107
# File 'lib/pingpp.rb', line 105

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

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



111
112
113
# File 'lib/pingpp.rb', line 111

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

.ca_bundleObject



193
194
195
# File 'lib/pingpp.rb', line 193

def self.ca_bundle
  @ca_bundle_path
end

.ca_bundle=(val) ⇒ Object



197
198
199
# File 'lib/pingpp.rb', line 197

def self.ca_bundle=(val)
  @ca_bundle_path = val
end

.max_network_retriesObject



185
186
187
# File 'lib/pingpp.rb', line 185

def self.max_network_retries
  @max_network_retries
end

.max_network_retries=(val) ⇒ Object



189
190
191
# File 'lib/pingpp.rb', line 189

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

.parse_headers(headers) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pingpp.rb', line 115

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/pingpp.rb', line 132

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