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.



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

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.



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

def base_url
  @base_url
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

Instance Method Details

#add_headers(headers) ⇒ Object



91
92
93
94
# File 'lib/tina4/api.rb', line 91

def add_headers(headers)
  @headers.merge!(headers)
  self
end

#delete(path, body: nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/tina4/api.rb', line 60

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

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



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

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

#patch(path, body: nil, content_type: "application/json") ⇒ Object



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

def patch(path, body: nil, content_type: "application/json")
  uri = build_uri(path)
  request = Net::HTTP::Patch.new(uri)
  if body
    request.body = body.is_a?(String) ? body : JSON.generate(body)
    request["Content-Type"] = content_type
  end
  apply_headers(request, {})
  execute(uri, request)
end

#post(path, body: nil, content_type: "application/json") ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/tina4/api.rb', line 27

def post(path, body: nil, content_type: "application/json")
  uri = build_uri(path)
  request = Net::HTTP::Post.new(uri)
  if body
    request.body = body.is_a?(String) ? body : JSON.generate(body)
    request["Content-Type"] = content_type
  end
  apply_headers(request, {})
  execute(uri, request)
end

#put(path, body: nil, content_type: "application/json") ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/tina4/api.rb', line 38

def put(path, body: nil, content_type: "application/json")
  uri = build_uri(path)
  request = Net::HTTP::Put.new(uri)
  if body
    request.body = body.is_a?(String) ? body : JSON.generate(body)
    request["Content-Type"] = content_type
  end
  apply_headers(request, {})
  execute(uri, request)
end

#send_request(method = "GET", path = "", body: nil, content_type: "application/json") ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/tina4/api.rb', line 96

def send_request(method = "GET", path = "", body: nil, content_type: "application/json")
  case method.upcase
  when "GET"    then get(path)
  when "POST"   then post(path, body: body, content_type: content_type)
  when "PUT"    then put(path, body: body, content_type: content_type)
  when "PATCH"  then patch(path, body: body, content_type: content_type)
  when "DELETE" then delete(path, body: body)
  else get(path)
  end
end

#set_basic_auth(username, password) ⇒ Object



81
82
83
84
# File 'lib/tina4/api.rb', line 81

def set_basic_auth(username, password)
  @headers["Authorization"] = "Basic #{Base64.strict_encode64("#{username}:#{password}")}"
  self
end

#set_bearer_token(token) ⇒ Object



86
87
88
89
# File 'lib/tina4/api.rb', line 86

def set_bearer_token(token)
  @headers["Authorization"] = "Bearer #{token}"
  self
end

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



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tina4/api.rb', line 68

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