Module: Zipper

Defined in:
lib/zipper.rb

Constant Summary collapse

DEFAULT_PORT =
3001
DEFAULT_ROOT =
"http://localhost:#{DEFAULT_PORT}/"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.filesObject

Returns the value of attribute files.



12
13
14
# File 'lib/zipper.rb', line 12

def files
  @files
end

.portObject

Returns the value of attribute port.



10
11
12
# File 'lib/zipper.rb', line 10

def port
  @port
end

.rootObject

Returns the value of attribute root.



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

def root
  @root
end

.usersObject

Returns the value of attribute users.



13
14
15
# File 'lib/zipper.rb', line 13

def users
  @users
end

Class Method Details

.fail(message) ⇒ Object



188
189
190
191
192
# File 'lib/zipper.rb', line 188

def fail(message)
  p "*********************FAILED*********************"
  p message
  p "************************************************"
end

.runObject



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

def run
  system "rails s --environment=test --port=#{self.port} -d"
  system "rake db:test:purge"
  system "rake db:test:load"
  pid = File.read("tmp/pids/server.pid")

  self.files.each do |file|
    system "ruby test/zipper/#{file}.rb"
  end
  
  system "kill -s 9 " + pid
  
  File.open( "docs/zipper.json" , 'a' ) {|f| f.write( @docs.to_json ) }
end

.test(url = "", options = {}) ⇒ Object



48
49
50
51
52
53
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
92
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/zipper.rb', line 48

def test(url = "", options = {})

  url = url || ""

  # inferences
  _URI        = URI.parse(url)
  _URL        = _URI.host ? url : File.join(self.root, url)
  _URI        = _URI.host ? _URI : URI.parse(_URL)
  _resource   = _URI.path ? _URI.path.gsub(/(^\/|\/.*$)/i,"") : "resource"
  _action     = _URI.path ? _URI.path.gsub(/(^.*\/)/i,"") : "action on"
  _title      = _action.capitalize + " " + _resource.capitalize
  _key        = _title.downcase.gsub(/ /i, "-").downcase


  # defaults
  _options = {
    :resource   => _resource,
    :title      => _title,
    :key        => _key,
    :method     => :get,
    :data       => nil,
    :assertions => { :status => 200 },
    :username   => nil,
    :password   => nil,
    :use_ssl    => false
  }


  # merge passed in options with default options
  _options = _options.merge(options)
  
  # infer user
  if _options[:user]
    uid = _options[:user]
    user = (self.users)[uid]
    raise ":user does not exist" unless user
    _un = user[:username]
    _pw = user[:password]
  end
  
  # print test title
  p "TESTING: " + _options[:title]

  # make request
  _result = Pocket.make_request(_URL, {
    :method     => _options[:method],
    :username   => _un || _options[:username],
    :password   => _pw || _options[:password],
    :use_ssl    => _options[:use_ssl],
    :data       => _options[:data]
  })



  # test assertions
  _assertions = _options[:assertions]

  if _assertions[:status] && _result.code.to_i != _assertions[:status]
    self.fail "Status should have been #{_assertions[:status]} but was #{_result.code}"
  end


  
  if _assertions[:body]
    b = _assertions[:body]
    if b.is_a?(TrueClass)
      if _result.body.length < 1
        self.fail "Body should have contained content but didn't"
      end
    elsif b.is_a?(FalseClass)
      if _result.body.length > 0
        self.fail "Body should have been blank but wasn't"
      end
    elsif b != _result.body
      self.fail "Body should have been #{b} but wasn't"
    end
  end



  if _assertions[:attributes]
    
    if _result.body.length < 1
      self.fail "Attempted to check for an attribute but the response body was blank"
      return
    end
    
    attrs = _assertions[:attributes]
    object = JSON.parse _result.body
    attrs.each do |a|
      if !object[a.to_s]
        self.fail "The response object should have contained the attribute #{a} but didn't"
      end
    end
  end



  
  # create example
  _curl_string = []
  _curl_string << "curl"
  
  if _options[:username] && _options[:password]
    _curl_string << "#{_options[:username]}:#{_options[:password]}"
  end
  
  _curl_string << '-H "Accept: application/json" -H "Content-Type: application/json"'
  
  if _options[:data]
    _curl_string << "-d '#{_options[:data]}'"
  end
  
  if _options[:method] && _options[:method] != "GET"
    _curl_string << "-X $method"
  end
  
  _curl_string << "$docs_base_url/$api_url";
  _curl_string << "\n"
      
      
  # create doc json
  @docs ||= []
  @docs << {
    :resource => _options[:resource],
    :title => _options[:title],
    :key => _options[:key],
    :method => _options[:method],
    :url => _options[:url],
    :request_body => _options[:data],
    :status => _options[:status],
    :response_body => _result.to_json,
    :example => _curl_string.join(" ")
  }

  _result
  
end