frenchy 0.5.1 → 0.6.3

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
- SHA1:
3
- metadata.gz: d2619d769fe19f6a125107e3473f36a77ce85e5d
4
- data.tar.gz: 17f15bcd90080806dd6b944bb1fceeeb9849cac7
2
+ SHA256:
3
+ metadata.gz: 90c9e971bf0cee2367ca74b3c2f2342da1a5740acf2473d266fa97a574bbcb69
4
+ data.tar.gz: 8633892fd38e341447124b068f70ca11069ad4e684092694f61d184f87981fc1
5
5
  SHA512:
6
- metadata.gz: 42cceb0cb279b6b8b5c96a603d8ead6a6c2b1c268d0b2171a86f399648e23c6977c6451054cebf0ebd7b94e5155a4751ea286099879d71c3e932035baa6718bc
7
- data.tar.gz: d5decd2797fb0b2f29f6d3e6afe1bbe6d1227f71774bf06dd403bfd615806a47b251f12d27435d737eb3291afb88da811968bfd23117f5952a8a3ef999f68297
6
+ metadata.gz: 7c5999ea947402f8f148bbea41536a6103826591aafa00e197e227b9dba11bc98118a3fe30cc0c0ee507b9613a2c51c926eba149f250e7748bfe9ae9e101009f
7
+ data.tar.gz: c68812567016ed5cd9feb96280cd10bcdb27c32ad279a643742557e204dd3f1936a7cf6a98304484a9a7dbc3eac59de1ced4a04361afab11532991ef1acb83ce
data/.travis.yml CHANGED
@@ -1,3 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3.0
3
+ - 2.3.4
4
+ - 2.4.1
5
+ - 2.5.9
6
+ - 2.7.3
7
+ - 3.0.1
data/frenchy.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency "json"
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "bundler"
24
24
  spec.add_development_dependency "guard-rspec"
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "rspec"
data/lib/frenchy.rb CHANGED
@@ -14,6 +14,8 @@ require "frenchy/version"
14
14
  module Frenchy
15
15
  class_eval do
16
16
  @services = {}
17
+ @content_types = {}
18
+ @content_type_accept = ""
17
19
  end
18
20
 
19
21
  def self.register_service(name, options={})
@@ -23,4 +25,19 @@ module Frenchy
23
25
  def self.find_service(name)
24
26
  @services[name.to_s] || raise(Frenchy::Error, "No service '#{name}' registered")
25
27
  end
28
+
29
+ def self.register_content_type(name, &block)
30
+ @content_types[name] = block
31
+ @content_type_accept = @content_types.keys.join(", ")
32
+ end
33
+
34
+ def self.find_content_type_handler(name)
35
+ @content_types[name] || raise(Frenchy::Error, "No content type '#{name}' registered")
36
+ end
37
+
38
+ def self.accept_header
39
+ @content_type_accept
40
+ end
26
41
  end
42
+
43
+ Frenchy.register_content_type("application/json") {|x| JSON.parse(x) }
@@ -1,5 +1,6 @@
1
1
  require "net/http"
2
2
  require "json"
3
+ require "uri"
3
4
 
4
5
  module Frenchy
5
6
  class Client
@@ -46,16 +47,13 @@ module Frenchy
46
47
  private
47
48
 
48
49
  def perform(method, path, params)
49
- uri = URI(@host)
50
+ uri = URI(@host + path)
50
51
  body = nil
51
52
  headers = {
52
53
  "User-Agent" => "Frenchy/#{Frenchy::VERSION}",
53
- "Accept" => "application/json",
54
+ "Accept" => Frenchy.accept_header,
54
55
  }.merge(@headers)
55
56
 
56
- # Set the URI path
57
- uri.path = path
58
-
59
57
  # Set request parameters
60
58
  if params.any?
61
59
  case method
@@ -102,7 +100,7 @@ module Frenchy
102
100
  when 200...399
103
101
  # Positive responses are expected to return JSON
104
102
  begin
105
- JSON.parse(resp.body)
103
+ decode_response(resp)
106
104
  rescue => ex
107
105
  raise Frenchy::InvalidResponse.new(ex, reqinfo, resp)
108
106
  end
@@ -124,5 +122,9 @@ module Frenchy
124
122
  def perform_request(http, req)
125
123
  http.request(req)
126
124
  end
125
+
126
+ def decode_response(resp)
127
+ Frenchy.find_content_type_handler(resp["Content-Type"]).call(resp.body)
128
+ end
127
129
  end
128
130
  end
