Module: Ja

Defined in:
lib/ja.rb,
lib/ja/api.rb,
lib/ja/error.rb,
lib/ja/methods.rb,
lib/ja/version.rb,
lib/ja/debug_logger.rb

Defined Under Namespace

Modules: DebugLogger, Methods Classes: API, Error

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.api(**kwargs, &block) ⇒ Object



31
32
33
# File 'lib/ja.rb', line 31

def self.api(**kwargs, &block)
  API.new(**kwargs, &block)
end

.default_loggerObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ja.rb', line 35

def self.default_logger
  if defined?(Rails) && Rails.logger
    Rails.logger
  elsif defined?(Hanami) && Hanami.logger
    Hanami.logger
  elsif defined?(SemanticLogger)
    SemanticLogger[self]
  else
    Logger.new($stdout)
  end
end

.enable_debug_logging!Object



76
77
78
79
# File 'lib/ja.rb', line 76

def self.enable_debug_logging!
  return if HTTP::Client.ancestors.include?(DebugLogger)
  HTTP::Client.prepend(DebugLogger)
end

.enable_semantic_logging=(bool) ⇒ Object



23
24
25
# File 'lib/ja.rb', line 23

def self.enable_semantic_logging=(bool)
  @enable_semantic_logging = bool
end

.enable_semantic_logging?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ja.rb', line 27

def self.enable_semantic_logging?
  !!@enable_semantic_logging
end

.format_body(headers, &body) ⇒ Object

TODO detect streaming



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ja.rb', line 48

def self.format_body(headers, &body)
  mime_type = parse_mime_type(headers)
  case mime_type
  when /\bjson$/
    str = body.call
    begin
      JSON.pretty_generate(JSON.parse(str))
    rescue JSON::ParserError
      str
    end
  when /\bhtml$/, /\bxml$/
    str = body.call
    if defined?(Nokogiri)
      Nokogiri::XML(str).to_xhtml.chomp
    else
      str
    end
  when /\bplain$/
    body.call
  else
    "«body ommitted: unsupported Content-Type: #{mime_type.inspect}»"
  end
end

.loggerObject



15
16
17
# File 'lib/ja.rb', line 15

def self.logger
  @logger ||= default_logger # rubocop:disable Naming/MemoizedInstanceVariableName
end

.logger=(logger) ⇒ Object



19
20
21
# File 'lib/ja.rb', line 19

def self.logger=(logger)
  @logger = logger
end

.parse_mime_type(headers) ⇒ Object



72
73
74
# File 'lib/ja.rb', line 72

def self.parse_mime_type(headers)
  HTTP::ContentType.parse(headers[HTTP::Headers::CONTENT_TYPE]).mime_type
end