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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40e714671fc06714a55011b766e49b9b9864f844
4
- data.tar.gz: 44de7eae53d8dc0de613d8a50b7a1ec69b6d66b0
3
+ metadata.gz: 6e7c41c224b2b2435828e862a3047a50aaf7ea6e
4
+ data.tar.gz: eacee61fff039351173484ea34db7d7d19a11b12
5
5
  SHA512:
6
- metadata.gz: 72f08448fd1f559f837dfd6d689a86b4832d8b75e3259fabc82c7c2a23c2d6c512b9e9bce6e19921b39fab0726d6b8f409bba15938461d5a61e8c2cc31bf2c10
7
- data.tar.gz: 6e5d65bd2804c5b325c96a3f459408f239f3b3f7b3984ed1c844b41d10a380f70aa33f6bab7cebb949001bc18d11bbad9cd0fa94b280bad36b2ff7912ef2af7d
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
@@ -1,3 +1,4 @@
1
1
  require 'apicake/version'
2
2
  require 'apicake/base'
3
3
  require 'apicake/payload'
4
+ require 'apicake/exceptions'
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.
@@ -0,0 +1,3 @@
1
+ module APICake
2
+ class BadResponse < StandardError; end
3
+ end
@@ -1,3 +1,3 @@
1
1
  module APICake
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
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.3
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-17 00:00:00.000000000 Z
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