Module: SimpleSDKBuilder::Base::ClassMethods

Defined in:
lib/simple_sdk_builder/base.rb

Instance Method Summary collapse

Instance Method Details

#check_response(response) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/simple_sdk_builder/base.rb', line 101

def check_response(response)
  return if response.code.to_s =~ /^2/

  error_handlers = config[:error_handlers] || {}

  if response.timed_out?
    raise_response_error(error_handlers[nil], response, 'timed out before receiving response')
  end

  # search for exact match
  error_handlers.each do |key, error|
    if key.is_a?(Integer) || key.is_a?(String) || key.is_a?(Symbol)
      if response.code.to_s == key.to_s
        raise error, response
      end
    end
  end

  # search for regex match
  error_handlers.each do |key, error|
    if key.is_a?(Regexp)
      if !!(key =~ response.code.to_s)
        raise error, response
      end
    end
  end

  raise_response_error(
    error_handlers['*'],
    response,
    "an error occurred with the response; code: #{response.code}; body: #{response.body};"
  )
end

#json_request(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/simple_sdk_builder/base.rb', line 59

def json_request(options = {})
  options = config.merge({
    :path => '/',
    :method => :get,
    :body => nil,
    :params => nil,
    :build => false
  }).merge(options)

  options[:headers] = {
    'Content-Type' => 'application/json'
  }.merge(options[:headers] || {})

  hydra = config[:hydra] || Typhoeus::Hydra.new

  url = "#{options[:service_url]}#{options[:path]}"

  request_body = options[:body]
  if request_body && !request_body.is_a?(String)
    request_body = request_body.to_json
  end

  request = Typhoeus::Request.new url,
    :method => options[:method],
    :timeout => options[:timeout],
    :headers => options[:headers],
    :params => options[:params],
    :body => request_body

  logger.debug "running HTTP #{options[:method]}: #{url}; PARAMS: #{options[:params]}; BODY: #{request_body};"

  hydra.queue(request)
  hydra.run

  response = request.response

  logger.debug "received response code #{response.code}; BODY: #{response.body};"

  check_response(response)
  Response.new(response)
end

#loggerObject



135
136
137
# File 'lib/simple_sdk_builder/base.rb', line 135

def logger
  config[:logger]
end