Module: JsonBrowser

Defined in:
lib/jsonbrowser.rb,
lib/jsonbrowser/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.rootObject

Returns the value of attribute root.



11
12
13
# File 'lib/jsonbrowser.rb', line 11

def root
  @root
end

Class Method Details

.browse(subject) ⇒ Object



18
19
20
# File 'lib/jsonbrowser.rb', line 18

def self.browse(subject)
  puts get_url(subject)
end

.get_json_string(subject) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jsonbrowser.rb', line 33

def self.get_json_string(subject)
  return subject if subject.is_a?(String) && subject[0].in?(['{', '['])
  return subject.to_json if subject.respond_to?(:to_json)

  %i(
    to_h
    to_hash
    to_a
    to_array
    as_json
    serializable_hash
  ).each do |method_name|
    if subject.respond_to?(method_name)
      return JSON.generate(subject.public_send(method_name))
    end
  end

  raise "Cannot convert #{subject.class.name} to a JSON. "\
        "Please convert it yourself and pass a string or provide an object "\
        "with a supported method."
end

.get_url(subject) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/jsonbrowser.rb', line 22

def self.get_url(subject)
  json = get_json_string(subject)
  response = RestClient.post(
    "#{root}/documents?return=json",
    json,
    content_type: :json,
    accept: :json
  )
  "#{root}#{JSON.parse(response.body)['path']}"
end