Class: Magentwo::Connection

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

Direct Known Subclasses

Adapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, user: nil, password: nil, base_path: nil, token: nil) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/connection.rb', line 5

def initialize uri:, user:nil, password:nil, base_path:nil, token:nil
  uri = URI(uri)
  @host = uri.host
  @port = uri.port
  @scheme = uri.scheme
  @base_path = base_path || "/rest/V1"

  if (user && password)
    @user = user
    @password = password
    request_token
  elsif (token)
    @token = token
  else
    raise ArgumentError, "expected user/password or token"
  end

end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



3
4
5
# File 'lib/connection.rb', line 3

def base_path
  @base_path
end

#hostObject

Returns the value of attribute host.



3
4
5
# File 'lib/connection.rb', line 3

def host
  @host
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/connection.rb', line 3

def password
  @password
end

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/connection.rb', line 3

def port
  @port
end

#schemeObject

Returns the value of attribute scheme.



3
4
5
# File 'lib/connection.rb', line 3

def scheme
  @scheme
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/connection.rb', line 3

def token
  @token
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/connection.rb', line 3

def user
  @user
end

Instance Method Details

#delete(path, data) ⇒ Object



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

def delete path, data
  request Net::HTTP::Delete, path:path, data:data
end

#get(path, query) ⇒ Object



63
64
65
# File 'lib/connection.rb', line 63

def get path, query
  request Net::HTTP::Get, path:"#{path}?#{query}"
end

#post(path, data) ⇒ Object



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

def post path, data
  request Net::HTTP::Post, path:path, data:data
end

#put(path, data) ⇒ Object



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

def put path, data
  request Net::HTTP::Put, path:path, data:data
end

#request(verb, path:, data: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/connection.rb', line 37

def request verb, path:, data:nil
  Magentwo.logger.info "#{verb.to_s} #{host}/#{base_path}/#{path}"
  Magentwo.logger.debug "DATA #{data}"

  url = "#{base_path}/#{path}"
  Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
    req = verb.new(url)
    req["Authorization"] = "Bearer #{self.token}"
    req['Content-Type'] = "application/json"
    req.body = data
    http.request(req)
  end
end

#request_tokenObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/connection.rb', line 24

def request_token
  Net::HTTP.start(self.host,self.port, :use_ssl => self.scheme == 'https') do |http|
    url = "#{base_path}/integration/admin/token"
    Magentwo.logger.info "POST #{url}"
    req = Net::HTTP::Post.new(url)
    req.body = {:username=> self.user, :password=> self.password}.to_json
    req['Content-Type'] = "application/json"
    req['Content-Length'] = req.body.length
    response = http.request(req).body
    @token = JSON.parse response
  end
end