Module: Cryptoprocessing::Connection
- Included in:
- Client
- Defined in:
- lib/cryptoprocessing/connection.rb
Instance Method Summary collapse
- #agent ⇒ Net::HTTP
- #auth_headers(method, path, body) ⇒ Object
-
#delete(path, params) ⇒ Object
HTTP DELETE method.
- #endpoint ⇒ Object
-
#get(path, params = {}) ⇒ Object
HTTP GET method.
-
#post(path, params) ⇒ Object
HTTP POST method.
-
#put(path, params) ⇒ Object
HTTP PUT method.
- #request(method, path, body = nil, headers = {}) {|out| ... } ⇒ Object
- #reset_agent ⇒ Object
Instance Method Details
#agent ⇒ Net::HTTP
14 15 16 17 18 19 20 21 |
# File 'lib/cryptoprocessing/connection.rb', line 14 def agent base_uri = URI.parse(endpoint) @agent = Net::HTTP.new(base_uri.host, base_uri.port) @agent.use_ssl = true if base_uri.scheme == 'https' # @agent.cert_store = self.class.whitelisted_certificates @agent.ssl_version = :TLSv1 @agent end |
#auth_headers(method, path, body) ⇒ Object
67 68 69 |
# File 'lib/cryptoprocessing/connection.rb', line 67 def auth_headers(method, path, body) {:Authorization => "Bearer #{@access_token}"} end |
#delete(path, params) ⇒ Object
HTTP DELETE method
132 133 134 135 136 137 138 |
# File 'lib/cryptoprocessing/connection.rb', line 132 def delete(path, params) headers = {} request('DELETE', path, nil, headers) do |resp| yield(resp) if block_given? end end |
#endpoint ⇒ Object
9 10 11 |
# File 'lib/cryptoprocessing/connection.rb', line 9 def endpoint api_endpoint end |
#get(path, params = {}) ⇒ Object
HTTP GET method
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cryptoprocessing/connection.rb', line 79 def get(path, params = {}) uri = path if params.count > 0 uri += "?#{URI.encode_www_form(params)}" end headers = {} request('GET', uri, nil, headers) do |resp| if params[:fetch_all] == true && resp.body.has_key?('pagination') && resp.body['pagination']['next_uri'] != nil params[:starting_after] = resp.body['data'].last['id'] get(path, params) do |page| body = resp.body body['data'] += page.data resp.body = body yield(resp) if block_given? end else yield(resp) if block_given? end end end |
#post(path, params) ⇒ Object
HTTP POST method
120 121 122 123 124 125 126 |
# File 'lib/cryptoprocessing/connection.rb', line 120 def post(path, params) headers = {} request('POST', path, params.to_json, headers) do |resp| yield(resp) if block_given? end end |
#put(path, params) ⇒ Object
HTTP PUT method
108 109 110 111 112 113 114 |
# File 'lib/cryptoprocessing/connection.rb', line 108 def put(path, params) headers = {} request('PUT', path, params.to_json, headers) do |resp| yield(resp) if block_given? end end |
#request(method, path, body = nil, headers = {}) {|out| ... } ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cryptoprocessing/connection.rb', line 28 def request(method, path, body = nil, headers = {}) # Prepend configured namespace path = "#{@api_namespace}#{path}" case method when 'GET' then req = Net::HTTP::Get.new(path) when 'PUT' then req = Net::HTTP::Put.new(path) when 'POST' then req = Net::HTTP::Post.new(path) when 'DELETE' then req = Net::HTTP::Delete.new(path) else raise end req.body = body # All requests with JSON encoded body req['Content-Type'] = 'application/json' # Set User Agent to Gem name and version req['User-Agent'] = user_agent auth_headers(method, path, body).each do |key, val| req[key] = val end headers.each do |key, val| req[key] = val end resp = agent.request(req) out = Cryptoprocessing::NetHTTPResponse.new(resp) Cryptoprocessing::check_response_status(out) yield(out) if block_given? out.data end |
#reset_agent ⇒ Object
71 72 73 |
# File 'lib/cryptoprocessing/connection.rb', line 71 def reset_agent @agent = nil end |