grape-client 0.3.2 → 0.3.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
2
  SHA1:
3
- metadata.gz: 4ceaceb46fa4a688ef855c6fb4f93edbdce4eb91
4
- data.tar.gz: fd74c6a7c64947991f1c18c9f008300af3cd74bf
3
+ metadata.gz: 341b46f253dfcb759fe23752366c38f6ae202f8b
4
+ data.tar.gz: fff3c514bc2727305905d86b6f866c9671eaef03
5
5
  SHA512:
6
- metadata.gz: d2af71e884d0235bcf3ca41c22147b1e13445ce843ec1bb37280709918a83956c3446ab8bd826c71f1fd85deb352fd4fca18187d41f78e1a6e621f79095622bd
7
- data.tar.gz: f36fcae56089adfe5402349dc60a3a4e5f20f574ed7fc625421d9ef0161c3a3e6b6621184e1fc034cb8d506320d202d9f30013f4a5a92706de0bd17e75806e21
6
+ metadata.gz: 82f1698dc7ec49188fa943a0a11edd14ccefaccd5022fd0af67c891bdb8a53b44eb7798ec4a184540f7ecdf5ca648a0d5c5046d79e84b13def2fa07ae495be97
7
+ data.tar.gz: 0a111811d89accbd8104ca14b8a2dd128af5ff032cf18fe7e89d7ad3f0b075813b371f834c4753a6fe17dd573b11139e3d3a31546195c3fac292b7de5f4ad1e6
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # GrapeClient
2
+ [![GitHub version](https://badge.fury.io/gh/desofto%2Fgrape-client.svg)](https://badge.fury.io/gh/desofto%2Fgrape-client)
3
+ [![Gem Version](https://badge.fury.io/rb/grape-client.svg)](https://badge.fury.io/rb/grape-client)
2
4
  [![Build Status](https://travis-ci.org/desofto/grape-client.svg?branch=master)](https://travis-ci.org/desofto/grape-client)
3
5
  [![Code Climate](https://codeclimate.com/github/desofto/grape-client/badges/gpa.svg)](https://codeclimate.com/github/desofto/grape-client)
4
6
  [![Test Coverage](https://codeclimate.com/github/desofto/grape-client/badges/coverage.svg)](https://codeclimate.com/github/desofto/grape-client/coverage)
@@ -9,15 +9,15 @@ module GrapeClient
9
9
  include BelongsTo
10
10
  include HasMany
11
11
 
12
- attr_accessor :attributes
12
+ attr_reader :attributes
13
13
  attr_accessor :site, :user, :password, :prefix
14
14
 
15
15
  def inherited(child)
16
- child.attributes = attributes.try(:dup) || []
17
- child.site = GrapeClient.configuration.site
18
- child.user = GrapeClient.configuration.user
19
- child.password = GrapeClient.configuration.password
20
- child.prefix = GrapeClient.configuration.prefix
16
+ child.instance_variable_set('@attributes', attributes.try(:dup) || [])
17
+ child.instance_variable_set('@site', GrapeClient.configuration.site)
18
+ child.instance_variable_set('@user', GrapeClient.configuration.user)
19
+ child.instance_variable_set('@password', GrapeClient.configuration.password)
20
+ child.instance_variable_set('@prefix', GrapeClient.configuration.prefix)
21
21
  end
22
22
 
23
23
  def attr_accessor(*names)
@@ -79,15 +79,18 @@ module GrapeClient
79
79
  end
80
80
 
81
81
  def respond_to?(method_name, *args, &block)
82
- name = method_name.to_s
83
- name = name[0..-2] if name.last == '='
84
- self.class.attributes.include?(name.to_sym) || super
82
+ super ||
83
+ begin
84
+ name = method_name.to_s
85
+ name = name[0..-2] if name.last == '='
86
+ self.class.attributes.include?(name.to_sym)
87
+ end
85
88
  end
86
89
 
87
90
  def to_post
88
- entity_name = self.class.entity_name
89
91
  list = self.class.attributes
90
92
  filtered_attributes = attributes.select { |key, _value| list.include? key }
93
+ entity_name = self.class.entity_name
91
94
  filtered_attributes.transform_keys { |key| "#{entity_name}[#{key}]" }
92
95
  end
93
96
 
@@ -38,6 +38,14 @@ module GrapeClient
38
38
  result
39
39
  end
40
40
 
41
+ def empty?
42
+ @is_first_page && @elements.empty?
43
+ end
44
+
45
+ def any?
46
+ !empty?
47
+ end
48
+
41
49
  private
42
50
 
43
51
  def per_page
@@ -50,8 +50,8 @@ module GrapeClient
50
50
  def validate_response(res)
51
51
  case res
52
52
  when Net::HTTPUnauthorized then raise Unauthorized
53
- when Net::HTTPSuccess then res.body
54
53
  when Net::HTTPUnprocessableEntity then raise InvalidEntity
54
+ when Net::HTTPSuccess then res.body
55
55
  else raise UnknownError, res.body
56
56
  end
57
57
  end
@@ -5,7 +5,7 @@ module GrapeClient
5
5
  clazz = class_from_name(options[:class_name] || property.to_s.singularize)
6
6
  case @attributes[property]
7
7
  when Collection then @attributes[property]
8
- when nil?
8
+ when nil
9
9
  @attributes[property] = clazz.where("#{self.class.entity_name}_id" => id)
10
10
  else
11
11
  @attributes[property].map! do |element|
@@ -49,6 +49,7 @@ module GrapeClient
49
49
 
50
50
  def request(method, url, params = {}, &_block)
51
51
  url = [endpoint, url].compact.join('/')
52
+ method = method.compact.join('/') if method.is_a? Array
52
53
  response = connection.request method, url, params
53
54
  if block_given?
54
55
  yield response
@@ -13,7 +13,7 @@ module GrapeClient
13
13
 
14
14
  def save
15
15
  save!
16
- rescue Connection::InvalidEntity
16
+ rescue Connection::InvalidEntity, Connection::UnknownError
17
17
  false
18
18
  end
19
19
 
@@ -32,19 +32,20 @@ module GrapeClient
32
32
 
33
33
  protected
34
34
 
35
+ def method_with_id(method)
36
+ [id, method].compact.join('/')
37
+ end
38
+
35
39
  def get(method, params = {}, &block)
36
- method = [id, method].compact.join('/')
37
- self.class.send(:get, method, params, &block)
40
+ self.class.send(:get, method_with_id(method), params, &block)
38
41
  end
39
42
 
40
43
  def put(method, params = {}, &block)
41
- method = [id, method].compact.join('/')
42
- self.class.send(:put, method, params, &block)
44
+ self.class.send(:put, method_with_id(method), params, &block)
43
45
  end
44
46
 
45
47
  def post(method, params = {}, &block)
46
- method = [id, method].compact.join('/')
47
- self.class.send(:post, method, params, &block)
48
+ self.class.send(:post, method_with_id(method), params, &block)
48
49
  end
49
50
 
50
51
  def delete(params = {}, &block)
@@ -1,3 +1,3 @@
1
1
  module GrapeClient
2
- VERSION = '0.3.2'.freeze
2
+ VERSION = '0.3.3'.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.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Silchenko