takeout 0.1.3 → 1.0.0

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: 2031cb01ff9f1e947b56c0425e139c1844d9c900
4
- data.tar.gz: 40cc086b045ed2774bf546636d3bc30afdedef2c
3
+ metadata.gz: 4dc87fd70527056a70cf40eea35d282d148acd4f
4
+ data.tar.gz: e8df926a3d6df0f27c813d99b08dff72a759e460
5
5
  SHA512:
6
- metadata.gz: ac33508401257dedea5dd3c577bd9d94e2c6c87d4341e90a24fb9b6b46ee746ac461d986d1066add3cfe274e63fc6b6a5914d5d66d5ff3bfae35a6203eade998
7
- data.tar.gz: 6090c18d3bebfe54cfcc689a881d1814aa12d3a8dd3ead6ef40a6c454697b3def7ebc22c9a29aaa295f8f235fefc3f9415ef3ec4e34cda5eed75bbb5f995bc76
6
+ metadata.gz: bd7596e61f5e2dc57c4567322b47eaa8b50b17c2cbcc24323b74357fd425bc03017d08dc0a8cc8a29793377bfbb01b3e5e57593fdb8d4a32b05f806ddfea25fa
7
+ data.tar.gz: 8871654e9b355e8de3001ff3978f8ab2b57ea4f19dcd17ac3dc71b41e51ce189d1de5a86a21c9fb7945711f1aff6a0282ff1e2432910c80066a5d7f1fbcef0ed
@@ -27,13 +27,10 @@ module Takeout
27
27
  # @return [String] the uri to send requests to
28
28
  attr_accessor :uri
29
29
 
30
- # @return [Hash] the hash containing the endpoints by request type to generate methods for
31
- attr_reader :endpoints
32
-
33
30
  attr_accessor :port
34
31
 
35
32
  # A constant specifying the kind of event callbacks to raise errors for
36
- FAILURES = [:failure, :missing, :redirect]
33
+ FAILURES = [:failure, :missing]
37
34
 
38
35
  # The main client initialization method.
39
36
  # ==== Attributes
@@ -42,7 +39,6 @@ module Takeout
42
39
  # ==== Options
43
40
  #
44
41
  # * +:uri+ - A string defining the URI for the API to call.
45
- # * +:endpoints+ - A hash containing the endpoints by request type to generate methods for
46
42
  # * +:headers+ - A hash specifying the headers to apply to each request
47
43
  # * +:ssl+ - A boolean to specify whether or not SSL is turned on
48
44
  # * +:schemas+ - A hash specifying the custom per-endpoint schema templates
@@ -67,13 +63,6 @@ module Takeout
67
63
  return !port.nil?
68
64
  end
69
65
 
70
- # Sets the instance variable and then generates the dynamic methods by calling #generate_enpoint_methods
71
- # @param [Hash] value A hash specifying the custom per-endpoint schema templates
72
- def endpoints=(value)
73
- generate_endpoint_methods(value)
74
- @endpoints = value
75
- end
76
-
77
66
  # Flips the @ssl instance variable to true
78
67
  def enable_ssl
79
68
  @ssl=true
@@ -86,31 +75,6 @@ module Takeout
86
75
 
87
76
  private
88
77
 
89
- # Generates the dynamic (request_type)_(endpoint_name) methods that allow you to access your API.
90
- # @param [Hash] endpoints A hash with the form {request_type: :endpoint_name} or {request_type: [:endpoint_name1, :endpoint_name_2]}
91
- def generate_endpoint_methods(endpoints)
92
- endpoints.each do |request_type, endpoint_names|
93
- # Force any give values into an array and then iterate over that
94
- [endpoint_names].flatten(1).each do |request_name|
95
- define_singleton_method("#{request_type}_#{request_name}".to_sym) do |options={}|
96
- # Extract values that we store separately from the options hash and then clean it up
97
- headers.merge!(options[:headers]) if options[:headers]
98
-
99
- # Merge in global options
100
- options.merge!(@options) if @options
101
-
102
- # Build the request_url and update the options to remove templated values (if there are any)
103
- request_url, options = generate_request_url(request_name, request_type, options)
104
-
105
- # Clean up options hash before performing request
106
- [:headers, :extension, :object_id].each { |value| options.delete(value)}
107
-
108
- return perform_curl_request(request_type, request_url, options, headers)
109
- end
110
- end
111
- end
112
- end
113
-
114
78
  # Render out the template values and return the updated options hash
115
79
  # @param [String] endpoint
116
80
  # @param [String] request_type
@@ -159,12 +123,13 @@ module Takeout
159
123
  curl.password = options[:password]
160
124
  end
161
125
 
162
- curl.on_success {|response| @parsed_body, @failure = Oj.load(response.body_str), false }
126
+ curl.on_success {|response| @parsed_body, @failure = Takeout::Response.new(headers: response.headers, body: Oj.load(response.body_str), response: response), false}
127
+ curl.on_redirect {|response| @parsed_body, @failure = Takeout::Response.new(headers: response.headers, body: Oj.load(response.body_str), response: response), false}
163
128
 
164
- FAILURES.each { |failure_type| curl.send("on_#{failure_type}") {@failure=true} }
129
+ FAILURES.each { |failure_type| curl.send("on_#{failure_type}") {|response| @parsed_body, @failure = Takeout::Response.new(headers: response.headers, body: Oj.load(response.body_str), response: response), true} }
165
130
  end
166
131
 
167
- raise Takeout::EndpointFailureError.new(curl, request_type) if @failure
132
+ raise Takeout::EndpointFailureError.new(curl, request_type, @parsed_body) if @failure
168
133
 
169
134
  return @parsed_body
170
135
  end
@@ -176,10 +141,10 @@ module Takeout
176
141
 
177
142
  # Generate URL based on if the custom schema exists, and if there is a given object_id
178
143
  request_url = if custom_schema.nil? || (custom_schema && custom_schema.empty?)
179
- (options[:object_id] ? url("/#{endpoint_name.to_s}/#{options[:object_id]}") : url("/#{endpoint_name.to_s}"))
180
- else
181
- url(custom_schema)
182
- end
144
+ (options[:object_id] ? url("/#{endpoint_name.to_s}/#{options[:object_id]}") : url("/#{endpoint_name.to_s}"))
145
+ else
146
+ url(custom_schema)
147
+ end
183
148
 
184
149
  # Append extension if one is given
185
150
  request_url = append_extension(request_url, options)
@@ -195,7 +160,6 @@ module Takeout
195
160
  def extract_instance_variables_from_options(options)
196
161
  # Set instance variables
197
162
  @uri = options[:uri] || ''
198
- self.endpoints = options[:endpoints] || {}
199
163
  @headers = options[:headers] || {}
200
164
  @schemas = options[:schemas] || {}
201
165
  @debug = options[:debug]
@@ -211,5 +175,30 @@ module Takeout
211
175
  opts = port? ? {host: @uri, path: endpoint, port: port} : {host: @uri, path: endpoint}
212
176
  ssl? ? URI::HTTPS.build(opts) : URI::HTTP.build(opts)
213
177
  end
178
+
179
+ def method_missing(method_sym, *attributes, &block)
180
+ request_type = method_sym.to_s.scan(/^(?:get|post|put|delete|patch|update)/).first
181
+ request_name = method_sym.to_s.scan(/(?<=_{1}).*/).first
182
+ if (request_type && request_name)
183
+ self.define_singleton_method(method_sym) do |options={}, &block|
184
+ # Extract values that we store separately from the options hash and then clean it up
185
+ headers.merge!(options[:headers]) if options[:headers]
186
+
187
+ # Merge in global options
188
+ options.merge!(@options) if @options
189
+
190
+ # Build the request_url and update the options to remove templated values (if there are any)
191
+ request_url, options = generate_request_url(request_name, request_type, options)
192
+
193
+ # Clean up options hash before performing request
194
+ [:headers, :extension, :object_id, :endpoint].each { |value| options.delete(value)}
195
+
196
+ return perform_curl_request(request_type, request_url, options, headers)
197
+ end
198
+ self.send(method_sym, *attributes, &block)
199
+ else
200
+ super
201
+ end
202
+ end
214
203
  end
215
204
  end
@@ -1,9 +1,9 @@
1
1
  module Takeout
2
2
  class EndpointFailureError < StandardError
3
- attr_reader :object, :request_type, :request_url, :response_code
3
+ attr_reader :object, :request_type, :request_url, :response_code, :response
4
4
 
5
- def initialize(object, request_type)
6
- @object, @request_type = object, request_type
5
+ def initialize(object, request_type, response)
6
+ @object, @request_type, @response = object, request_type, response
7
7
  end
8
8
 
9
9
  def message
@@ -0,0 +1,28 @@
1
+ module Takeout
2
+ class Response
3
+ attr_accessor :headers
4
+ attr_accessor :body
5
+ attr_accessor :response
6
+ attr_accessor :options
7
+
8
+ def initialize(options={})
9
+ if block_given?
10
+ yield self
11
+ else
12
+ extract_instance_variables_from_options(options)
13
+ end
14
+ end
15
+
16
+ def extract_instance_variables_from_options(options)
17
+ # Set instance variables
18
+ @headers = options[:headers] || ''
19
+ @body = options[:body] || {}
20
+ @response = options[:response] || {}
21
+
22
+
23
+ # Clean instance variables out of options hash and set that as options instance variable
24
+ [:headers, :body, :response].each { |v| options.delete(v) }
25
+ @options = options
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module Takeout
2
- VERSION = "0.1.3"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/takeout.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'takeout/version'
2
2
  require 'takeout/endpoint_failure_error'
