Class: Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



11
12
13
14
# File 'lib/branston/lib/client.rb', line 11

def initialize(options)
  self.options = options
  self.errors = []
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#generate_story_filesObject



16
17
18
19
20
21
22
23
24
# File 'lib/branston/lib/client.rb', line 16

def generate_story_files
  begin 
    return process_xml get_xml
  rescue StandardError => e
    errors << "Could not connect to Branston server on " + 
      "#{options[:Host]}:#{options[:Port]}: #{e.message}\n" +
    "Is Branston running?"
  end
end

#get_xmlObject



68
69
70
71
72
73
74
75
76
# File 'lib/branston/lib/client.rb', line 68

def get_xml
  Net::HTTP.start(options[:Host] , options[:Port]) { |http|
    req = Net::HTTP::Get.new("/stories/#{options[:feature]}.xml")
    puts "generating /stories/#{options[:feature]}.xml"
    response = http.request(req)
    xml = REXML::Document.new response.body
    return xml
  }
end

#process_xml(xml) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/branston/lib/client.rb', line 26

def process_xml(xml)
  errors.clear
  
  if xml.nil? or xml.root.nil? or xml.root.elements.nil?
    errors << "Did not recieve XML data for story #{options[:feature]}.\n" +
    "Is the Branston server running, and have you provided the correct story name?"
  else
    
    begin
      root = xml.root
      story = OpenStruct.new
      story.description = root.elements["/story/description"].text
      story.title = root.elements["/story/title"].text
      story.scenarios = []
      root.elements.each("/story/scenarios/scenario") { |scenario|
        s = OpenStruct.new
        s.preconditions = []
        s.outcomes = []
        s.title = scenario.elements["title"].text
        
        scenario.elements.each("preconditions/precondition") { |precondition|
          p = OpenStruct.new
          p.description = precondition.elements["description"].text
          s.preconditions << p
        }
        
        scenario.elements.each("outcomes/outcome") { |outcome|          
          o = OpenStruct.new
          o.description = outcome.elements["description"].text
          s.outcomes << o
        }
        
        story.scenarios << s
      }
      
      generate(story)
    rescue StandardError => error
      errors << "Could not generate feature: " + error
    end
  end
end