grape-client 0.2.0 → 0.3.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: ea69852f478ab39fc8bd0a7decc8977957026f1b
4
- data.tar.gz: ffd3fd7206257e88a4533ca50e21de9ef0e8cb0c
3
+ metadata.gz: 3d99e13172df1dbea1cdcd2e85d189ef17d4399d
4
+ data.tar.gz: 2ac2dffcf2dad4ff2c9c4a6ae8d082b86272a84a
5
5
  SHA512:
6
- metadata.gz: d2afdfe9f38b7836286dcd4760c44b24bbc05a61f13886a3c8662dd7165c149014f7a2d6f8d3f82a5a92f689286c924754ccef74f56846c94f9fdea8b5cdfb44
7
- data.tar.gz: 6dec3eaa9399f5304ce403370d1c4483ac89c7a18a4764141d85cf227a0ce23ff498e0cc8a68a23550039a42d6a5329c314a1a8e99b12a1e273aee31726e918d
6
+ metadata.gz: 9e5d85cad27a76b8272d449478b6e1fd3e796993df972f87195537fd6fd1fe59dba9dd78f9b55ed6fc4e40b306925ccd19aa33b8626b5e050f787740caadbe8a
7
+ data.tar.gz: 94b0242a1d87f4057f3c051cf7514f73b6cfa22e42989ff42868d14674bdac9f242243dd3392141bcb3d97921594e92b674428d017e747f6794f57db3aabe2dd
data/.rubocop_todo.yml CHANGED
@@ -1,33 +1,31 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2016-03-22 11:41:17 +0200 using RuboCop version 0.37.2.
3
+ # on 2016-03-23 18:23:13 +0200 using RuboCop version 0.37.2.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 3
9
+ # Offense count: 1
10
+ Metrics/AbcSize:
11
+ Max: 19
12
+
13
+ # Offense count: 9
10
14
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
11
15
  # URISchemes: http, https
12
16
  Metrics/LineLength:
13
17
  Max: 104
14
18
 
15
- # Offense count: 1
19
+ # Offense count: 2
16
20
  # Configuration parameters: CountComments.
17
21
  Metrics/MethodLength:
18
- Max: 15
22
+ Max: 16
19
23
 
20
- # Offense count: 9
21
- Style/Documentation:
24
+ # Offense count: 1
25
+ # Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
26
+ # NamePrefix: is_, has_, have_
27
+ # NamePrefixBlacklist: is_, has_, have_
28
+ # NameWhitelist: is_a?
29
+ Style/PredicateName:
22
30
  Exclude:
23
- - 'spec/**/*'
24
- - 'test/**/*'
25
- - 'lib/grape_client.rb'
26
- - 'lib/grape_client/accessors.rb'
27
- - 'lib/grape_client/base.rb'
28
- - 'lib/grape_client/cache.rb'
29
- - 'lib/grape_client/collection.rb'
30
- - 'lib/grape_client/connection.rb'
31
- - 'lib/grape_client/response_parser.rb'
32
- - 'lib/grape_client/rest_methods_collection.rb'
33
- - 'lib/grape_client/rest_methods_member.rb'
31
+ - 'lib/grape_client/has_many.rb'
data/README.md CHANGED
@@ -4,7 +4,7 @@
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
6
  Simple access from your client to Grape APIs.
7
- Automatically supports: kaminari pagination, network access, nested objects.
7
+ Automatically supports: kaminari pagination, network access cache, nested objects, http basic authentication.
8
8
 
9
9
  ## Installation
10
10
 
@@ -29,9 +29,9 @@ class User < GrapeClient::Base
29
29
  self.site = 'http://localhost:3000'
30
30
  self.user = 'user'
31
31
  self.password = 'password'
32
- self.prefix = '/api/v1/'
32
+ self.prefix = '/api/v1/'
33
33
 
34
- field_accessor :id, :email
34
+ attr_accessor :id, :email
35
35
  end
36
36
 
37
37
  u = User.create(email: 'test@example.com')
data/grape-client.gemspec CHANGED
@@ -10,7 +10,9 @@ 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. Automatically supports: kaminari pagination, network access, nested objects.'
13
+ spec.description = 'Simple access from your client to Grape APIs.'\
14
+ 'Automatically supports: kaminari pagination, '\
15
+ 'network access cache, nested objects, http basic authentication.'
14
16
  spec.homepage = 'https://github.com/desofto/grape-client'
15
17
  spec.license = 'MIT'
16
18
 
@@ -1,40 +1,22 @@
1
1
  module GrapeClient
2
2
  class Base
3
- extend Accessors
4
- mattr_accessor :site, :user, :password, :prefix
5
-
6
- extend RestMethodsCollection
7
3
  include RestMethodsMember
8
4
 
