Class: Tina4::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, headers: {}, timeout: 30) ⇒ API

Returns a new instance of API.



10
11
12
13
14
15
16
17
# File 'lib/tina4/api.rb', line 10

def initialize(base_url, headers: {}, timeout: 30)
  @base_url = base_url.chomp("/")
  @headers = {
    "Content-Type" => "application/json",
    "Accept" => "application/json"
  }.merge(headers)
  @timeout = timeout
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



8
9
10
# File 'lib/tina4/api.rb', line 8

def base_url
  @base_url
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/tina4/api.rb', line 8

def headers
  @headers
end

Instance Method Details

#delete(path, headers: {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/tina4/api.rb', line 50

def delete(path, headers: {})
  uri = build_uri(path)
  request = Net::HTTP::Delete.new(uri)
  apply_headers(request, headers)
  execute(uri, request)
end

#get(path, params: {}, headers: {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/tina4/api.rb', line 19

def get(path, params: {}, headers: {})
  uri = build_uri(path, params)
  request = Net::HTTP::Get.new(uri)
  apply_headers(request, headers)
  execute(uri, request)
end

#patch(path, body: nil, headers: {}) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/tina4/api.rb', line 42

def patch(path, body: nil, headers: {})
  uri = build_uri(path)
  request = Net::HTTP::Patch.new(uri)
  request.body = body.is_a?(String) ? body : JSON.generate(body) if body
  apply_headers(request, headers)
  execute(uri, request)
end

#post(path, body: nil, headers: {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/tina4/api.rb', line 26

def post(path, body: nil, headers: {})
  uri = build_uri(path)
  request = Net::HTTP::Post.new(uri)
  request.body = body.is_a?(String) ? body : JSON.generate(body) if body
  apply_headers(request, headers)
  execute(uri, request)
end

#put(path, body: nil, headers: {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/tina4/api.rb', line 34

def put(path, body: nil, headers: {})
  uri = build_uri(path)
  request = Net::HTTP::Put.new(uri)
  request.body = body.is_a?(String) ? body : JSON.generate(body) if body
  apply_headers(request, headers)
  execute(uri, request)
end

#upload(path, file_path, field_name: "file", extra_fields: {}, headers: {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tina4/api.rb', line 57

def upload(path, file_path, field_name: "file", extra_fields: {}, headers: {})
  uri = build_uri(path)
  boundary = "----Tina4Boundary#{SecureRandom.hex(16)}"

  body = build_multipart_body(boundary, file_path, field_name, extra_fields)

  request = Net::HTTP::Post.new(uri)
  request.body = body
  request["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
  headers.each { |k, v| request[k] = v }
  execute(uri, request)
end