3
+ require 'takeout/response'
3
4
  require 'takeout/client.rb'
4
5
 
5
6
  module Takeout
data/spec/client_spec.rb CHANGED
@@ -4,14 +4,7 @@ describe Takeout::Client do
4
4
  let (:client) {Takeout::Client.new(uri: 'test.com', endpoints: {get: ENDPOINTS, post: ENDPOINTS, put: ENDPOINTS, delete: ENDPOINTS})}
5
5
 
6
6
  context 'initialization' do
7
- it 'creates methods for all provided endpoints' do
8
- expect((client.methods - Object.methods - Takeout::Client.instance_methods(false))).to eq([:get_posts, :get_fake_failure, :get_fake_missing, :get_fake_redirect,
9
- :post_posts, :post_fake_failure, :post_fake_missing, :post_fake_redirect,
10
- :put_posts, :put_fake_failure, :put_fake_missing, :put_fake_redirect,
11
- :delete_posts, :delete_fake_failure, :delete_fake_missing, :delete_fake_redirect])
12
- end
13
-
14
- it 'yeilds when block is given' do
7
+ it 'yields when block is given' do
15
8
  expect { |b| Takeout::Client.new(&b) }.to yield_with_args (Takeout::Client)
16
9
  end
17
10
  end
@@ -59,8 +52,12 @@ describe Takeout::Client do
59
52
  end
60
53
 
61
54
  context 'get' do
62
- it 'returns array on success' do
63
- expect(client.get_posts).to be_an(Array)
55
+ it 'returns Takeout::Response on success' do
56
+ expect(client.get_posts).to be_an(Takeout::Response)
57
+ end
58
+
59
+ it 'returns an array in its body on success' do
60
+ expect(client.get_posts.body).to be_an(Array)
64
61
  end
65
62
 
66
63
  it 'raises EndpointFailureError on missing' do
@@ -74,8 +71,12 @@ describe Takeout::Client do
74
71
  end
75
72
 
76
73
  context 'post' do
77
- it 'returns hash on success' do
78
- expect(client.post_posts).to be_an(Hash)
74
+ it 'returns Takeout::Response on success' do
75
+ expect(client.post_posts).to be_an(Takeout::Response)
76
+ end
77
+
78
+ it 'returns as hash in its body on success' do
79
+ expect(client.post_posts.body).to be_an(Hash)
79
80
  end
80
81
 
81
82
  it 'raises EndpointFailureError when called without object_id' do
@@ -93,8 +94,12 @@ describe Takeout::Client do
93
94
  end
94
95
 
95
96
  context 'delete' do
96
- it 'returns hash on success with object_id' do
97
- expect(client.delete_posts(object_id: 1)).to be_a(Hash)
97
+ it 'returns Takeout::Response on success' do
98
+ expect(client.delete_posts(object_id: 1)).to be_a(Takeout::Response)
99
+ end
100
+
101
+ it 'returns hash in its body on success with object_id' do
102
+ expect(client.delete_posts(object_id: 1).body).to be_a(Hash)
98
103
  end
99
104
 
100
105
  it 'raises EndpointFailureError when called without object_id' do
@@ -113,8 +118,12 @@ describe Takeout::Client do
113
118
  end
114
119
 
115
120
  context 'put' do
116
- it 'returns hash on success with object_id' do
117
- expect(client.put_posts(object_id: 1)).to be_a(Hash)
121
+ it 'returns Takeout::Response on success' do
122
+ expect(client.put_posts(object_id: 1)).to be_a(Takeout::Response)
123
+ end
124
+
125
+ it 'returns hash in its body on success with object_id' do
126
+ expect(client.put_posts(object_id: 1).body).to be_a(Hash)
118
127
  end
119
128
 
120
129
  it 'raises EndpointFailureError when called without object_id' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: takeout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Lucas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-24 00:00:00.000000000 Z
11
+ date: 2016-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -155,6 +155,7 @@ files:
155
155
  - lib/takeout.rb
156
156
  - lib/takeout/client.rb
157
157
  - lib/takeout/endpoint_failure_error.rb
158
+ - lib/takeout/response.rb
158
159
  - lib/takeout/version.rb
159
160
  - spec/.DS_Store
160
161
  - spec/client_spec.rb