Class: HTTPRequestDataParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



8
9
10
# File 'lib/HTTPRequestDataParser/HTTPRequestDataParser.rb', line 8

def flags
  @flags
end

#primary_keyObject

Returns the value of attribute primary_key.



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

def primary_key
  @primary_key
end

Instance Method Details

#fetch(urlString, class_mapping) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/HTTPRequestDataParser/HTTPRequestDataParser.rb', line 31

def fetch(urlString,class_mapping)
   puts  "start request #{urlString}..."
   uri = URI(urlString)
   res = Net::HTTP.get_response(uri)
 if res.is_a?(Net::HTTPSuccess) 
    json_objects = JSON.parse(res.body)
      parse(json_objects,class_mapping)
 end
end

#get_hash_from_array(json_object) ⇒ Object



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

def get_hash_from_array(json_object)
  if  json_object.instance_of? Array
   if json_object.length > 0  
      json_object = json_object[0]
      json_object = get_hash_from_array(json_object)
   else
        return json_object
   end
 else
  return json_object
 end
end

#parse(json_object, class_mapping) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/HTTPRequestDataParser/HTTPRequestDataParser.rb', line 54

def parse(json_object,class_mapping)
      
    json_object = get_hash_from_array(json_object)
    
    model_generator=ModelGenerator.new
   class_mapping.each_pair do |key , value|
      
    command_line = value
      command_line = command_line + " "  + @flags  if @flags
    path_components = key.split "/"
          
          final_object = json_object 
          path_components.each do |compoment|
             compoment_value = final_object[compoment]
             if compoment_value == nil || compoment_value.length ==0
                 puts "Make sure your http response is JSON and your JSON content path is correct"
             else
                final_object = get_hash_from_array(compoment_value)
             end
          end 
          #parse finnal object to command_line
          index = 0
          final_object.each_pair do |key ,value|
              param = @primary_key == key || (@primary_key.empty? && index==0) ? "*" : ""
              param += "#{key}:#{type_mapping value}"
              command_line = command_line + " " + param
              index+=1
          end
          
          foo_argv = command_line.split " "
          commandTask = model_generator.analyze_command(foo_argv)
          model_generator.generate_header
          model_generator.generate_source

          puts  "generate #{value} entity"
   end
          
end

#type_mapping(parse_object) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/HTTPRequestDataParser/HTTPRequestDataParser.rb', line 11

def type_mapping(parse_object)
  if  parse_object.kind_of? Fixnum
    return  "int"
  elsif parse_object.kind_of? Float
    return "float"
  elsif parse_object.kind_of? String
      return "string"
  elsif parse_object.kind_of? Array
      return "array"
  elsif parse_object.kind_of? FalseClass
      return "bool"
  elsif parse_object.kind_of? TrueClass
      return "bool"
  elsif  parse_object.kind_of? Hash
      return "custom"
  end

  return nil
end