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.



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

def files
  @files
end

.portObject

Returns the value of attribute port.



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

def port
  @port
end

.rootObject

Returns the value of attribute root.



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

def root
  @root
end

.usersObject

Returns the value of attribute users.



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

def users
  @users
end

Class Method Details

.runObject



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

def run
  system "rails s --environment=test --port=#{self.instance.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
end

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



46
47
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
# File 'lib/zipper.rb', line 46

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


  # 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,
    :status     => 200,
    :username   => nil,
    :password   => nil,
    :use_ssl    => false
  }


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


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






  # 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
  # doc = {
  #   :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(" ")
  # }
  # 
  # # write to docs file
  # file_path = Rails.root.join("docs/zipper_api.json")
  # current_json_string = File.open( file_path , "rb" ).read
  # 
  # docs_json = []
  # begin
  #   docs_json = JSON.parse current_json_string
  # rescue
  #   docs_json = []
  # end
  # 
  # docs_json << doc_json
  # File.open( file_path , 'a' ) {|f| f.write( docs_json ) }

  _result
  
end