spigot 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +154 -8
- data/lib/spigot/translator.rb +0 -3
- data/lib/spigot/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e07ea63fbeff718ff094b1065c149f27e196503
|
4
|
+
data.tar.gz: 835f293ba6e02ffaae34e9d503903382a7352c30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e433a4f0385ce33d62820cb2ca39ce9d3bc42323da041ace8880ee359bd1511ac16309d16d178b535070c54f0573d4cf0885e11366ded410503996d93fb25b04
|
7
|
+
data.tar.gz: 2d34f017ce44682f090626f00b7164a52ac67a103f27161e90612ba5843ce5f5ec61f481c87b090cb463e505c3f2fd67cc85216c3634255ad4a2c7f3a7a3c63f
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# Spigot
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/mwerner/spigot.png?branch=master)](https://travis-ci.org/mwerner/spigot
|
4
|
-
[![Code Climate](https://codeclimate.com/github/mwerner/spigot.png)](https://codeclimate.com/github/mwerner/spigot
|
5
|
-
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/mwerner/spigot/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
|
3
|
+
[![Build Status](https://travis-ci.org/mwerner/spigot.png?branch=master)](https://travis-ci.org/mwerner/spigot "Travis CI")
|
4
|
+
[![Code Climate](https://codeclimate.com/github/mwerner/spigot.png)](https://codeclimate.com/github/mwerner/spigot "Code Climate")
|
6
5
|
|
7
6
|
Spigot is an attempt to bring some sanity to consuming external API data. Without Spigot, you need
|
8
7
|
to do this manual mapping at creation, such as:
|
@@ -28,17 +27,14 @@ to do this manual mapping at creation, such as:
|
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
31
|
-
This becomes particularly difficult as you start having multiple external sources for the same resource (
|
30
|
+
This becomes particularly difficult as you start having multiple external sources for the same resource (eg: users from both twitter and facebook).
|
32
31
|
|
33
|
-
Spigot
|
34
|
-
able to convey a mapping of their structure into your attributes in a concise format. Afterwards, you can accomplish the above work in a simple statement:
|
32
|
+
Spigot produces a map of the raw API data you receive to columns in your database. As a result, you're able to parse their data structure into meaningful attributes in a very concise expression. This turns the previous code block into the following statement:
|
35
33
|
|
36
34
|
User.find_or_create_by_api(params[:data])
|
37
35
|
|
38
36
|
Much better.
|
39
37
|
|
40
|
-
[Read More](http://mwerner.github.io/spigot/)
|
41
|
-
|
42
38
|
## Installation
|
43
39
|
|
44
40
|
Add this line to your application's Gemfile:
|
@@ -53,6 +49,156 @@ Or install it yourself as:
|
|
53
49
|
|
54
50
|
$ gem install spigot
|
55
51
|
|
52
|
+
## Setup
|
53
|
+
|
54
|
+
Spigot is configured using simple ruby blocks. You just create an initializer, which evaluates your Spigot definition block to be ready for any data you throw at it.
|
55
|
+
|
56
|
+
# config/initializers/spigot.rb
|
57
|
+
Spigot.define do
|
58
|
+
service :github do
|
59
|
+
resource :pull_request do
|
60
|
+
id :github_id
|
61
|
+
number :number
|
62
|
+
created :created_at
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
This `define` block establishes a `pull_request` resource that is sourced by the `github` service. When you include the Spigot mixin to the `PullRequest` model, the class will gain the ability to parse raw pull request data as received from the Github API.
|
68
|
+
|
69
|
+
class PullRequest < ActiveRecord::Base
|
70
|
+
include Spigot::Base
|
71
|
+
end
|
72
|
+
|
73
|
+
PullRequest.create_by_api(github: params[:pull_request])
|
74
|
+
|
75
|
+
## Services
|
76
|
+
|
77
|
+
Services are the source from which you are receiving the data you're consuming. By specifying the service which you are using, you're able to accurately map the same resource from multiple sources (such as a User from Twitter and Facebook's API data formats).
|
78
|
+
|
79
|
+
Inside your service block you must further define a `resource`. Any API data that you map can only be attributed to a resource in your app.
|
80
|
+
|
81
|
+
It is not required to specify a service. You only need to do so if you need to parse the same resource data from multiple services. If you are not parsing multiple services, you can instead define your spigot map with only the resource definition:
|
82
|
+
|
83
|
+
Spigot.define do
|
84
|
+
resource :pull_request do
|
85
|
+
id :github_id
|
86
|
+
number :number
|
87
|
+
created :created_at
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
Then, when invoking the methods on the model, you do not need to specify a service when passing in the data.
|
92
|
+
|
93
|
+
class PullRequest < ActiveRecord::Base
|
94
|
+
include Spigot::Base
|
95
|
+
end
|
96
|
+
|
97
|
+
PullRequest.create_by_api(params[:pull_request])
|
98
|
+
|
99
|
+
## Resources
|
100
|
+
|
101
|
+
A resource is a model in your app that will receive the parsed data provided by spigot. These resources are defined using a ruby block:
|
102
|
+
|
103
|
+
Spigot.define do
|
104
|
+
resource :user do
|
105
|
+
login :username
|
106
|
+
name :full_name
|
107
|
+
created :created_at
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
The method you are calling within the block corresponds to the API data key you are attempting to access. The symbol you specify, or pass to the function, corresponds to your database table attribute to which the value will be assigned.
|
112
|
+
|
113
|
+
A good way to remember which is which is to say (from the above example) "Their `login` is my `username`. Their `name` is my `full_name`".
|
114
|
+
|
115
|
+
## Parsing Data
|
116
|
+
|
117
|
+
When Spigot parses data, it will read the resource definition present in your Spigot map. It will format the raw API data which passed in, and return a hash of data in the format the calling model can understand.
|
118
|
+
|
119
|
+
Looking at an example:
|
120
|
+
|
121
|
+
Spigot.resource(:pull_request) do
|
122
|
+
id :github_id
|
123
|
+
metadata do
|
124
|
+
number :number do |attr|
|
125
|
+
"##{attr}"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
author User
|
129
|
+
created :created_at
|
130
|
+
end
|
131
|
+
|
132
|
+
There are several things happening in this definition, let's look at each one.
|
133
|
+
|
134
|
+
**Nested block**
|
135
|
+
|
136
|
+
If you have a nested block, Spigot will dig down into the API data, to retreive the keys specified inside the block.
|
137
|
+
|
138
|
+
data = params[:pull_request]
|
139
|
+
if data[:metadata].present?
|
140
|
+
number = data[:metadata][:number]
|
141
|
+
end
|
142
|
+
|
143
|
+
**Attribute block**
|
144
|
+
|
145
|
+
When you pass a block to an attribute mapping, that block will be executed on the value found at that location in the API data. You can use this to manipulate and massage the data into exactly what you'd like to assign to your attribute. In this case we're prepending a hashtag before the pull request number.
|
146
|
+
|
147
|
+
number = data[:metadata][:number]
|
148
|
+
pr.number = "##{number}"
|
149
|
+
|
150
|
+
**Nested Resource**
|
151
|
+
|
152
|
+
If the API data you are receiving has an associated resource which you would also like to capture, you can pass a class inside the resource block. Spigot will recognize that `User` has a defined Spigot map and will use it to create a user. Once the nested data has been parsed and used to create a user, Spigot will merge a key value pair associating the created user's id to the API data which `PullRequest` will use.
|
153
|
+
|
154
|
+
userdata = data.delete(:author)
|
155
|
+
user = User.find_or_create_by_api(userdata)
|
156
|
+
data.merge!(user_id: user.id)
|
157
|
+
|
158
|
+
**Resulting Data**
|
159
|
+
|
160
|
+
With the above example, this is the data which would be parsed:
|
161
|
+
|
162
|
+
# Original
|
163
|
+
{ id: 123, metadata: {number: 456}, author: { id: 987, login: 'mwerner' }, created: '2000-04-01 00:01:00' }
|
164
|
+
|
165
|
+
# Parsed data, passed to PullRequest
|
166
|
+
{ github_id: 123, number: '#456', user_id: 1, created_at: '2000-04-01 00:01:00' }
|
167
|
+
|
168
|
+
**Passing Arrays of data**
|
169
|
+
|
170
|
+
Anytime you are parsing an array of data, Spigot will iterate over each element of the array and run the invoked function on each item. It will then return an array of objects which have been created for each element of the array.
|
171
|
+
|
172
|
+
## Abbreviated Setup
|
173
|
+
|
174
|
+
If you are only consuming one resource from one service, you can use abbreviated syntax to make your Spigot implementation more concise. The following two code blocks are equivalent:
|
175
|
+
|
176
|
+
Spigot.define do
|
177
|
+
resource :user do
|
178
|
+
login :username
|
179
|
+
created :created_at
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
Spigot.resource :user do
|
184
|
+
login :username
|
185
|
+
created :created_at
|
186
|
+
end
|
187
|
+
|
188
|
+
This method can be used for both `service` and `resource`.
|
189
|
+
|
190
|
+
## Alternative Syntax
|
191
|
+
|
192
|
+
Every class which you include `Spigot::Base` gains a variety of methods to find or save data. These methods can be accessed by a proxy object included on the class. This provides an object that contains all the parsing logic, as well as a more concise syntax.
|
193
|
+
|
194
|
+
# No service specified
|
195
|
+
client = PullRequest.spigot
|
196
|
+
client.find_or_create(data)
|
197
|
+
|
198
|
+
# Using the :github service
|
199
|
+
github = PullRequest.spigot(:github)
|
200
|
+
github.find_or_create(data)
|
201
|
+
|
56
202
|
## Contributing
|
57
203
|
|
58
204
|
1. Fork it
|
data/lib/spigot/translator.rb
CHANGED
data/lib/spigot/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spigot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Werner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
181
|
version: '0'
|
182
182
|
requirements: []
|
183
183
|
rubyforge_project:
|
184
|
-
rubygems_version: 2.2.
|
184
|
+
rubygems_version: 2.2.0
|
185
185
|
signing_key:
|
186
186
|
specification_version: 4
|
187
187
|
summary: Spigot is a tool to parse and format data for the creation of ruby objects
|