Class: Refocus::Http
- Inherits:
-
Object
- Object
- Refocus::Http
- Defined in:
- lib/refocus/http.rb
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #connection(path) ⇒ Object
- #convert(body) ⇒ Object
- #delete(path) ⇒ Object
- #get(path) ⇒ Object
- #headers ⇒ Object
-
#initialize(url:, token:, content_type: "application/json") ⇒ Http
constructor
A new instance of Http.
- #patch(path, body:) ⇒ Object
- #post(path, body:, expects: 201) ⇒ Object
- #put(path, body:) ⇒ Object
Constructor Details
#initialize(url:, token:, content_type: "application/json") ⇒ Http
Returns a new instance of Http.
6 7 8 9 10 |
# File 'lib/refocus/http.rb', line 6 def initialize(url:, token:, content_type: "application/json") @url = url @token = token @content_type = content_type end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
4 5 6 |
# File 'lib/refocus/http.rb', line 4 def content_type @content_type end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
4 5 6 |
# File 'lib/refocus/http.rb', line 4 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
4 5 6 |
# File 'lib/refocus/http.rb', line 4 def url @url end |
Instance Method Details
#connection(path) ⇒ Object
43 44 45 |
# File 'lib/refocus/http.rb', line 43 def connection(path) Excon.new("#{url}/#{path}") end |
#convert(body) ⇒ Object
39 40 41 |
# File 'lib/refocus/http.rb', line 39 def convert(body) content_type == "application/json" ? body.to_json : body end |
#delete(path) ⇒ Object
28 29 30 |
# File 'lib/refocus/http.rb', line 28 def delete(path) connection(path).delete(headers: headers, expects: 200) end |
#get(path) ⇒ Object
16 17 18 |
# File 'lib/refocus/http.rb', line 16 def get(path) connection(path).get(headers: headers, expects: 200) end |
#headers ⇒ Object
32 33 34 35 36 37 |
# File 'lib/refocus/http.rb', line 32 def headers { "Authorization" => token, "Content-Type" => content_type } end |
#patch(path, body:) ⇒ Object
20 21 22 |
# File 'lib/refocus/http.rb', line 20 def patch(path, body:) connection(path).patch(body: convert(body), headers: headers, expects: 200) end |
#post(path, body:, expects: 201) ⇒ Object
12 13 14 |
# File 'lib/refocus/http.rb', line 12 def post(path, body:, expects: 201) connection(path).post(body: convert(body), headers: headers, expects: expects) end |
#put(path, body:) ⇒ Object
24 25 26 |
# File 'lib/refocus/http.rb', line 24 def put(path, body:) connection(path).put(body: convert(body), headers: headers, expects: 201) end |