5
+ mattr_accessor :site, :user, :password, :prefix
9
6
  attr_reader :attributes
10
7
 
11
8
  class << self
12
- def field_accessor(*names)
9
+ include RestMethodsCollection
10
+ include BelongsTo
11
+ include HasMany
12
+
13
+ def attr_accessor(*names)
13
14
  attributes = self.attributes
14
15
  names.each do |name|
15
16
  attributes << name.to_sym
16
17
  end
17
18
  end
18
19
 
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
-
38
20
  def connection
39
21
  @connection ||= Connection.new(user, password)
40
22
  end
@@ -92,6 +74,12 @@ module GrapeClient
92
74
  end
93
75
  end
94
76
 
77
+ def respond_to?(method_name, *args, &block)
78
+ name = method_name.to_s
79
+ name = name[0..-2] if name.last == '='
80
+ self.class.attributes.include?(name.to_sym) || super
81
+ end
82
+
95
83
  def to_post
96
84
  entity_name = self.class.entity_name
97
85
  list = self.class.attributes
@@ -101,8 +89,8 @@ module GrapeClient
101
89
 
102
90
  private
103
91
 
104
- def retrive_object(name, id)
105
- name.to_s.camelcase.constantize.find(id) if id.present?
92
+ def class_from_name(name)
93
+ name.to_s.camelcase.constantize
106
94
  end
107
95
  end
108
96
  end
@@ -0,0 +1,36 @@
1
+ module GrapeClient
2
+ module BelongsTo
3
+ def belongs_to(property, options = {})
4
+ attr_accessor "#{property}_id"
5
+
6
+ define_method("#{property}_id=") do |id|
7
+ @attributes[property] = nil
8
+ self["#{property}_id"] = id
9
+ end
10
+
11
+ define_object_getter(property, options)
12
+ define_object_setter(property, options)
13
+ end
14
+
15
+ private
16
+
17
+ def define_object_getter(property, options = {})
18
+ define_method(property) do
19
+ clazz = class_from_name(options[:class_name] || property)
20
+ if @attributes[property].is_a? Hash
21
+ @attributes[property] = clazz.new(@attributes[property])
22
+ else
23
+ id = self["#{property}_id"]
24
+ @attributes[property] ||= clazz.find(id) if id.present?
25
+ end
26
+ end
27
+ end
28
+
29
+ def define_object_setter(property, _options = {})
30
+ define_method("#{property}=") do |value|
31
+ self["#{property}_id"] = value.try(:id) unless value.is_a? Hash
32
+ @attributes[property] = value
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ module GrapeClient
2
+ module HasMany
3
+ def has_many(property, options = {})
4
+ define_method(property) do
5
+ clazz = class_from_name(options[:class_name] || property.to_s.singularize)
6
+ case @attributes[property]
7
+ when Collection then @attributes[property]
8
+ when nil?
9
+ @attributes[property] = clazz.where("#{self.class.entity_name}_id" => id)
10
+ else
11
+ @attributes[property].map! do |element|
12
+ next element unless element.is_a? Hash
13
+ clazz.new(element)
14
+ end
15
+ end
16
+ end
17
+
18
+ define_method("#{property}=") do |array|
19
+ @attributes[property] = array
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module GrapeClient
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Silchenko
@@ -66,8 +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. Automatically supports:
70
- kaminari pagination, network access, nested objects.'
69
+ description: 'Simple access from your client to Grape APIs.Automatically supports:
70
+ kaminari pagination, network access cache, nested objects, http basic authentication.'
71
71
  email:
72
72
  - dmitry@desofto.com
73
73
  executables: []
@@ -87,11 +87,12 @@ files:
87
87
  - bin/setup
88
88
  - grape-client.gemspec
89
89
  - lib/grape_client.rb
90
- - lib/grape_client/accessors.rb
91
90
  - lib/grape_client/base.rb
91
+ - lib/grape_client/belongs_to.rb
92
92
  - lib/grape_client/cache.rb
93
93
  - lib/grape_client/collection.rb
94
94
  - lib/grape_client/connection.rb
95
+ - lib/grape_client/has_many.rb
95
96
  - lib/grape_client/response_parser.rb
96
97
  - lib/grape_client/rest_methods_collection.rb
97
98
  - lib/grape_client/rest_methods_member.rb
@@ -1,14 +0,0 @@
1
- module GrapeClient
2
- module Accessors
3
- def mattr_accessor(*attr_names)
4
- attr_names.each do |attr_name|
5
- define_singleton_method(attr_name) do
6
- class_variable_get("@@#{attr_name}")
7
- end
8
- define_singleton_method("#{attr_name}=") do |value|
9
- class_variable_set("@@#{attr_name}", value)
10
- end
11
- end
12
- end
13
- end
14
- end