data/lib/frenchy/model.rb CHANGED
@@ -145,7 +145,7 @@ module Frenchy
145
145
  # Convert value to a Time or DateTime. Numbers are treated as unix timestamps,
146
146
  # other values are parsed with DateTime.parse.
147
147
  define_method("#{name}=") do |v|
148
- if v.is_a?(Fixnum)
148
+ if v.is_a?(Integer)
149
149
  set(name, Time.at(v).to_datetime)
150
150
  elsif v.is_a?(DateTime)
151
151
  set(name, v)
@@ -19,7 +19,7 @@ module Frenchy
19
19
  end
20
20
 
21
21
  params.delete(k)
22
- path.sub!(pat, v)
22
+ path.sub!(pat, CGI.escape(v))
23
23
  end
24
24
 
25
25
  @service = service
@@ -7,7 +7,7 @@ module Frenchy
7
7
  module ClassMethods
8
8
  # Find record(s) using the default endpoint and flexible input
9
9
  def find(params={})
10
- params = {"id" => params.to_s} if [Fixnum, String].any? {|c| params.is_a? c }
10
+ params = {"id" => params.to_s} if [Integer, String].any? {|c| params.is_a? c }
11
11
  find_with_endpoint("default", params)
12
12
  end
13
13
 
@@ -1,3 +1,3 @@
1
1
  module Frenchy
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.3"
3
3
  end
@@ -250,7 +250,7 @@ describe Frenchy::Model do
250
250
  expect(v.other_priority).to eql(Box::Priority::NORMAL)
251
251
  end
252
252
 
253
- it "accepts fixnums" do
253
+ it "accepts integers" do
254
254
  v = Box.new(priority: 2)
255
255
  expect(v.priority).to eql(Box::Priority::EXPRESS)
256
256
  expect(v.priority.to_i).to eql(2)
@@ -307,4 +307,4 @@ describe Frenchy::Model do
307
307
  end
308
308
  end
309
309
  end
310
- end
310
+ end
@@ -7,6 +7,12 @@ describe Frenchy::Request do
7
7
  expect(request.path).to eql("/v1/users/1234/md5something")
8
8
  end
9
9
 
10
+ it "escapes path parameters but not query params" do
11
+ request = Frenchy::Request.new("service", "get", "/v1/users/:slug/:fullname", {"slug" => "big dog 20", "fullname" => "How Big Is He?", "division" => "How Big Is He, Really?"}, {})
12
+ expect(request.path).to eql("/v1/users/big+dog+20/How+Big+Is+He%3F")
13
+ expect(request.params).to eql({"division" => "How Big Is He, Really?"})
14
+ end
15
+
10
16
  it "retains remaining parameters as query parameters" do
11
17
  request = Frenchy::Request.new("service", "get", "/v1/users/:id", {"id" => 1234, "token" => "md5something"}, {})
12
18
  expect(request.path).to eql("/v1/users/1234")
@@ -15,4 +15,16 @@ describe Frenchy do
15
15
  expect{Frenchy.find_service("nonexistent")}.to raise_error(Frenchy::Error)
16
16
  end
17
17
  end
18
- end
18
+
19
+ describe ".register_content_type" do
20
+ it "adds the content type to the accept header" do
21
+ expect(Frenchy.accept_header).to eql("application/json")
22
+ Frenchy.register_content_type("application/other") do |x|
23
+ 5
24
+ end
25
+ expect(Frenchy.accept_header).to eql("application/json, application/other")
26
+ expect(Frenchy.find_content_type_handler("application/other").call(nil)).to eql(5)
27
+ expect{Frenchy.find_content_type_handler("nonexistent")}.to raise_error(Frenchy::Error)
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frenchy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Coene
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-28 00:00:00.000000000 Z
11
+ date: 2021-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.3'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.3'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: guard-rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -165,7 +165,7 @@ homepage: https://github.com/jcoene/frenchy
165
165
  licenses:
166
166
  - MIT
167
167
  metadata: {}
168
- post_install_message:
168
+ post_install_message:
169
169
  rdoc_options: []
170
170
  require_paths:
171
171
  - lib
@@ -180,9 +180,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
180
  - !ruby/object:Gem::Version
181
181
  version: '0'
182
182
  requirements: []
183
- rubyforge_project:
184
- rubygems_version: 2.5.2
185
- signing_key:
183
+ rubygems_version: 3.2.3
184
+ signing_key:
186
185
  specification_version: 4
187
186
  summary: Opinionated JSON API modeling framework for Ruby.
188
187
  test_files: