flexirest 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc944642eeec80748c437111eb7e7b5b5ee98d40
4
- data.tar.gz: f931c97b2a77159732a14e78e69f125ef006cd69
3
+ metadata.gz: 6710bc9498f0361be184d117313246aba2f8bba6
4
+ data.tar.gz: fee1ab9d2d860e40b71c0f612fc590d2e381704b
5
5
  SHA512:
6
- metadata.gz: 72e1f0677d501933a37699452787dc2f7a43ee7919c94e362b6daf6f7eb1b5fd0fe65c5c7cd8254d90a1c9c2027ddbd4fa26151205f2976de7074421e7b9d1b4
7
- data.tar.gz: 86f2aa14d43f8d16a3bdc69824e443eedb30ee6e645c402898d0dab9e0d41514f0ac26ae30acabecc90d599507fb9f19f85db0b2681d9be90b4fee3f5c300964
6
+ metadata.gz: d885fb9c26907c53037ac373922233ab1bba00dcf3c3f36e227aaae412fdbf47e201bbc07f11715bf794a603618600c98ee48bc3074a9ca040d3b5e5bca773c5
7
+ data.tar.gz: 0d8936b2fd3ffc16d0eaff6ff32ad7ba360fa5a933be80a5a70a06382d9927d5aca471075061111de67554b22f181f802d649085c485acb836c9033847b3dc23
data/.travis.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.3.0
3
4
  - 2.2.0
4
5
  - 2.1.0
5
6
  - 2.0.0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.3
4
+
5
+ Features:
6
+
7
+ - New Ruby on Rails integration guide (thanks to Matthias Neumayr)
8
+ - New `ignore_root` method to ignore JSON or XML root nodes, deprecated `ignore_xml_root` (thanks to dsarhadian for the request)
9
+
3
10
  ## 1.3.2
4
11
 
5
12
  Features:
@@ -10,7 +17,7 @@ Features:
10
17
 
11
18
  Features:
12
19
 
13
- - You can now use has_many/has_one on the class live, more like ActiveRecord
20
+ - You can now use `has_many`/`has_one` on the class live, more like ActiveRecord
14
21
 
15
22
  ## 1.3.0
16
23
 
@@ -28,7 +35,7 @@ Features:
28
35
 
29
36
  Features:
30
37
 
31
- - Allow nil option in validators (thanks to Jurgen Jocubeit)
38
+ - Allow `nil` option in validators (thanks to Jurgen Jocubeit)
32
39
  - Added array feature for returning simple scalar values (thanks to Jurgen Jocubeit)
33
40
 
34
41
  ## 1.2.17
@@ -41,7 +48,7 @@ Bugfixes:
41
48
 
42
49
  Feature:
43
50
 
44
- - Replaces URL :keyed parameters for direct requests.
51
+ - Replaces URL `:keyed` parameters for direct requests.
45
52
 
46
53
  ## 1.2.15
47
54
 
@@ -55,8 +62,6 @@ Bugfixes:
55
62
 
56
63
  - Patch was partially implemented in 2014, but never completed. It should be working now (thanks to Joel Low)
57
64
 
58
- # Changelog
59
-
60
65
  ## 1.2.13
61
66
 
62
67
  Bugfixes:
data/README.md CHANGED
@@ -31,7 +31,7 @@ First you need to create your new model class:
31
31
 
32
32
  ```ruby
33
33
  # config/environments/production.rb
34
- MyApp::Application.configure do
34
+ Rails.application.configure do
35
35
  # ...
36
36
  config.api_server_url = "https://www.example.com/api/v1"
37
37
  end
@@ -123,6 +123,10 @@ If you want to debug the response, using inspect on the response object may well
123
123
  puts @person.to_json
124
124
  ```
125
125
 
