Class: BigcommerceTest::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/bigcommerce_test/connection.rb

Instance Method Summary collapse

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

#configurationObject



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, options = {}, headers = {})
  request(:delete, path, options, headers)
end

#get(path, options = {}, headers = {}) ⇒ Object



48
49
50
# File 'lib/bigcommerce_test/connection.rb', line 48

def get(path, options = {}, headers = {})
  request(:get, path, options, headers)
end

#post(path, options = {}, headers = {}) ⇒ Object



52
53
54
# File 'lib/bigcommerce_test/connection.rb', line 52

def post(path, options = {}, headers = {})
  request(:post, path, options, headers)
end

#put(path, options = {}, headers = {}) ⇒ Object



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

def put(path, options = {}, headers = {})
  request(:put, path, options, 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, options,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 => options, :accept => :json, :content_type => :json
               when :post then
                 restclient.post(options.to_json, :content_type => :json, :accept => :json)
               when :put then
                 restclient.put(options.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