Class: Crocodoc::FakeServer
- Inherits:
-
Object
- Object
- Crocodoc::FakeServer
- Defined in:
- lib/crocodoc/fake_server.rb
Instance Attribute Summary collapse
-
#docs ⇒ Object
Returns the value of attribute docs.
-
#ids ⇒ Object
Returns the value of attribute ids.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #document_delete(verb, params) ⇒ Object
- #document_status(verb, params) ⇒ Object
- #document_upload(verb, params) ⇒ Object
-
#initialize(opts) ⇒ FakeServer
constructor
A new instance of FakeServer.
- #method_missing(m, *args, &block) ⇒ Object
- #next_uuid ⇒ Object
- #request(req, body = nil) ⇒ Object
- #session_create(verb, params) ⇒ Object
Constructor Details
#initialize(opts) ⇒ FakeServer
Returns a new instance of FakeServer.
4 5 6 7 8 |
# File 'lib/crocodoc/fake_server.rb', line 4 def initialize(opts) @ids = 0 @docs = {} @token = opts[:token] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
142 143 144 |
# File 'lib/crocodoc/fake_server.rb', line 142 def method_missing(m, *args, &block) Crocodoc::FakeResponse.new("404", "") end |
Instance Attribute Details
#docs ⇒ Object
Returns the value of attribute docs.
3 4 5 |
# File 'lib/crocodoc/fake_server.rb', line 3 def docs @docs end |
#ids ⇒ Object
Returns the value of attribute ids.
3 4 5 |
# File 'lib/crocodoc/fake_server.rb', line 3 def ids @ids end |
#token ⇒ Object
Returns the value of attribute token.
3 4 5 |
# File 'lib/crocodoc/fake_server.rb', line 3 def token @token end |
Instance Method Details
#document_delete(verb, params) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/crocodoc/fake_server.rb', line 100 def document_delete(verb, params) # Failure cases unless verb == "POST" return Crocodoc::FakeResponse.new("405", "") end unless params['uuid'] && @docs[params['uuid']] return Crocodoc::FakeResponse.new("400", { :error => "invalid document uuid" }.to_json) end # Remove doc and succeed @docs.delete(params['uuid']) Crocodoc::FakeResponse.new("200", "true") end |
#document_status(verb, params) ⇒ Object
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 |
# File 'lib/crocodoc/fake_server.rb', line 70 def document_status(verb, params) # Failure cases unless verb == "GET" return Crocodoc::FakeResponse.new("405", "") end unless params['uuids'] return Crocodoc::FakeResponse.new("400", { :error => "missing parameter uuids" }.to_json) end # Build response and succeed uuids = params['uuids'].split(",") res = uuids.map do |uuid| if @docs[uuid] { :status => "DONE", :uuid => uuid, :viewable => true } else { :uuid => uuid, :error => "invalid uuid" } end end Crocodoc::FakeResponse.new("200", res.to_json) end |
#document_upload(verb, params) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/crocodoc/fake_server.rb', line 51 def document_upload(verb, params) # Failure cases unless verb == "POST" return Crocodoc::FakeResponse.new("405", "") end unless params['url'] || params['file'] return Crocodoc::FakeResponse.new("400", { :error => "no url or file specified" }.to_json) end # Store doc and succeed uuid = next_uuid @docs[uuid] = params['url'] Crocodoc::FakeResponse.new("200", { :uuid => uuid }.to_json) end |
#next_uuid ⇒ Object
10 11 12 |
# File 'lib/crocodoc/fake_server.rb', line 10 def next_uuid (@ids += 1).to_s end |
#request(req, body = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/crocodoc/fake_server.rb', line 14 def request(req, body=nil) # normalize path and params case req.method when "GET" path, params = req.path.split("?") when "POST" path = req.path params = req.body end # verify correct api version unless path.start_with? "/api/v2/" return Crocodoc::FakeResponse.new("404", "") end path.gsub!("/api/v2/", "") # parse params params = params.split("&") params = params.inject({}) do |hsh, p| k,v = p.split("=") hsh[k.to_s] = CGI::unescape(v) hsh end # verify token unless params['token'] == @token return Crocodoc::FakeResponse.new("401", { :error => "invalid API token" }.to_json) end # dispatch self.send(path.gsub("/", "_"), req.method, params) rescue => exception return Crocodoc::FakeResponse.new("500", exception.) end |
#session_create(verb, params) ⇒ Object
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 |
# File 'lib/crocodoc/fake_server.rb', line 116 def session_create(verb, params) # Failure cases unless verb == "POST" return Crocodoc::FakeResponse.new("405", "") end unless params['uuid'] && @docs[params['uuid']] return Crocodoc::FakeResponse.new("400", { :error => "invalid uuid" }.to_json) end if params['editable'] == 'true' && params['user'] user = params['user'].split(",") if user.length != 2 || user.first != user.first.to_i.to_s || user.first.to_i >= (2**31) return Crocodoc::FakeResponse.new("400", { :error => "invalid user ID" }.to_json) end end # Generate session uuid and succeed uuid = next_uuid Crocodoc::FakeResponse.new("200", { :session => uuid }.to_json) end |