Module: Foobara::HttpApiCommand
- Includes:
- Concern, Concerns::Url
- Defined in:
- foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb,
foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb
Defined Under Namespace
Modules: Concerns
Instance Attribute Summary collapse
-
#request_body ⇒ Object
Returns the value of attribute request_body.
-
#request_headers ⇒ Object
Returns the value of attribute request_headers.
-
#response ⇒ Object
Returns the value of attribute response.
-
#response_body ⇒ Object
Returns the value of attribute response_body.
Instance Method Summary collapse
- #build_request_body ⇒ Object
- #build_request_headers ⇒ Object
- #build_result ⇒ Object
- #execute ⇒ Object
- #issue_http_request ⇒ Object
- #parse_response ⇒ Object
Methods included from Concerns::Url
#api_uri_object, #api_url, #net_http
Methods included from Concern
foobara_class_methods_module_for, foobara_concern?, included
Instance Attribute Details
#request_body ⇒ Object
Returns the value of attribute request_body.
18 19 20 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 18 def request_body @request_body end |
#request_headers ⇒ Object
Returns the value of attribute request_headers.
18 19 20 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 18 def request_headers @request_headers end |
#response ⇒ Object
Returns the value of attribute response.
18 19 20 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 18 def response @response end |
#response_body ⇒ Object
Returns the value of attribute response_body.
18 19 20 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 18 def response_body @response_body end |
Instance Method Details
#build_request_body ⇒ Object
20 21 22 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 20 def build_request_body self.request_body = {} end |
#build_request_headers ⇒ Object
24 25 26 27 28 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 24 def build_request_headers self.request_headers = { "Content-Type" => "application/json" } end |
#build_result ⇒ Object
55 56 57 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 55 def build_result response_body end |
#execute ⇒ Object
10 11 12 13 14 15 16 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 10 def execute build_request_body build_request_headers issue_http_request parse_response build_result end |
#issue_http_request ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 30 def issue_http_request request = case self.class.http_method when :get uri = if request_body.empty? api_uri_object else api_uri_object.dup.tap do |new_uri| new_uri.query = URI.encode_www_form(request_body) end end Net::HTTP::Get.new(uri.request_uri, request_headers) when :post Net::HTTP::Post.new(api_uri_object.request_uri, request_headers).tap do |post| post.body = JSON.generate(request_body) end else # :nocov: raise "Unknown http method #{self.class.http_method}" # :nocov: end self.response = net_http.request(request) end |
#parse_response ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 59 def parse_response json = if response.is_a?(Net::HTTPSuccess) response.body else # :nocov: raise "Could not successfully hit #{api_url}: " \ "#{response.code} #{response.}" # :nocov: end self.response_body = JSON.parse(json) end |