class SCB::API
Attributes
config[RW]
Public Class Methods
new(config = nil) { |config| ... }
click to toggle source
# File lib/scb/api.rb, line 11 def initialize(config = nil) @config = config || API::Config.new yield(@config) if block_given? end
Public Instance Methods
base_url()
click to toggle source
# File lib/scb/api.rb, line 39 def base_url config.base_url end
dump_json(obj)
click to toggle source
# File lib/scb/api.rb, line 51 def dump_json(obj) config.json_parser.dump(obj) end
get(path = nil)
click to toggle source
# File lib/scb/api.rb, line 17 def get(path = nil) response = http_get(path) if response && response.body response.body.force_encoding('UTF-8') end end
get_and_parse(path = nil)
click to toggle source
# File lib/scb/api.rb, line 25 def get_and_parse(path = nil) load_json get(path) end
load_json(doc)
click to toggle source
# File lib/scb/api.rb, line 47 def load_json(doc) config.json_parser.load(doc) end
post(path, query)
click to toggle source
# File lib/scb/api.rb, line 29 def post(path, query) response = http_post path, dump_json(query) response_body_without_utf8_bom(query, response) end
post_and_parse(path, query)
click to toggle source
# File lib/scb/api.rb, line 34 def post_and_parse(path, query) query.merge!({ response: { format: "json" }}) load_json post(path, query) end
uri(endpoint = nil)
click to toggle source
# File lib/scb/api.rb, line 43 def uri(endpoint = nil) URI.parse("#{base_url}/#{endpoint}") end
Private Instance Methods
http_get(path)
click to toggle source
# File lib/scb/api.rb, line 69 def http_get(path) config.http_client.get(uri(path)) end
http_post(path, payload)
click to toggle source
# File lib/scb/api.rb, line 73 def http_post(path, payload) config.http_client.post(uri(path), payload) end
response_body_without_utf8_bom(query, response)
click to toggle source
# File lib/scb/api.rb, line 57 def response_body_without_utf8_bom(query, response) if query[:response] && query[:response][:format] == "json" # Force the response body to be UTF-8 body = response.body.force_encoding('UTF-8') # Return the body, stripped of the BOM body.sub!(/^\xEF\xBB\xBF/, '') else response.body end end