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/version.rb,
lib/pingpp/webhook.rb,
lib/pingpp/transfer.rb,
lib/pingpp/list_object.rb,
lib/pingpp/api_resource.rb,
lib/pingpp/red_envelope.rb,
lib/pingpp/wx_pub_oauth.rb,
lib/pingpp/pingpp_object.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/singleton_api_resource.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, ChannelError, Charge, Event, Identification, InvalidRequestError, ListObject, PingppError, PingppObject, RedEnvelope, Refund, SingletonAPIResource, Transfer

Constant Summary collapse

DEFAULT_CA_BUNDLE_PATH =
File.dirname(__FILE__) + '/data/ca-certificates.crt'
HEADERS_TO_PARSE =
[:pingpp_one_version, :pingpp_sdk_version]
VERSION =
'2.0.15'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



56
57
58
# File 'lib/pingpp.rb', line 56

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



56
57
58
# File 'lib/pingpp.rb', line 56

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



56
57
58
# File 'lib/pingpp.rb', line 56

def api_version
  @api_version
end

.parsed_headersObject

Returns the value of attribute parsed_headers.



56
57
58
# File 'lib/pingpp.rb', line 56

def parsed_headers
  @parsed_headers
end

.private_keyObject

Returns the value of attribute private_key.



56
57
58
# File 'lib/pingpp.rb', line 56

def private_key
  @private_key
end

.pub_keyObject

Returns the value of attribute pub_key.



56
57
58
# File 'lib/pingpp.rb', line 56

def pub_key
  @pub_key
end

.verify_ssl_certsObject

Returns the value of attribute verify_ssl_certs.



56
57
58
# File 'lib/pingpp.rb', line 56

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

.api_url(url = '') ⇒ Object



59
60
61
# File 'lib/pingpp.rb', line 59

def self.api_url(url='')
  @api_base + url
end

.parse_headers(headers) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pingpp.rb', line 63

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 = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
# File 'lib/pingpp.rb', line 80

def self.request(method, url, api_key, params={}, headers={})
  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 => @ssl_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)

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

  request_opts.update(:headers => request_headers(api_key, method.to_s.downcase.to_sym, payload).update(headers),
                      :method => method, :open_timeout => 30,
                      :payload => payload, :url => url, :timeout => 80)

  begin
    response = execute_request(request_opts)
  rescue SocketError => e
    handle_restclient_error(e)
  rescue NoMethodError => e
    # Work around RestClient bug
    if e.message =~ /\WRequestFailed\W/
      e = APIConnectionError.new('Unexpected HTTP response code')
      handle_restclient_error(e)
    else
      raise
    end
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      handle_api_error(rcode, rbody)
    else
      handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    handle_restclient_error(e)
  end

  [parse(response), api_key]
end