Class: Remitano::Client::Net

Inherits:
Object
  • Object
show all
Defined in:
lib/remitano/client/net.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Net

Returns a new instance of Net.



25
26
27
# File 'lib/remitano/client/net.rb', line 25

def initialize(config:)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/remitano/client/net.rb', line 23

def config
  @config
end

Class Method Details

.public_get(path, params = {}) ⇒ Object



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

def self.public_get(path, params = {})
  options = {
    :url => self.to_uri(path),
    :method => :get,
    :timeout => 20,
    :headers => {
      :params => params,
      :lang => "vi"
    }
  }
  req = RestClient::Request.new(options)
  req.headers['Content-Type'] = 'application/json'
  Remitano::Client::Request.new(req)
end

.serverObject



33
34
35
# File 'lib/remitano/client/net.rb', line 33

def self.server
  @server ||= (ENV['REMITANO_SERVER'] || "https://api.remitano.com")
end

.to_uri(path) ⇒ Object



29
30
31
# File 'lib/remitano/client/net.rb', line 29

def self.to_uri(path)
  return "#{server}/api/v1#{path}"
end

Instance Method Details

#delete(path, params = {}) ⇒ Object



67
68
69
70
# File 'lib/remitano/client/net.rb', line 67

def delete(path, params={})
  request = new_request(:delete, path, params)
  sign_request(request)
end

#get(path, params = {}) ⇒ Object



52
53
54
55
# File 'lib/remitano/client/net.rb', line 52

def get(path, params={})
  request = new_request(:get, path, params)
  sign_request(request)
end

#new_request(method, path, params = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/remitano/client/net.rb', line 72

def new_request(method, path, params={})
  p [:new_request, method, path] if config.verbose

  options = {
    :method => method,
    :timeout => 20
  }

  usec = Time.now.usec
  if method == :get
    path += (path.include?("?") ? "&" : "?")
    path += "usec=#{usec}"
  else
    params[:usec] = usec
    options[:payload] = params.to_json
  end

  options[:url] = self.class.to_uri(path)

  RestClient::Request.new(options)
end

#patch(path, params = {}) ⇒ Object



62
63
64
65
# File 'lib/remitano/client/net.rb', line 62

def patch(path, params={})
  request = new_request(:put, path, params)
  sign_request(request)
end

#post(path, params = {}) ⇒ Object



57
58
59
60
# File 'lib/remitano/client/net.rb', line 57

def post(path, params={})
  request = new_request(:post, path, params)
  sign_request(request)
end

#sign_request(req) ⇒ Object



94
95
96
97
98
# File 'lib/remitano/client/net.rb', line 94

def sign_request(req)
  req.headers['Content-Type'] = 'application/json'
  ApiAuth.sign!(req, config.key, config.secret)
  Remitano::Client::Request.new(req)
end