apicake 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/apicake.rb +1 -0
- data/lib/apicake/base.rb +44 -0
- data/lib/apicake/exceptions.rb +3 -0
- data/lib/apicake/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e7c41c224b2b2435828e862a3047a50aaf7ea6e
|
4
|
+
data.tar.gz: eacee61fff039351173484ea34db7d7d19a11b12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e08bb8197889886c32400bc2bc035a924d730b71eb9c03f2bd4b74e91cfdda7678693c6de867d204d6efb3cd37011ff8e2513b237dcce23151f4a380ab8e8ebe
|
7
|
+
data.tar.gz: c385af56680fbad9d96437f4ae9e9a24feea6a3250a9f83d6ec7e9dfd6eaa53f7fe93017e480e1fe64bea32d0effe37d3e9a0f623dc562659e65b265eb63d3ae
|
data/README.md
CHANGED
@@ -61,6 +61,7 @@ Features
|
|
61
61
|
- Built in caching
|
62
62
|
- Built in save to file
|
63
63
|
- Built in response parsing (part of HTTParty)
|
64
|
+
- Built in convert and save to CSV
|
64
65
|
- Designed for GET-only APIs (e.g., data services)
|
65
66
|
|
66
67
|
|
@@ -115,4 +116,4 @@ Documentation to be completed
|
|
115
116
|
|
116
117
|
[1]: https://github.com/DannyBen/apicake/tree/master/examples
|
117
118
|
[2]: https://github.com/DannyBen/lightly
|
118
|
-
[3]: https://github.com/DannyBen/apicake/blob/master/examples/04-caching.rb
|
119
|
+
[3]: https://github.com/DannyBen/apicake/blob/master/examples/04-caching.rb
|
data/lib/apicake.rb
CHANGED
data/lib/apicake/base.rb
CHANGED
@@ -59,6 +59,50 @@ module APICake
|
|
59
59
|
File.write filename, payload.response.body
|
60
60
|
end
|
61
61
|
|
62
|
+
# Forwards all arguments to #get! and converts its parsed response to
|
63
|
+
# CSV. If the response contains one or more arrays, the first array will
|
64
|
+
# be the CSV output, otherwise, the response itself will be used.
|
65
|
+
# You can override this method if you wish to provide a different
|
66
|
+
# behavior.
|
67
|
+
def get_csv(*args)
|
68
|
+
payload = get!(*args)
|
69
|
+
|
70
|
+
if payload.response.code != '200'
|
71
|
+
raise BadResponse, "#{payload.response.code} #{payload.response.msg}"
|
72
|
+
end
|
73
|
+
|
74
|
+
response = payload.parsed_response
|
75
|
+
|
76
|
+
unless response.is_a? Hash
|
77
|
+
raise BadResponse, "Cannot parse response"
|
78
|
+
end
|
79
|
+
|
80
|
+
data = csv_node response
|
81
|
+
|
82
|
+
header = data.first.keys
|
83
|
+
result = CSV.generate do |csv|
|
84
|
+
csv << header
|
85
|
+
data.each { |row| csv << row.values }
|
86
|
+
end
|
87
|
+
|
88
|
+
result
|
89
|
+
end
|
90
|
+
|
91
|
+
# Send a request, convert it to CSV and save it to a file.
|
92
|
+
def save_csv(file, *args)
|
93
|
+
File.write file, get_csv(*args)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Determins which part of the data is best suited to be displayed
|
97
|
+
# as CSV.
|
98
|
+
# - In case there is an array in the data, it will be returned.
|
99
|
+
# - Otherwise, we will use the entire response as a single row CSV.
|
100
|
+
# Override this if you want to have a different decision process.
|
101
|
+
def csv_node(data)
|
102
|
+
arrays = data.keys.select { |key| data[key].is_a? Array }
|
103
|
+
arrays.empty? ? [data] : data[arrays.first]
|
104
|
+
end
|
105
|
+
|
62
106
|
private
|
63
107
|
|
64
108
|
# Make a call with HTTParty and return a payload object.
|
data/lib/apicake/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apicake
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lightly
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- README.md
|
160
160
|
- lib/apicake.rb
|
161
161
|
- lib/apicake/base.rb
|
162
|
+
- lib/apicake/exceptions.rb
|
162
163
|
- lib/apicake/payload.rb
|
163
164
|
- lib/apicake/version.rb
|
164
165
|
homepage: https://github.com/DannyBen/apicake
|