vericred 0.1.0 → 0.1.1

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: ae610f28b156fd1c08054c0797f69b7aaec226af
4
- data.tar.gz: baaaffe27dc229dcbc68706318871580231609e3
3
+ metadata.gz: 33dc91cd863234fdc18029ae075c592013916e44
4
+ data.tar.gz: a2d1989b06607f9bfb26d587a1985db654022734
5
5
  SHA512:
6
- metadata.gz: 27d553dde5b5ddf8a364e694baf21bdbca69c4300df72f4746eed5e011340a4a7aecc72edd8cc222c78f18607dd9d35543bf3c878c721378847fb9d12b904da0
7
- data.tar.gz: f103bc7e1b19bdf694f8b8b38795bfdd99d7906ffe379a6808039ef745921713bd40639c75373fed62d56c14dd4a7193750c7ff5f3ebf5bcf219a47090768d6c
6
+ metadata.gz: fee62b0ece4317956b7d54cfa9745bf3a8cb0e5a4217f898360921dc386f679bae4d6900728ef96f9c6b359264b5af5b3e091acfe36a56019b65c452c84effb1
7
+ data.tar.gz: 34611b966590b139577978efbc6693b45c3d69441483430ac83d04efed1065cc580ec1c134c342f310e568d3f5eb3e97e7fd164a8b426213a8dbbf93a9444a7a
@@ -53,16 +53,14 @@ module Vericred
53
53
  (data[root_name.pluralize] || []).map { |row| new(row, data) }
54
54
  end
55
55
 
56
- def initialize(attributes, full_data = {})
57
- parse_relationships(attributes, full_data)
58
- @data = OpenStruct.new(attributes)
56
+ def initialize(attrs, full_data = {})
57
+ parse_relationships(attrs, full_data)
58
+ @data = OpenStruct.new(attrs)
59
59
  end
60
60
 
61
61
  private
62
62
 
63
- def self.make_request(verb, uri, *args)
64
- logger.info { "#{verb.to_s.upcase} #{uri} with #{args}"}
65
- response = connection.send(verb, "#{BASE_URL}#{uri}", *args)
63
+ def self.handle_response(response)
66
64
  case response.status
67
65
  when 200..299 then JSON.parse(response.content)
68
66
  when 401 then fail Vericred::UnauthenticatedError, response
@@ -73,26 +71,41 @@ module Vericred
73
71
  end
74
72
  end
75
73
 
74
+ def self.make_request(verb, uri, *args)
75
+ logger.info { "#{verb.to_s.upcase} #{uri} with #{args}"}
76
+ response = nil
77
+ ActiveSupport::Notifications
78
+ .instrument "vericred.http_request", opts: [verb, uri, *args] do
79
+ response = connection.send(verb, "#{BASE_URL}#{uri}", *args)
80
+ end
81
+ handle_response(response)
82
+ end
83
+
76
84
  def method_missing(m, *args, &block)
77
85
  return @data.send(m, *args, &block) if @data.respond_to?(m)
78
86
  super
79
87
  end
80
88
 
81
- def parse_relationships(attributes, full_data)
89
+ def parse_plural_relationship(relationship, attrs, full_data)
90
+ attrs[relationship.root_name] =
91
+ full_data[relationship.root_name]
92
+ .select { |row| attrs[relationship.foreign_key].include?(row['id'])}
93
+ .map { |row| OpenStruct.new(row) }
94
+ end
95
+
96
+ def parse_singular_relationship(relationship, attrs, full_data)
97
+ record = full_data[relationship.root_name]
98
+ .find { |row| row['id'] == attrs[relationship.foreign_key] }
99
+ attrs[relationship.root_name.singularize] =
100
+ record ? OpenStruct.new(record) : nil
101
+ end
102
+
103
+ def parse_relationships(attrs, full_data)
82
104
  relationships.values.map(&:values).flatten.each do |relationship|
83
105
  next if full_data[relationship.root_name].blank?
84
- if relationship.singular?
85
- record = full_data[relationship.root_name].find do |row|
86
- row['id'] == attributes[relationship.foreign_key]
87
- end
88
- attributes[relationship.root_name.singularize] =
89
- record ? OpenStruct.new(record) : nil
90
- else
91
- attributes[relationship.root_name] =
92
- full_data[relationship.root_name]
93
- .select { |row| attributes[relationship.foreign_key].include?(row['id'])}
94
- .map { |row| OpenStruct.new(row) }
95
- end
106
+ relationship.singular? ?
107
+ parse_singular_relationship(relationship, attrs, full_data) :
108
+ parse_plural_relationship(relationship, attrs, full_data)
96
109
  end
97
110
  end
98
111
  end
@@ -1,3 +1,3 @@
1
1
  module Vericred
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vericred
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Langevin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-18 00:00:00.000000000 Z
11
+ date: 2015-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport