Class: OrcaApi::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/orca_api/result.rb

Overview

日レセAPIの呼び出し結果を扱うクラス

Constant Summary

LOCKED_API_RESULT =
Set.new(%w(E90 E9999))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Result

Returns a new instance of Result



65
66
67
68
69
70
# File 'lib/orca_api/result.rb', line 65

def initialize(raw)
  @raw = raw
  @attr_names = body.keys.map { |key|
    [Client.underscore(key).to_sym, key]
  }.to_h
end

Constructor Details

#initialize(raw) ⇒ Result

Returns a new instance of Result



65
66
67
68
69
70
# File 'lib/orca_api/result.rb', line 65

def initialize(raw)
  @raw = raw
  @attr_names = body.keys.map { |key|
    [Client.underscore(key).to_sym, key]
  }.to_h
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/orca_api/result.rb', line 100

def method_missing(symbol, *args)
  if (key = @attr_names[symbol])
    body[key]
  else
    super
  end
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



63
64
65
# File 'lib/orca_api/result.rb', line 63

def raw
  @raw
end

Class Method Details

.def_info(name, *path) ⇒ Array<Array, Hash>

深いパスのレスポンスボディに対するアクセサーを定義するメソッド

Examples:

class SomeResult < Result
  def_info :some_info, "Some_Information", "Some_Info"
end
res = SomeResult.new({
                       "someres" => {
                         "Some_Information" => {
                           "Some_Info" => [
                             { "Some_ID" => "foo" }, { "Some_ID" => "bar" }
                           ]
                         }
                       }
                     }.to_json)
res.some_info
# => [ { "Some_ID" => "foo" }, { "Some_ID" => "bar" } ]
res = SomeResult.new({
                       "someres" => {
                       }
                     }.to_json)
res.some_info
# => []

Parameters:

  • name (String, Symbol)

    定義するメソッド名

  • path (Array<String>)

    レスポンスボディのパス

Returns:

  • (Array<Array, Hash>)

    レスポンスボディ



57
58
59
60
61
# File 'lib/orca_api/result.rb', line 57

def self.def_info(name, *path)
  define_method name do
    Array(body.dig(*path))
  end
end

.parse(raw) ⇒ Object



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

def self.parse(raw)
  trim_response(JSON.parse(raw)).first[1]
end

.trim_response(hash) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/orca_api/result.rb', line 8

def self.trim_response(hash)
  result = {}
  hash.each do |k, v|
    result[k] = case v
                when Hash
                  trim_response(v)
                when Array
                  v.reverse_each.drop_while(&:empty?).reverse.map { |e| trim_response(e) }
                else
                  v
                end
  end
  result
end

Instance Method Details

#[](key) ⇒ Object



76
77
78
# File 'lib/orca_api/result.rb', line 76

def [](key)
  body[key]
end

#bodyObject



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

def body
  @body ||= self.class.parse(@raw)
end

#locked?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/orca_api/result.rb', line 92

def locked?
  LOCKED_API_RESULT.include?(api_result)
end

#messageObject



96
97
98
# File 'lib/orca_api/result.rb', line 96

def message
  "#{api_result_message}(#{api_result})"
end

#ok?Boolean

Api_Resultが00、0000、W00といった処理完了を示す値である場合にtrueを返す

Returns:

  • (Boolean)

    Api_Resultが処理完了を示す値である場合にtrueを返す



84
85
86
# File 'lib/orca_api/result.rb', line 84

def ok?
  /\AW?0+\z/.match? api_result
end

#respond_to_missing?(symbol, arg) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
# File 'lib/orca_api/result.rb', line 108

def respond_to_missing?(symbol, arg)
  if @attr_names.key?(symbol)
    true
  else
    super
  end
end

#warning?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/orca_api/result.rb', line 88

def warning?
  /\AW/.match? api_result
end