Module: Coinsetter::Net

Defined in:
lib/coinsetter/net.rb

Class Method Summary collapse

Class Method Details

.body(res) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/coinsetter/net.rb', line 48

def self.body(res)
  if res.status == 200
    res.body
  else
    JSON.generate({response: "error", status: res.status, message: res.body})
  end
end

.connectionObject



56
57
58
59
60
61
62
# File 'lib/coinsetter/net.rb', line 56

def self.connection
  @@connection ||= Faraday.new(url: uri) do |faraday|
    faraday.request  :url_encoded
    faraday.response :logger
    faraday.adapter  Faraday.default_adapter
  end
end

.delete(path, headers = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/coinsetter/net.rb', line 38

def self.delete(path, headers={})
  res = connection.delete do |req|
    req.url path
    req.headers["Accept"] = "application/json"
    req.headers.merge!(headers)
  end

  body(res)
end

.get(path, args = {}, headers = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/coinsetter/net.rb', line 7

def self.get(path, args={}, headers={})
  res = connection.get do |req|
    req.url path, args
    req.headers["Accept"] = "application/json"
    req.headers.merge!(headers)
  end

  body(res)
end

.post(path, args = {}, headers = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/coinsetter/net.rb', line 17

def self.post(path, args={}, headers={})
  res = connection.post do |req|
    req.url path
    req.headers['Content-Type'] = 'application/json'
    req.headers.merge!(headers)
    req.body =  JSON.generate(args)
  end

  body(res)
end

.put(path, args = {}, headers = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/coinsetter/net.rb', line 28

def self.put(path, args={}, headers={})
  res = connection.put do |req|
    req.url path, args
    req.headers['Content-Type'] = 'application/json'
    req.headers.merge!(headers)
  end

  body(res)
end

.uriObject



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

def self.uri
  Coinsetter.configuration.uri
end