Class: Arbetsformedlingen::API::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/arbetsformedlingen/api/response.rb

Overview

API response object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, uri = nil) ⇒ Response

Initialize response



15
16
17
18
19
# File 'lib/arbetsformedlingen/api/response.rb', line 15

def initialize(response, uri = nil)
  @response = response
  @json = nil
  @uri = uri
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Delegate missing values to response



46
47
48
49
50
51
52
# File 'lib/arbetsformedlingen/api/response.rb', line 46

def method_missing(method_name, *arguments, &block)
  if response.respond_to?(method_name)
    response.public_send(method_name, *arguments, &block)
  else
    super
  end
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



12
13
14
# File 'lib/arbetsformedlingen/api/response.rb', line 12

def uri
  @uri
end

Instance Method Details

#bodyString

Response body

Returns:

  • (String)

    the response body



29
30
31
# File 'lib/arbetsformedlingen/api/response.rb', line 29

def body
  response.read_body
end

#jsonHash

Response JSON

Returns:

  • (Hash)

    response json - empty if JSON is invalid



35
36
37
# File 'lib/arbetsformedlingen/api/response.rb', line 35

def json
  @json ||= parse_json(body)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Return true if missing method can be delegated

Returns:

  • (Boolean)


55
56
57
# File 'lib/arbetsformedlingen/api/response.rb', line 55

def respond_to_missing?(method_name, include_private = false)
  response.respond_to?(method_name) || super
end

#success?Boolean

True if response is 200

Returns:

  • (Boolean)

    true if response code is 200



23
24
25
# File 'lib/arbetsformedlingen/api/response.rb', line 23

def success?
  response.code == '200'
end

#xmlNokogiri::XML::Document

Response XML

Returns:

  • (Nokogiri::XML::Document)

    response - empty is XML is invalid



41
42
43
# File 'lib/arbetsformedlingen/api/response.rb', line 41

def xml
  @xml ||= parse_xml(body)
end