grape-client 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: de222ff72b1fd8635c6551dda5832a403a867955
4
- data.tar.gz: b708ca40554c08395acaf0c75e45f38233ca04a4
3
+ metadata.gz: ea69852f478ab39fc8bd0a7decc8977957026f1b
4
+ data.tar.gz: ffd3fd7206257e88a4533ca50e21de9ef0e8cb0c
5
5
  SHA512:
6
- metadata.gz: fc3cf3857176e2e3d02e2cf1e0cc2299921301287c228060e0234a46f67f671a7f3d4b1f585e5da26fb1049c68ba1cb08be426c648f17bd7d956cf4b9acfda32
7
- data.tar.gz: 420adb29ac9fb753d150c46efb8f922d9b49e7a73529f54c35ab2ecca17311a49472d7f5eb603dcf583fc355b68eb6b4265a17f2ebb35b98f5c5c7ba0771d1d8
6
+ metadata.gz: d2afdfe9f38b7836286dcd4760c44b24bbc05a61f13886a3c8662dd7165c149014f7a2d6f8d3f82a5a92f689286c924754ccef74f56846c94f9fdea8b5cdfb44
7
+ data.tar.gz: 6dec3eaa9399f5304ce403370d1c4483ac89c7a18a4764141d85cf227a0ce23ff498e0cc8a68a23550039a42d6a5329c314a1a8e99b12a1e273aee31726e918d
data/README.md CHANGED
@@ -3,6 +3,9 @@
3
3
  [![Code Climate](https://codeclimate.com/github/desofto/grape-client/badges/gpa.svg)](https://codeclimate.com/github/desofto/grape-client)
4
4
  [![Test Coverage](https://codeclimate.com/github/desofto/grape-client/badges/coverage.svg)](https://codeclimate.com/github/desofto/grape-client/coverage)
5
5
 
6
+ Simple access from your client to Grape APIs.
7
+ Automatically supports: kaminari pagination, network access, nested objects.
8
+
6
9
  ## Installation
7
10
 
8
11
  Add this line to your application's Gemfile:
@@ -21,12 +24,33 @@ Or install it yourself as:
21
24
 
22
25
  ## Usage
23
26
 
24
- ## Development
25
-
26
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
27
-
28
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
27
+ ```ruby
28
+ class User < GrapeClient::Base
29
+ self.site = 'http://localhost:3000'
30
+ self.user = 'user'
31
+ self.password = 'password'
32
+ self.prefix = '/api/v1/'
33
+
34
+ field_accessor :id, :email
35
+ end
36
+
37
+ u = User.create(email: 'test@example.com')
38
+ u.email = 'another@example.com'
39
+ u.save!
40
+ u.reload
41
+
42
+ u = User.find(2)
43
+ u.destroy
44
+
45
+ User.all.each do |user|
46
+ ...
47
+ end
48
+ ```
29
49
 
30
50
  ## Contributing
31
51
 
32
- Bug reports and pull requests are welcome on GitHub at https://github.com/desofto/grape-client.
52
+ 1. Fork it ( https://github.com/desofto/grape-client/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['dmitry@desofto.com']
11
11
 
12
12
  spec.summary = 'Simple access from your client to Grape APIs.'
13
- spec.description = 'Simple access from your client to Grape APIs.'
13
+ spec.description = 'Simple access from your client to Grape APIs. Automatically supports: kaminari pagination, network access, nested objects.'
14
14
  spec.homepage = 'https://github.com/desofto/grape-client'
15
15
  spec.license = 'MIT'
16
16
 
@@ -16,6 +16,25 @@ module GrapeClient
16
16
  end
17
17
  end
18
18
 
19
+ def belongs_to(property, options = {})
20
+ field_accessor "#{property}_id"
21
+
22
+ define_method(property) do
23
+ @attributes[property] ||= retrive_object(options[:class_name] || property,
24
+ self["#{property}_id"])
25
+ end
26
+
27
+ define_method("#{property}=") do |object|
28
+ self["#{property}_id"] = object.id
29
+ @attributes[property] = object
30
+ end
31
+
32
+ define_method("#{property}_id=") do |id|
33
+ @attributes[property] = nil
34
+ self["#{property}_id"] = id
35
+ end
36
+ end
37
+
19
38
  def connection
20
39
  @connection ||= Connection.new(user, password)
21
40
  end
@@ -39,14 +58,14 @@ module GrapeClient
39
58
  end
40
59
 
41
60
  def initialize(attrs = {})
42
- @attributes = {}
43
61
  self.attributes = attrs
44
62
  end
45
63
 
46
64
  def attributes=(attrs)
65
+ @attributes = {}
47
66
  attributes = self.class.attributes
48
67
  attrs.each do |name, value|
49
- next unless attributes.include? name.to_sym
68
+ next unless attributes.include?(name.to_sym) || methods.include?(name.to_sym)
50
69
  send("#{name}=", value)
51
70
  end
52
71
  end
@@ -75,7 +94,15 @@ module GrapeClient
75
94
 
76
95
  def to_post
77
96
  entity_name = self.class.entity_name
78
- attributes.transform_keys { |key| "#{entity_name}[#{key}]" }
97
+ list = self.class.attributes
98
+ filtered_attributes = attributes.select { |key, _value| list.include? key }
99
+ filtered_attributes.transform_keys { |key| "#{entity_name}[#{key}]" }
100
+ end
101
+
102
+ private
103
+
104
+ def retrive_object(name, id)
105
+ name.to_s.camelcase.constantize.find(id) if id.present?
79
106
  end
80
107
  end
81
108
  end
@@ -13,39 +13,41 @@ module GrapeClient
13
13
  end
14
14
 
15
15
  def each(&_block)
16
- prepare_for_iteration
17
- loop do
18
- @elements.each do |attrs|
16
+ per_page do |elements|
17
+ elements.each do |attrs|
19
18
  yield @clazz.new(attrs)
20
19
  end
21
- break unless load_next_page
22
20
  end
23
21
  end
24
22
 
25
23
  def map(&_block)
26
- prepare_for_iteration
27
24
  result = []
28
- loop do
29
- result += @elements.map do |attrs|
25
+ per_page do |elements|
26
+ result += elements.map do |attrs|
30
27
  yield @clazz.new(attrs)
31
28
  end
32
- break unless load_next_page
33
29
  end
34
30
  result
35
31
  end
36
32
 
37
33
  def collect
38
- prepare_for_iteration
39
34
  result = []
40
- loop do
41
- result += @elements
42
- break unless load_next_page
35
+ per_page do |elements|
36
+ result += elements
43
37
  end
44
38
  result
45
39
  end
46
40
 
47
41
  private
48
42
 
43
+ def per_page
44
+ prepare_for_iteration
45
+ loop do
46
+ yield @elements
47
+ break unless load_next_page
48
+ end
49
+ end
50
+
49
51
  def update(elements, headers = nil)
50
52
  @elements = elements
51
53
  @count = headers['total'].first.try(:to_i) if headers
@@ -1,3 +1,3 @@
1
1
  module GrapeClient
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Silchenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-22 00:00:00.000000000 Z
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,7 +66,8 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '4.2'
69
- description: Simple access from your client to Grape APIs.
69
+ description: 'Simple access from your client to Grape APIs. Automatically supports:
70
+ kaminari pagination, network access, nested objects.'
70
71
  email:
71
72
  - dmitry@desofto.com
72
73
  executables: []