flexirest 1.6.3 → 1.6.4

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
  SHA1:
3
- metadata.gz: 395be9d32a66854a03dfee23f11da2773fce4852
4
- data.tar.gz: 624db41375f5919e5495eef37d1bb5a2362e3f7e
3
+ metadata.gz: 59c109cfab49994ec4c27fb6b65745b504f717d3
4
+ data.tar.gz: 73a93865b5fb49e59a3e080a9a5679e674acf57f
5
5
  SHA512:
6
- metadata.gz: a6721c8c74d486de9101c54f07909c479416cc49e6838738323c804ea20d82e42f2148dbc218c3ca90d3632b9ebe60bab1cf519a27c1789c6976cb0fa45993d3
7
- data.tar.gz: b61cb95703f902fd1bdd26ef2c47fa67a230ff496d39ab30b111b5e9c65546c7f371af8f6c1d0fabd6e8fd584771b200c3b863cb1aeddc7f61e380fe14c290fc
6
+ metadata.gz: 94809b3ca647b8b60bb155eff4f3defe4b824129a2ba0c3d1525fe2d357799a1ac1f5795e7820112119c36ca702d2b05959f574d14e46a321ac0eba5745049ca
7
+ data.tar.gz: 776efc1bf75d74c44e197dfef3d8222bb911f57f97e205700b87a847b7191ecd4597b6dbedbffbfac4fec7d932df376b7bb59f5d2634b23175cbc0bf835f4e1b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.6.4
4
+
5
+ Feature:
6
+
7
+ - Added the ability to automatically change attributes returned from an API like `SomeName` or `someName` to Ruby-style `some_name` by setting `:rubify_names` when mapping an API call.
8
+
3
9
  ## 1.6.3
4
10
 
5
11
  Bugfix:
data/CONTRIBUTING.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # Flexirest (ARC) Contributing Guide
2
2
 
3
- ## Introduction
3
+ ## Introduction
4
4
 
5
5
  This project was built at Which? Ltd in the UK as ActiveRestClient, but was released as open source in 2014 under the MIT Licence. This is Andy Jeffries' fork of the project as the original seems not to be maintained by Which? and there's been no communication back about the project.
6
6
 
7
7
  We're happy to receive contributions from the community for features and bugfixes and hopefully this guide helps new developers to the project to understand how to get started with the internals of Flexirest.
8
8
 
9
- ## Overview
9
+ ## Overview
10
10
 
11
- ![Component Overview Diagram](https://raw.githubusercontent.com/andyjeffries/flexirest/master/doc/Flexirest%20Internals.png)
11
+ ![Component Overview Diagram](https://raw.githubusercontent.com/andyjeffries/flexirest/master/docs/Flexirest%20Internals.png)
12
12
 
13
13
  ## Components
14
14
 
data/README.md CHANGED
@@ -900,7 +900,19 @@ NB: Updating relationships is not yet supported.
900
900
 
901
901
  ### Proxying APIs
902
902
 
903
- Sometimes you may be working with an old API that returns JSON in a less than ideal format or the URL or parameters required have changed. In this case you can define a descendent of `Flexirest::ProxyBase`, pass it to your model as the proxy and have it rework URLs/parameters on the way out and the response on the way back in (already converted to a Ruby hash/array). By default any non-proxied URLs are just passed through to the underlying connection layer. For example:
903
+ Sometimes you may be working with an old API that returns JSON in a less than ideal format or the URL or parameters required have changed.
904
+
905
+ If it's simply that you want attribute names like `SomeName` or `someName` to be more Ruby-style `some_name` then you can simply do that by setting `:rubify_names` when mapping an API call.
906
+
907
+ ```ruby
908
+ class Article < Flexirest::Base
909
+ base_url "http://www.example.com"
910
+
911
+ get :all, "/all", rubify_names: true
912
+ end
913
+ ```
914
+
915
+ In more complex cases you can define a descendent of `Flexirest::ProxyBase`, pass it to your model as the proxy and have it rework URLs/parameters on the way out and the response on the way back in (already converted to a Ruby hash/array). By default any non-proxied URLs are just passed through to the underlying connection layer. For example:
904
916
 
905
917
  ```ruby
906
918
  class ArticleProxy < Flexirest::ProxyBase
File without changes
File without changes
@@ -0,0 +1,26 @@
1
+ ### Expected behaviour
2
+
3
+ When I use the (something) method, I get this exception xyz...
4
+
5
+ ### Desired behaviour
6
+
7
+ It should call the remote server correctly and not raise an exception
8
+
9
+ ### How to reproduce
10
+
11
+ 1. Clone the Flexirest repo (`git clone https://github.com/flexirest/flexirest.git`)
12
+ 2. Change to the flexirest folder (`cd flexirest`)
13
+ 3. Install any dependencies (`bundle install`)
14
+ 4. Run an interactive console (`rake c`)
15
+ 5. Paste the following:
16
+
17
+ ```ruby
18
+ class MyObject < Flexirest::Base
19
+ base_url "https://requestb.in"
20
+ post :create, "/{CREATE_A_PASTEBIN_ID}"
21
+ end
22
+
23
+ my_object = MyObject.new(params: "something")
24
+ my_object.create
25
+ # => something is output to the terminal
26
+ ```
@@ -8,6 +8,7 @@ module Flexirest
8
8
  class Request
9
9
  include AttributeParsing
10
10
  include JsonAPIProxy
11
+ include ActiveSupport::Inflector
11
12
  attr_accessor :post_params, :get_params, :url, :path, :headers, :method, :object, :body, :forced_url, :original_url, :retrying
12
13
 
13
14
  def initialize(method, object, params = {})
@@ -584,7 +585,11 @@ module Flexirest
584
585
  end
585
586
 
586
587
  attributes.each do |k,v|
587
- k = k.to_sym
588
+ if @method[:options][:rubify_names]
589
+ k = rubify_name(k)
590
+ else
591
+ k = k.to_sym
592
+ end
588
593
  overridden_name = select_name(k, overridden_name)
589
594
  if @method[:options][:lazy].include?(k)
590
595
  object._attributes[k] = Flexirest::LazyAssociationLoader.new(overridden_name, v, self, overridden_name:(overridden_name))
@@ -756,6 +761,10 @@ module Flexirest
756
761
  end
757
762
  end
758
763
  end
764
+
765
+ def rubify_name(k)
766
+ k.underscore.to_sym
767
+ end
759
768
  end
760
769
 
761
770
  class RequestException < StandardError ; end
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.6.3"
2
+ VERSION = "1.6.4"
3
3
  end
@@ -32,7 +32,7 @@ describe Flexirest::Request do
32
32
  get :find, "/:id", required: [:id]
33
33
  get :change, "/change"
34
34
  get :plain, "/plain/:id", plain: true
35
- post :create, "/create"
35
+ post :create, "/create", rubify_names: true
36
36
  post :test_encoding, "/encoding", request_body_type: :json
37
37
  post :testing_no_content_headers, "/no-content"
38
38
  put :update, "/put/:id"
@@ -468,6 +468,7 @@ describe Flexirest::Request do
468
468
  object.debug = true
469
469
  object.only_changed_1
470
470
  end
471
+
471
472
  it "should only send changed attributes within the :only_changed array if :only_changed is an array" do
472
473
  expect_any_instance_of(Flexirest::Connection).to receive(:patch).with("/changed2", "debug2=true", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"[{\"first_name\":\"Johnny\"}, {\"first_name\":\"Billy\"}, {\"debug\":\"true\"}]", status:200, response_headers:{})))
