Class: GeekierFactory::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/geekier_factory/action.rb

Constant Summary collapse

AttributesMissing =
Class.new(Exception)
ValidationException =
Class.new(Exception)
Retry =
Class.new(Exception)
ConnectionException =
Class.new(Exception)

Instance Method Summary collapse

Constructor Details

#initialize(api, structure) ⇒ Action

Returns a new instance of Action.



6
7
8
9
# File 'lib/geekier_factory/action.rb', line 6

def initialize(api, structure)
  @api = api
  @structure = structure
end

Instance Method Details

#api_connectionObject



78
79
80
# File 'lib/geekier_factory/action.rb', line 78

def api_connection
  @api.api_connection
end

#body_hash(param_values) ⇒ Object



32
33
34
35
# File 'lib/geekier_factory/action.rb', line 32

def body_hash(param_values)
  names = body_params.map{ |p| p['name'] }
  param_values.select{ |k,v| names.include?(k.to_s) }
end

#body_paramsObject



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

def body_params
  params.select{ |p| p['paramType'] == 'body' }
end

#build_body(param_values) ⇒ Object



54
55
56
# File 'lib/geekier_factory/action.rb', line 54

def build_body(param_values)
  body_hash(param_values)
end

#build_url(param_values) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/geekier_factory/action.rb', line 42

def build_url(param_values)
  vals = path_hash(param_values)
  if (as = (path_variables - vals.keys.map(&:to_s))).any?
    raise AttributesMissing.new(as)
  end
  p = path
  vals.each do |k, v|
    p = p.sub("{#{k}}", v)
  end
  api_connection.build_url(p, url_hash(param_values))
end

#call(param_values = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/geekier_factory/action.rb', line 82

def call(param_values = {})
  validate_parameter_values(param_values)
  reqhash = request_hash(param_values)
  response = api_connection.run_request(reqhash[:verb], reqhash[:url], reqhash[:body], reqhash[:headers])
  handle_response!(response)
  {:request => reqhash, :response => response}
rescue Retry => e
  sleep 0.5
  retry
end

#error_responsesObject



133
134
135
# File 'lib/geekier_factory/action.rb', line 133

def error_responses
  (@structure['errorResponses'] || []) + @api.error_responses
end

#handle_response!(response) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/geekier_factory/action.rb', line 139

def handle_response!(response)
  if error_responses.map{ |er| er['code'] }.include? response.status
    ex = error_responses.find{ |er| er['code'] == response.status }
    if ex.has_key?('retry') && (@retries ||= 0) < ex['retry'].to_i
      @retries += 1
      raise Retry.new
    else
      message = "#{ex['reason']} (HTTP status code #{ex['code']})"
      message = message + "\n\n" + response[:body] if ex['details'] && ex['details'] == 'body'
      raise ConnectionException.new(message)
    end
  elsif !response.success?
    raise ConnectionException.new
  end
end

#http_methodObject



58
59
60
# File 'lib/geekier_factory/action.rb', line 58

def http_method
  @structure['httpMethod']
end

#paramsObject



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

def params
  @structure['parameters']
end

#pathObject



62
63
64
# File 'lib/geekier_factory/action.rb', line 62

def path
  @structure['path'].start_with?('/') ? @structure['path'][1..-1] : @structure['path']
end

#path_hash(param_values) ⇒ Object



37
38
39
40
# File 'lib/geekier_factory/action.rb', line 37

def path_hash(param_values)
  names = path_params.map{ |p| p['name'] }
  param_values.select{ |k,v| names.include?(k.to_s) }
end

#path_paramsObject



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

def path_params
  params.select{ |p| p['paramType'] == 'path' }
end

#path_variablesObject



66
67
68
# File 'lib/geekier_factory/action.rb', line 66

def path_variables
  path.scan(/\{(\w*)\}/).flatten
end

#request_hash(param_values) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/geekier_factory/action.rb', line 70

def request_hash(param_values)
  { 
    :verb => http_method.downcase.to_sym,
    :url => build_url(param_values),
    :body => build_body(param_values)
  }
end

#url_hash(param_values) ⇒ Object



27
28
29
30
# File 'lib/geekier_factory/action.rb', line 27

def url_hash(param_values)
  names = url_params.map{ |p| p['name'] }
  param_values.select{ |k,v| names.include?(k.to_s) }
end

#url_paramsObject



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

def url_params
  params.select{ |p| p['paramType'] == 'query' }
end

#validate_parameter_values(param_values) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/geekier_factory/action.rb', line 93

def validate_parameter_values(param_values)
  errors = Hash.new { |hash,key|hash[key] = [] }

  param_values.each do |k,v|
    parameter = params.detect{|p| p['name'] == k.to_s}

    errors[k] << "expected to be #{parameter['dataType']} (was #{p.class})" unless case parameter['dataType']
                                                                                   when 'byte'
                                                                                     true
                                                                                   when 'string'
                                                                                     v.is_a?(String)
                                                                                   when 'boolean'
                                                                                     v.is_a?(TrueClass) ||
                                                                                     v.is_a?(FalseClass)
                                                                                   when 'int'
                                                                                     v.is_a?(Integer)
                                                                                   when 'float'
                                                                                     v.is_a?(Float)
                                                                                   when 'double'
                                                                                     v.is_a?(Double)
                                                                                   when 'Date'
                                                                                     (v.is_a?(Date) || Date.parse(v)) rescue false
                                                                                   end

    if parameter.has_key?('allowableValues')
      vals = case parameter['allowableValues']['valueType']
      when 'RANGE'
        Range.new(parameter['allowableValues']['min'].to_i, parameter['allowableValues']['max'].to_i)
      when 'LIST'
        parameter['allowableValues']['values']
      end
      errors[k] << "expected to be one of #{vals}" unless vals.include?(v)
    end
  end

  if errors.any?
    raise ValidationException.new(errors.inspect)
  end
end