Class: Liquid::Network

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

Constant Summary collapse

USER_AGENT =
"Liquid/#{Liquid::VERSION} (Ruby ;)"

Instance Method Summary collapse

Constructor Details

#initialize(end_point = nil, token = nil) ⇒ Network

Returns a new instance of Network.



6
7
8
9
10
# File 'lib/liquid/network.rb', line 6

def initialize(end_point = nil, token = nil)
  @headers = default_headers
  @end_point = end_point
  change_token token unless token.nil?
end

Instance Method Details

#change_token(token) ⇒ Object



32
33
34
# File 'lib/liquid/network.rb', line 32

def change_token(token)
  @headers['Authorization'] = "Token #{token}"
end

#default_headersObject



36
37
38
39
40
41
42
43
44
# File 'lib/liquid/network.rb', line 36

def default_headers
  {
    'User-Agent' => USER_AGENT,
    'Accept' => 'application/vnd.lqd.v1+json',
    'Content-Type' => 'application/json',
    'Accept-Encoding' => 'gzip',
    'Authorization' => 'Token '
  }
end

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



24
25
26
# File 'lib/liquid/network.rb', line 24

def delete(path, params = {})
  parse(generic_request(path).delete params, &handle_error)
end

#generic_request(path) ⇒ Object



28
29
30
# File 'lib/liquid/network.rb', line 28

def generic_request(path)
  RestClient::Resource.new("#{@end_point}#{path}", headers: @headers)
end

#get(path) ⇒ Object



12
13
14
# File 'lib/liquid/network.rb', line 12

def get(path)
  parse(generic_request(path).get(&handle_error))
end

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



16
17
18
# File 'lib/liquid/network.rb', line 16

def post(path, params = {})
  parse(generic_request(path).post params, &handle_error)
end

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



20
21
22
# File 'lib/liquid/network.rb', line 20

def put(path, params = {})
  parse(generic_request(path).put params, &handle_error)
end