Class: Arbetsformedlingen::API::Request
- Inherits:
-
Object
- Object
- Arbetsformedlingen::API::Request
- Defined in:
- lib/arbetsformedlingen/api/request.rb
Overview
API request object
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
Instance Method Summary collapse
-
#get(url, query: {}) ⇒ Response
Perform GET request.
-
#initialize(base_url: '', locale: 'sv') ⇒ Request
constructor
Initialize request.
-
#post(url, data: {}) ⇒ Response
Perform POST request.
Constructor Details
#initialize(base_url: '', locale: 'sv') ⇒ Request
Initialize request
15 16 17 18 |
# File 'lib/arbetsformedlingen/api/request.rb', line 15 def initialize(base_url: '', locale: 'sv') @base_url = base_url @locale = locale end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
12 13 14 |
# File 'lib/arbetsformedlingen/api/request.rb', line 12 def base_url @base_url end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
12 13 14 |
# File 'lib/arbetsformedlingen/api/request.rb', line 12 def locale @locale end |
Instance Method Details
#get(url, query: {}) ⇒ Response
Perform GET request
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/arbetsformedlingen/api/request.rb', line 24 def get(url, query: {}) uri = to_uri(url, query: query) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri) request['Content-Type'] = 'application/json' request['Accept-Language'] = locale response = http.request(request) # AFs APIs somtimes redirects with HTTP 303 if response.code == '303' && response.header['Location'] return get(response.header['Location'], query: query) end Response.new(response, uri) end |
#post(url, data: {}) ⇒ Response
Perform POST request
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/arbetsformedlingen/api/request.rb', line 45 def post(url, data: {}) uri = to_uri(url) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri) request['Content-Type'] = 'application/json' request['Accept-Language'] = locale request['cache-control'] = 'no-cache' request.body = JSON.dump(data) response = http.request(request) Response.new(response, uri) end |