Class: BigcommerceTest::Connection
- Inherits:
-
Object
- Object
- BigcommerceTest::Connection
- Defined in:
- lib/bigcommerce_test/connection.rb
Instance Method Summary collapse
- #api_key=(api_key) ⇒ Object
- #configuration ⇒ Object
- #delete(path, options = {}, headers = {}) ⇒ Object
- #get(path, options = {}, headers = {}) ⇒ Object
-
#initialize(configuration) ⇒ Connection
constructor
A new instance of Connection.
- #post(path, options = {}, headers = {}) ⇒ Object
- #put(path, options = {}, headers = {}) ⇒ Object
- #request(method, path, options, headers = {}) ⇒ Object
- #ssl_ca_file=(path) ⇒ Object
- #ssl_client_cert=(path) ⇒ Object
- #ssl_client_key=(path, passphrase = nil) ⇒ Object
- #store_url=(store_url) ⇒ Object
- #username=(username) ⇒ Object
- #verify_peer=(verify) ⇒ Object
Constructor Details
#initialize(configuration) ⇒ Connection
Returns a new instance of Connection.
4 5 6 7 8 9 |
# File 'lib/bigcommerce_test/connection.rb', line 4 def initialize(configuration) @configuration = {} configuration.each do |key, val| send(key.to_s + "=", val) end end |
Instance Method Details
#api_key=(api_key) ⇒ Object
24 25 26 |
# File 'lib/bigcommerce_test/connection.rb', line 24 def api_key=(api_key) @configuration[:api_key] = api_key end |
#configuration ⇒ Object
11 12 13 |
# File 'lib/bigcommerce_test/connection.rb', line 11 def configuration @configuration end |
#delete(path, options = {}, headers = {}) ⇒ Object
60 61 62 |
# File 'lib/bigcommerce_test/connection.rb', line 60 def delete(path, = {}, headers = {}) request(:delete, path, , headers) end |
#get(path, options = {}, headers = {}) ⇒ Object
48 49 50 |
# File 'lib/bigcommerce_test/connection.rb', line 48 def get(path, = {}, headers = {}) request(:get, path, , headers) end |
#post(path, options = {}, headers = {}) ⇒ Object
52 53 54 |
# File 'lib/bigcommerce_test/connection.rb', line 52 def post(path, = {}, headers = {}) request(:post, path, , headers) end |
#put(path, options = {}, headers = {}) ⇒ Object
56 57 58 |
# File 'lib/bigcommerce_test/connection.rb', line 56 def put(path, = {}, headers = {}) request(:put, path, , headers) end |
#request(method, path, options, headers = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/bigcommerce_test/connection.rb', line 64 def request(method, path, ,headers={}) restclient = RestClient::Resource.new "#{@configuration[:store_url]}/api/v2#{path}.json", @configuration[:username], @configuration[:api_key] if @configuration[:ssl_client_key] && @configuration[:ssl_client_cert] && @configuration[:ssl_ca_file] restclient = RestClient::Resource.new( "#{@configuration[:store_url]}/api/v2#{path}.json", :username => @configuration[:username], :password => @configuration[:api_key], :ssl_client_cert => @configuration[:ssl_client_cert], :ssl_client_key => @configuration[:ssl_client_key], :ssl_ca_file => @configuration[:ssl_ca_file], :verify_ssl => @configuration[:verify_ssl] ) end begin response = case method when :get then restclient.get :params => , :accept => :json, :content_type => :json when :post then restclient.post(.to_json, :content_type => :json, :accept => :json) when :put then restclient.put(.to_json, :content_type => :json, :accept => :json) when :delete then restclient.delete end if((200..201) === response.code) JSON.parse response elsif response.code == 204 nil end rescue => e raise "Failed to parse Bigcommerce response: #{e}" end end |
#ssl_ca_file=(path) ⇒ Object
32 33 34 |
# File 'lib/bigcommerce_test/connection.rb', line 32 def ssl_ca_file=(path) @configuration.ssl_ca_file = path end |
#ssl_client_cert=(path) ⇒ Object
44 45 46 |
# File 'lib/bigcommerce_test/connection.rb', line 44 def ssl_client_cert=(path) @configuration.client_cert = OpenSSL::X509::Certificate.new(File.read(path)) end |
#ssl_client_key=(path, passphrase = nil) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/bigcommerce_test/connection.rb', line 36 def ssl_client_key=(path,passphrase=nil) if passphrase.nil? @configuration.ssl_client_key = OpenSSL::PKey::RSA.new(File.read(path)) else @configuration.ssl_client_key = OpenSSL::PKey::RSA.new(File.read(path), passphrase) end end |
#store_url=(store_url) ⇒ Object
15 16 17 18 |
# File 'lib/bigcommerce_test/connection.rb', line 15 def store_url=(store_url) url = URI.parse(store_url) @configuration[:store_url] = url.scheme + "://" + url.host end |
#username=(username) ⇒ Object
20 21 22 |
# File 'lib/bigcommerce_test/connection.rb', line 20 def username=(username) @configuration[:username] = username end |
#verify_peer=(verify) ⇒ Object
28 29 30 |
# File 'lib/bigcommerce_test/connection.rb', line 28 def verify_peer=(verify) @configuration[:verify_ssl] = verify end |