Class: Bark::Response
- Inherits:
-
Object
- Object
- Bark::Response
- Defined in:
- lib/bark/response.rb
Instance Attribute Summary collapse
-
#json ⇒ Object
readonly
All OT responses are in json format.
Instance Method Summary collapse
-
#initialize(request: {}) ⇒ Response
constructor
A new instance of Response.
-
#parse_json(string) ⇒ Object
Parse the json, and store it in @json.
- #request_failure_message ⇒ Object
- #request_succeeded? ⇒ Boolean
- #result ⇒ Object
Constructor Details
#initialize(request: {}) ⇒ Response
Returns a new instance of Response.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bark/response.rb', line 7 def initialize(request: {}) raise 'No request passed' if request == {} || request.nil? # warn Bark::Error, 'Warning request is not valid, making it anyway.' if !request.valid? @json = {} if [:get_study, :get_study_tree ].include?( request.method ) req = Net::HTTP::Get.new(request.uri) else req = Net::HTTP::Post.new(request.uri, initheader = {'Content-Type' =>'application/json'}) end res = Net::HTTP.start(request.uri.hostname, request.uri.port) do |http| req.body = request.json_payload http.request(req) end parse_json(res.body) end |
Instance Attribute Details
#json ⇒ Object (readonly)
All OT responses are in json format. The json is stored here as a hash (dictionary).
5 6 7 |
# File 'lib/bark/response.rb', line 5 def json @json end |
Instance Method Details
#parse_json(string) ⇒ Object
Parse the json, and store it in @json.
29 30 31 32 33 34 35 36 |
# File 'lib/bark/response.rb', line 29 def parse_json(string) begin @json = JSON.parse(string) rescue JSON::ParserError => e puts e. ap request end end |
#request_failure_message ⇒ Object
42 43 44 |
# File 'lib/bark/response.rb', line 42 def @json['message'] end |
#request_succeeded? ⇒ Boolean
38 39 40 |
# File 'lib/bark/response.rb', line 38 def request_succeeded? !@json['exception'] && !@json['error'] end |
#result ⇒ Object
46 47 48 |
# File 'lib/bark/response.rb', line 46 def result @json end |