navigable-server 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44a577b55032e77007e967d1468b58f6acec275bcaa20ec03dd4643abf8184b5
4
- data.tar.gz: 98bc33d54dc4811f4d16989ff3021b3f06dc3c1e4f641575b394b88c5dfc448b
3
+ metadata.gz: 126d577099fc83bd854908258e60fb7e66f50bd2c6756d17d43141662a1a9270
4
+ data.tar.gz: 9cd7e23c64365f991eb4a89d348b8585a66039f63a0ac1719bf3b01fdf4c77a8
5
5
  SHA512:
6
- metadata.gz: a1824a664888c326e0ecce71c8665a57a4263753008acd98df91992c4e2d7908294fb127c42da5f4d22746b9cc91af1423f5b2ae375bc680e60d70e6a7952214
7
- data.tar.gz: c7b463b914a0ed25c67f3158f857767d5d8125b0fcad5dbf32b61362833bd220c65aecbc04ede52f3d180ed3e95f2d65cf2aec6c7d45599804a993869068f8bd
6
+ metadata.gz: 778df9f366e96e7a609b995d2271200e5371dd30c5c2fabb7f18bdda82463bcd880954ea9cd9a96ee0d9e0a84b17f11ad18bfb13ece8d92b81d069034847a058
7
+ data.tar.gz: 289bad94b8b4a0bb72cec07a40ad87870b6fc03fb6cbc0a3fe39bc50aae311c584e82c975a80baf8704934c138372a74b70e5418a12489b1833ba21efaebd5f6
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2020 Alan Ridlehoover
3
+ Copyright (c) 2020 Alan Ridlehoover and Fito von Zastrow
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Navigable::Server
2
2
 
3
- [![Build Status](https://travis-ci.org/first-try-software/navigable-server.svg?branch=main)](https://travis-ci.org/first-try-software/navigable-server) [![Maintainability](https://api.codeclimate.com/v1/badges/e62e187bb05abe883169/maintainability)](https://codeclimate.com/github/first-try-software/navigable-server/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/e62e187bb05abe883169/test_coverage)](https://codeclimate.com/github/first-try-software/navigable-server/test_coverage)
3
+ [![Gem Version](https://badge.fury.io/rb/navigable-server.svg)](https://badge.fury.io/rb/navigable-server) [![Build Status](https://travis-ci.org/first-try-software/navigable-server.svg?branch=main)](https://travis-ci.org/first-try-software/navigable-server) [![Maintainability](https://api.codeclimate.com/v1/badges/e62e187bb05abe883169/maintainability)](https://codeclimate.com/github/first-try-software/navigable-server/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/e62e187bb05abe883169/test_coverage)](https://codeclimate.com/github/first-try-software/navigable-server/test_coverage)
4
4
 
5
5
  Navigable is a family of gems that together provide all the tools you need to build fast, testable, and reliable JSON and/or GraphQL based APIs with isolated, composable business logic. The gems include:
6
6
 
@@ -72,7 +72,68 @@ Or install it yourself as:
72
72
 
73
73
  ## Usage
74
74
 
75
- TODO: Write usage instructions here
75
+ `Navigable::Server` is a different kind of server. Rather than registering routes in a route file, `Endpoint` classes register themselves at startup using the `responds_to` method. When the route is requested, `Navigable::Server` injects the `Request` into the `Endpoint` and calls the `execute` method.
76
+
77
+ ```ruby
78
+ class AhoyEndpoint
79
+ extend Navigable::Server::Endpoint
80
+
81
+ responds_to :get, '/ahoy'
82
+
83
+ def execute
84
+ { status: 200, html: '<h1>Ahoy! Welcome aboard Navigable!</h1>' }
85
+ end
86
+ end
87
+ ```
88
+ The `execute` method should return a hash containing:
89
+
90
+ * Some content (either `json`, `html`, `text`, or `body`)
91
+ * An optional status code (default = 200)
92
+ * Optional headers (`Navigable::Server` sets `Content-Type` and `Content-Length` for you if they can be inferred)
93
+
94
+ Here are three examples:
95
+
96
+ ```ruby
97
+ # returning successful creation of SomeActiveRecordModel
98
+ class CreateSomeActiveRecordModelEndpoint
99
+ extend Navigable::Server::Endpoint
100
+
101
+ responds_to :post, '/models'
102
+
103
+ def execute
104
+ model = SomeActiveRecordModel.create(request.params)
105
+ { status: 201, json: model }
106
+ end
107
+ end
108
+
109
+ # Returning 404 Not Found
110
+ class ShowSomeActiveRecordModelEndpoint
111
+ extend Navigable::Server::Endpoint
112
+
113
+ responds_to :get, '/models/:id'
114
+
115
+ def execute
116
+ model = SomeActiveRecordModel.find_by(id: request.params[:id])
117
+
118
+ return { status: 404, text: 'Not Found' } unless model
119
+
120
+ { json: model }
121
+ end
122
+ end
123
+
124
+ # Returning an image
125
+ class ShowTreasureMapEndpoint
126
+ extend Navigable::Server::Endpoint
127
+
128
+ responds_to :get, '/treasure-map'
129
+
130
+ def execute
131
+ treasure_map = read_file('x_marks_the_spot.png')
132
+ { headers: { 'Content-Type' => 'image/png' }, body: treasure_map }
133
+ end
134
+ end
135
+ ```
136
+ If you are considering creating a JSON API with `Navigable::Server`, you should know about `Navigable::API` and `Navigable::GraphQL`. Both of these gems extend `Navigable::Server` in ways that bring all of Navigable together from `Commands` and `Observers`, to `Endpoints` and `Resolvers`.
76
137
 
77
138
  ## Development
78
139
 
@@ -4,6 +4,7 @@ module Navigable
4
4
  module Server
5
5
  class Response
6
6
  CONTENT_TYPE = 'Content-Type'
7
+ CONTENT_LENGTH = 'Content-Length'
7
8
  MIME_TYPE_JSON = 'application/json'
8
9
  MIME_TYPE_HTML = 'text/html'
9
10
  MIME_TYPE_TEXT = 'text/plain'
@@ -17,14 +18,16 @@ module Navigable
17
18
  end
18
19
 
19
20
  def to_rack_response
20
- [status, headers, content]
21
+ [status, headers, [content]]
21
22
  end
22
23
 
23
24
  private
24
25
 
25
26
  def headers
26
- headers = params[:headers] || {}
27
+ headers = {}
27
28
  headers[CONTENT_TYPE] = content_type if content_type
29
+ headers[CONTENT_LENGTH] = content_length
30
+ headers.merge!(params[:headers]) if params[:headers]
28
31
  headers
29
32
  end
30
33
 
@@ -34,8 +37,12 @@ module Navigable
34
37
  return MIME_TYPE_TEXT if text
35
38
  end
36
39
 
40
+ def content_length
41
+ content.bytesize
42
+ end
43
+
37
44
  def content
38
- [json || html || text || body || EMPTY_CONTENT]
45
+ json || html || text || body || EMPTY_CONTENT
39
46
  end
40
47
 
41
48
  def json
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Navigable
4
4
  module Server
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: navigable-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Ridlehoover
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-09-25 00:00:00.000000000 Z
12
+ date: 2020-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: navigable-router