Class: SCB::API

Inherits:
Object
  • Object
show all
Defined in:
lib/scb/api.rb,
lib/scb/api/config.rb

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) {|@config| ... } ⇒ API

Returns a new instance of API.

Yields:



11
12
13
14
15
# File 'lib/scb/api.rb', line 11

def initialize(config = nil)
  @config = config || API::Config.new

  yield(@config) if block_given?
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/scb/api.rb', line 9

def config
  @config
end

Instance Method Details

#base_urlObject



39
40
41
# File 'lib/scb/api.rb', line 39

def base_url
  config.base_url
end

#dump_json(obj) ⇒ Object



51
52
53
# File 'lib/scb/api.rb', line 51

def dump_json(obj)
  config.json_parser.dump(obj)
end

#get(path = nil) ⇒ Object



17
18
19
20
21
22
23
# 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) ⇒ Object



25
26
27
# File 'lib/scb/api.rb', line 25

def get_and_parse(path = nil)
  load_json get(path)
end

#load_json(doc) ⇒ Object



47
48
49
# File 'lib/scb/api.rb', line 47

def load_json(doc)
  config.json_parser.load(doc)
end

#post(path, query) ⇒ Object



29
30
31
32
# 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) ⇒ Object



34
35
36
37
# 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) ⇒ Object



43
44
45
# File 'lib/scb/api.rb', line 43

def uri(endpoint = nil)
  URI.parse("#{base_url}/#{endpoint}")
end