126
+ ### Ruby on Rails Integration
127
+
128
+ A detailed guide, how to integrate Flexirest with a RESTful resources can be found in the [Ruby-on-Rails-Integration.md](https://github.com/andyjeffries/flexirest/blob/master/Ruby-on-Rails-Integration.md).
129
+
126
130
  ## Advanced Features
127
131
 
128
132
  ### Faraday Configuration
@@ -786,6 +790,16 @@ class Person < Flexirest::Base
786
790
  end
787
791
  ```
788
792
 
793
+ ### Root element removal
794
+
795
+ If your JSON or XML object comes back with a root node and you'd like to ignore it, you can define the mapping as:
796
+
797
+ ```ruby
798
+ class Feed < Flexirest::Base
799
+ get :list, "/feed", ignore_root: "feed"
800
+ end
801
+ ```
802
+
789
803
  ### Required Parameters
790
804
 
791
805
  If you want to specify that certain parameters are required for a specific call, you can specify them like:
@@ -917,9 +931,7 @@ class Article < Flexirest::Base
917
931
  end
918
932
  ```
919
933
 
920
- ## Beta Features
921
-
922
- ### XML Responses
934
+ ## XML Responses
923
935
 
924
936
  Flexirest uses Crack to allow parsing of XML responses. For example, given an XML response of (with a content type of `application/xml` or `text/xml`):
925
937
 
@@ -971,14 +983,6 @@ puts @atom.feed.link.href
971
983
  end
972
984
  ```
973
985
 
974
- If your XML object comes back with a root node and you'd like to ignore it, you can define the mapping as:
975
-
976
- ```ruby
977
- class Feed < Flexirest::Base
978
- get :atom, "/atom", ignore_xml_root: "feed"
979
- end
980
- ```
981
-
982
986
  For testing purposes, if you are using a `fake` content response when defining your endpoint, you should also provide `fake_content_type: "application/xml"` so that the parser knows to use XML parsing.
983
987
 
984
988
  ## Contributing
@@ -0,0 +1,142 @@
1
+ # Ruby on Rails Integration
2
+
3
+ Flexirest works fine with Ruby on Rails Framework. This guide was tested with a Rails 4.2.x Application.
4
+
5
+
6
+ ## Integration
7
+
8
+ Start referencing `flexirest` in your `Gemfile`
9
+
10
+ ```ruby
11
+ # Gemfile
12
+ gem 'flexirest'
13
+ ```
14
+
15
+
16
+ ## Configuration
17
+
18
+ It's possible to explicit specify the `base_url` in the Model Class. If you have an common API Endpoint it makes sense to setup an initializer in `config/initializer` or use the `Rails.application.configure` namespace.
19
+
20
+ This example use a custom file in `config/initializer` to setup the API endpoint. Either set a fixed URL or use environment variables if you would like to follow the [12factor](http://12factor.net/config) rules for preparing your application running on cloud infrastructure like heroku.
21
+
22
+ ```ruby
23
+ # config/initializer/flexirest.rb
24
+ Flexirest::Base.base_url = ENV.fetch("API_ENDPOINT_URL")
25
+ ```
26
+
27
+
28
+ ## Routes
29
+
30
+ Like every other RESTful resource, just add the controller as a reference into your routes.
31
+
32
+ ```ruby
33
+ # config/routes.rb
34
+
35
+ Rails.application.routes.draw do
36
+ # ...
37
+ resources :people
38
+ end
39
+ ```
40
+
41
+
42
+ ## Model
43
+
44
+ The `ActiveModel` shortcuts will add support for `form_for` helper and `@person.errors` functionally in your views.
45
+ For example, if you have a scaffolded view structure this will just work out of the box. Read more about `ActiveModel` here:
46
+
47
+ * [ActiveModel::Naming](http://api.rubyonrails.org/classes/ActiveModel/Naming.html)
48
+ * [ActiveModel::Conversion](http://api.rubyonrails.org/classes/ActiveModel/Conversion.html)
49
+ * [ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations.html)
50
+
51
+ In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions.
52
+ Add the GET, POST, PATCH and DELETE methods that reflect to your endpoint.
53
+
54
+ Add the `persisted?` method to your Class to support Rails named_routes, so you could use `edit_person_path(person)` without explicit pass the `person.id`.
55
+
56
+
57
+ ```ruby
58
+ # app/models/person.rb
59
+ class Person < Flexirest::Base
60
+
61
+ extend ActiveModel::Naming
62
+ include ActiveModel::Conversion
63
+ include ActiveModel::Validations
64
+
65
+ get :all, "/people"
66
+ get :find, "/people/:id"
67
+ patch :update, "/people/:id"
68
+ post :save, "/people"
69
+ delete :destroy, "/people/:id"
70
+
71
+ def persisted?
72
+ id.present?
73
+ end
74
+
75
+ end
76
+ ```
77
+
78
+
79
+ ## Controller
80
+
81
+ The Controller is structured like an standard RESTful Rails Controller. Only the `update` method has
82
+ a small change about how params getting processed.
83
+
84
+ Flexirest requires the `id` inside the params hash, this is not included by default.
85
+ Easily merge the current id into the params with `person_params.merge(id: @person.id)`
86
+
87
+ No other changes had to be made for the controller.
88
+
89
+ ```ruby
90
+ # app/controller/people_controller.rb
91
+ class PeopleController < ApplicationController
92
+ before_action :set_person, only: [:show, :edit, :update, :destroy]
93
+
94
+ def index
95
+ @people = Person.all
96
+ end
97
+
98
+ def show
99
+ end
100
+
101
+ def new
102
+ @person = Person.new
103
+ end
104
+
105
+ def edit
106
+ end
107
+
108
+ def create
109
+ @person = Person.new(person_params)
110
+
111
+ if @person.save
112
+ redirect_to @person, notice: 'Person was successfully created.'
113
+ else
114
+ render :new
115
+ end
116
+ end
117
+
118
+ def update
119
+ if @person.update(person_params.merge(id: @person.id))
120
+ redirect_to @person, notice: 'Person was successfully updated.'
121
+ else
122
+ render :edit
123
+ end
124
+ end
125
+
126
+ def destroy
127
+ @person.destroy
128
+ redirect_to people_url, notice: 'Person was successfully destroyed.'
129
+ end
130
+
131
+ private
132
+ # Use callbacks to share common setup or constraints between actions.
133
+ def set_person
134
+ @person = Person.find(params[:id])
135
+ end
136
+
137
+ # Never trust parameters from the scary internet, only allow the white list through.
138
+ def person_params
139
+ params.require(:person).permit(:title, :first_name, :last_name, :email)
140
+ end
141
+ end
142
+ ```
@@ -390,7 +390,8 @@ module Flexirest
390
390
  else
391
391
  Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Response received #{@response.body.size} bytes"
392
392
  end
393
- result = generate_new_object(ignore_xml_root: @method[:options][:ignore_xml_root])
393
+ result = generate_new_object(ignore_root: @method[:options][:ignore_root], ignore_xml_root: @method[:options][:ignore_xml_root])
394
+ # TODO: Cleanup when ignore_xml_root is removed
394
395
  else
395
396
  raise ResponseParseException.new(status:status, body:@response.body)
396
397
  end
@@ -540,9 +541,15 @@ module Flexirest
540
541
  body = @response.body
541
542
  elsif is_json_response?
542
543
  body = @response.body.blank? ? {} : MultiJson.load(@response.body)
544
+ if options[:ignore_root]
545
+ body = body[options[:ignore_root].to_s]
546
+ end
543
547
  elsif is_xml_response?
544
548
  body = @response.body.blank? ? {} : Crack::XML.parse(@response.body)
545
- if options[:ignore_xml_root]
549
+ if options[:ignore_root]
550
+ body = body[options[:ignore_root].to_s]
551
+ elsif options[:ignore_xml_root]
552
+ Flexirest::Logger.warn("Using `ignore_xml_root` is deprecated, please switch to `ignore_root`")
546
553
  body = body[options[:ignore_xml_root].to_s]
547
554
  end
548
555
  end
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.3.2"
2
+ VERSION = "1.3.3"
3
3
  end
@@ -81,6 +81,16 @@ describe Flexirest::Request do
81
81
  post :save, "/save"
82
82
  end
83
83
 
84
+ class IgnoredRootExampleClient < ExampleClient
85
+ get :root, "/root", ignore_root: "feed", fake: %Q{
86
+ {
87
+ "feed": {
88
+ "title": "Example Feed"
89
+ }
90
+ }
91
+ }
92
+ end
93
+
84
94
  allow_any_instance_of(Flexirest::Request).to receive(:read_cached_response)
85
95
  end
86
96
 
@@ -758,4 +768,8 @@ describe Flexirest::Request do
758
768
  expect(object._delegate?).to be_truthy
759
769
  end
760
770
  end
771
+
772
+ it "should ignore a specified root element" do
773
+ expect(IgnoredRootExampleClient.root.title).to eq("Example Feed")
774
+ end
761
775
  end
data/spec/lib/xml_spec.rb CHANGED
@@ -2,7 +2,13 @@ require 'spec_helper'
2
2
 
3
3
  class XmlResponseExample < Flexirest::Base
4
4
  base_url "http://www.example.com/v1/"
5
- get :root, "/root", ignore_xml_root: "feed", fake_content_type: "application/xml", fake: %Q{
5
+ get :root, "/root", ignore_root: "feed", fake_content_type: "application/xml", fake: %Q{
6
+ <?xml version="1.0" encoding="utf-8"?>
7
+ <feed xmlns="http://www.w3.org/2005/Atom">
8
+ <title>Example Feed</title>
9
+ </feed>
10
+ }
11
+ get :root_deprecated, "/root", ignore_xml_root: "feed", fake_content_type: "application/xml", fake: %Q{
6
12
  <?xml version="1.0" encoding="utf-8"?>
7
13
  <feed xmlns="http://www.w3.org/2005/Atom">
8
14
  <title>Example Feed</title>
@@ -71,4 +77,10 @@ describe XmlResponseExample do
71
77
  @feed = XmlResponseExample.root
72
78
  expect(@feed.title).to eq("Example Feed")
73
79
  end
80
+
81
+ it "allows ignoring of the XML root node using the deprecated call" do
82
+ expect(Flexirest::Logger).to receive(:warn).with("Using `ignore_xml_root` is deprecated, please switch to `ignore_root`")
83
+ @feed = XmlResponseExample.root_deprecated
84
+ expect(@feed.title).to eq("Example Feed")
85
+ end
74
86
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexirest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-26 00:00:00.000000000 Z
11
+ date: 2016-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -253,6 +253,7 @@ files:
253
253
  - Migrating-from-ActiveRestClient.md
254
254
  - README.md
255
255
  - Rakefile
256
+ - Ruby-on-Rails-Integration.md
256
257
  - doc/Flexirest Internals.graffle
257
258
  - doc/Flexirest Internals.png
258
259
  - flexirest.gemspec