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/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/errors/api_error.rb,
lib/pingpp/api_operations/list.rb,
lib/pingpp/errors/pingpp_error.rb,
lib/pingpp/api_operations/create.rb,
lib/pingpp/api_operations/delete.rb,
lib/pingpp/api_operations/update.rb,
lib/pingpp/certificate_blacklist.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, CertificateBlacklist, Util, WxPubOauth Classes: APIConnectionError, APIError, APIResource, AuthenticationError, Charge, Event, InvalidRequestError, ListObject, PingppError, PingppObject, RedEnvelope, Refund, SingletonAPIResource

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.5'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



53
54
55
# File 'lib/pingpp.rb', line 53

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



53
54
55
# File 'lib/pingpp.rb', line 53

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



53
54
55
# File 'lib/pingpp.rb', line 53

def api_version
  @api_version
end

.parsed_headersObject

Returns the value of attribute parsed_headers.



53
54
55
# File 'lib/pingpp.rb', line 53

def parsed_headers
  @parsed_headers
end

.verify_ssl_certsObject

Returns the value of attribute verify_ssl_certs.



53
54
55
# File 'lib/pingpp.rb', line 53

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

.api_url(url = '') ⇒ Object



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

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

.parse_headers(headers) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pingpp.rb', line 60

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



77
78
79
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
# File 'lib/pingpp.rb', line 77

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, or email [email protected] ' +
      'if you have any questions.')
  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, or ' +
      'email [email protected] if you have any questions.)')
  end

  request_opts = { :verify_ssl => false, :ssl_version => 'TLSv1' }

  if ssl_preflight_passed?
    request_opts.update(:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
                        :ssl_ca_file => @ssl_bundle_path)
  end

  if @verify_ssl_certs and !@CERTIFICATE_VERIFIED
    @CERTIFICATE_VERIFIED = CertificateBlacklist.check_ssl_cert(@api_base, @ssl_bundle_path)
  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 == :post).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