473
474
  object = ExampleClient.new
@@ -478,6 +479,7 @@ describe Flexirest::Request do
478
479
  object.debug2 = true
479
480
  object.only_changed_2
480
481
  end
482
+
481
483
  it "should only send changed attributes marked true within the :only_changed hash when :only_changed is a hash" do
482
484
  expect_any_instance_of(Flexirest::Connection).to receive(:patch).with("/changed3", "debug1=false&debug2=true", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"[{\"first_name\":\"Johnny\"}, {\"first_name\":\"Billy\"}, {\"debug\":\"true\"}]", status:200, response_headers:{})))
483
485
  object = ExampleClient.new
@@ -489,6 +491,7 @@ describe Flexirest::Request do
489
491
  object.debug2 = true
490
492
  object.only_changed_3
491
493
  end
494
+
492
495
  it "should always send changed attributes marked false within the :only_changed hash when :only_changed is an hash" do
493
496
  expect_any_instance_of(Flexirest::Connection).to receive(:patch).with("/changed3", "debug1=true", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"[{\"first_name\":\"Johnny\"}, {\"first_name\":\"Billy\"}, {\"debug\":\"true\"}]", status:200, response_headers:{})))
494
497
  object = ExampleClient.new
@@ -499,7 +502,6 @@ describe Flexirest::Request do
499
502
  object.only_changed_3
500
503
  end
501
504
 
502
-
503
505
  it "should instantiate other classes using has_many when required to do so" do
504
506
  expect_any_instance_of(Flexirest::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"expenses\":[{\"amount\":1}, {\"amount\":2}]}", status:200, response_headers:{})))
505
507
  object = ExampleClient.all
@@ -536,6 +538,16 @@ describe Flexirest::Request do
536
538
  expect(object.id).to eq(1234)
537
539
  end
538
540
 
541
+ it "should rubify attribute names" do
542
+ expect_any_instance_of(Flexirest::Connection).
543
+ to receive(:post).
544
+ with("/create", "", an_instance_of(Hash)).
545
+ and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"firstName\":\"John\", \"OtherProperty\":1234}", response_headers:{})))
546
+ object = ExampleClient.create
547
+ expect(object.first_name).to eq("John")
548
+ expect(object.other_property).to eq(1234)
549
+ end
550
+
539
551
  it "should expose etag if available" do
540
552
  response = ::FaradayResponseMock.new(OpenStruct.new(body: "{}", response_headers: {"ETag" => "123456"}, status: 200))
541
553
  expect_any_instance_of(Flexirest::Connection).to receive(:get).with("/123", an_instance_of(Hash)).and_return(response)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexirest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.3
4
+ version: 1.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
@@ -225,11 +225,12 @@ files:
225
225
  - README.md
226
226
  - Rakefile
227
227
  - Ruby-on-Rails-Integration.md
228
- - doc/FLEXIREST.svg
229
- - doc/FLEXIREST@2x.png
230
- - doc/Flexirest Internals.graffle
231
- - doc/Flexirest Internals.png
232
- - doc/Flexirest Logo.sketch
228
+ - docs/FLEXIREST.svg
229
+ - docs/FLEXIREST@2x.png
230
+ - docs/Flexirest Internals.graffle
231
+ - docs/Flexirest Internals.png
232
+ - docs/Flexirest Logo.sketch
233
+ - docs/issue_template.md
233
234
  - flexirest.gemspec
234
235
  - lib/flexirest.rb
235
236
  - lib/flexirest/associations.rb