heroics 0.0.16 → 0.0.17

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: 71ffe47375db83f6d799ab0fba948893c9e242b2
4
- data.tar.gz: b38fd2974615c491bebab27ef593a91769247af6
3
+ metadata.gz: db15e29f26b3dba405af8b01a48edcad2d2d7acd
4
+ data.tar.gz: 3854c7caff8cd64471b6b57b58c2ab807d971a48
5
5
  SHA512:
6
- metadata.gz: 5aae57f3c48275ac9d1c70008cdd64322e39ee44ce44adc3b0e88cf824fbf4904ab41547b0d423e7c42458b27cccbe521050532eec011ad677d0ea3a705d9ae1
7
- data.tar.gz: e0fc57b7e0e20fca7fd5ca5a1d069597c4f311183bfe440b7be33645342f0781e574808b4cca29cfb093f9a92b3393e8a89b49b247c8ecf0750ff6e16be1cbff
6
+ metadata.gz: a01a0b5e5f9662efd580ab1f4c869907c017baa43ca5faa0c0179c6593547d58a01fd33b4d285215420ebc01c0819f2ade58153368e0bfb304c9f268de6146f0
7
+ data.tar.gz: e47f14886442b6739220abb5fccd561db5e73885daab3b595c034ef6540a21e9265bf6837690f49cb6b964b953c27422c2794e8c84dc4ec1471c1cade5006208
@@ -18,10 +18,15 @@ module Heroics
18
18
  # @raise [NoMethodError] Raised if the name doesn't match a known resource.
19
19
  # @return [Resource] The resource matching the name.
20
20
  def method_missing(name)
21
- name = name.to_s.gsub('_', '-')
21
+ name = name.to_s
22
22
  resource = @resources[name]
23
23
  if resource.nil?
24
- raise NoMethodError.new("undefined method `#{name}' for #{to_s}")
24
+ # Try substituting underscores for dashes
25
+ name = name.to_s.gsub('_', '-')
26
+ resource = @resources[name]
27
+ if resource.nil?
28
+ raise NoMethodError.new("undefined method `#{name}' for #{to_s}")
29
+ end
25
30
  end
26
31
  resource
27
32
  end
@@ -80,8 +80,12 @@ module Heroics
80
80
  end
81
81
  end
82
82
 
83
- # The list of parameters to render in generated source code for the method
84
- # signature for the link.
83
+ # list of parameters for method signature, body is optional
84
+ def method_signature
85
+ @parameters.map { |info| info.name == 'body' ? "body = {}" : info.name }.join(', ')
86
+ end
87
+
88
+ # list of parameters to pass along from method signature to client calls
85
89
  def parameter_names
86
90
  @parameters.map { |info| info.name }.join(', ')
87
91
  end
@@ -48,14 +48,24 @@ module Heroics
48
48
  path = "#{@path_prefix}#{path}" unless @path_prefix == '/'
49
49
  headers = @default_headers
50
50
  if body
51
- headers = headers.merge({'Content-Type' => @link_schema.content_type})
52
- body = @link_schema.encode(body)
51
+ case @link_schema.method
52
+ when :put, :post, :patch
53
+ headers = headers.merge({'Content-Type' => @link_schema.content_type})
54
+ body = @link_schema.encode(body)
55
+ when :get, :delete
56
+ if body.is_a?(Hash)
57
+ query = body
58
+ else
59
+ query = MultiJson.load(body)
60
+ end
61
+ body = nil
62
+ end
53
63
  end
54
64
 
55
65
  connection = Excon.new(@root_url, thread_safe_sockets: true)
56
66
  response = request_with_cache(connection,
57
67
  method: @link_schema.method, path: path,
58
- headers: headers, body: body,
68
+ headers: headers, body: body, query: query,
59
69
  expects: [200, 201, 202, 204, 206])
60
70
  content_type = response.headers['Content-Type']
61
71
  if content_type && content_type =~ /application\/.*json/
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Heroics
3
- VERSION = '0.0.16'
3
+ VERSION = '0.0.17'
4
4
  end
@@ -11,19 +11,27 @@ require 'heroics'
11
11
  require 'uri'
12
12
 
13
13
  module <%= @module_name %>
14
- # Get a Client configured to use HTTP Basic authentication.
14
+ # Get a Client configured to use HTTP Basic or header-based authentication.
15
15
  #
16
16
  # @param api_key [String] The API key to use when connecting.
17
17
  # @param options [Hash<Symbol,String>] Optionally, custom settings
18
18
  # to use with the client. Allowed options are `default_headers`,
19
19
  # `cache`, `user` and `url`.
20
20
  # @return [Client] A client configured to use the API with HTTP Basic
21
- # authentication.
21
+ # or header-based authentication.
22
22
  def self.connect(api_key, options=nil)
23
23
  options = custom_options(options)
24
24
  uri = URI.parse(options[:url])
25
- uri.user = URI.encode_www_form_component options.fetch(:user, 'user')
26
- uri.password = api_key
25
+
26
+ if options[:user]
27
+ uri.user = URI.encode_www_form_component options[:user]
28
+ end
29
+
30
+ if api_key
31
+ uri.user ||= 'user'
32
+ uri.password = api_key
33
+ end
34
+
27
35
  client = Heroics.client_from_schema(SCHEMA, uri.to_s, options)
28
36
  Client.new(client)
29
37
  end
@@ -116,7 +124,7 @@ module <%= @module_name %>
116
124
  # @param <%= parameter.name %>: <%= parameter.description %>
117
125
  <% end %>
118
126
  <% end %>
119
- def <%= link.name %>(<%= link.parameter_names %>)
127
+ def <%= link.name %>(<%= link.method_signature %>)
120
128
  @client.<%= resource.name %>.<%= link.name %>(<%= link.parameter_names %>)
121
129
  end
122
130
  <% end %>
@@ -45,6 +45,23 @@ class ClientTest < MiniTest::Unit::TestCase
45
45
  assert_equal('Hello, world!', client.resource.link)
46
46
  end
47
47
 
48
+ # Client.<resource>.<link> finds the appropriate link and invokes it with
49
+ # query params, since it is a GET resource
50
+ def test_resource_with_query_param
51
+ schema = Heroics::Schema.new(SAMPLE_SCHEMA)
52
+ link = Heroics::Link.new('https://username:secret@example.com',
53
+ schema.resource('resource').link('list'))
54
+ resource = Heroics::Resource.new({'link' => link})
55
+ client = Heroics::Client.new({'resource' => resource},
56
+ 'http://example.com')
57
+ Excon.stub(method: :get) do |request|
58
+ assert_equal({:limit => '50', :page => '25'}, request[:query])
59
+ Excon.stubs.pop
60
+ {status: 200, body: 'Hello, world!'}
61
+ end
62
+ assert_equal('Hello, world!', client.resource.link(limit: '50', page: '25'))
63
+ end
64
+
48
65
  # Client converts underscores in resource method names to dashes to match
49
66
  # names specified in the schema.
50
67
  def test_resource_with_dashed_name
@@ -65,6 +82,27 @@ class ClientTest < MiniTest::Unit::TestCase
65
82
  end
66
83
  assert_equal('Hello, world!', client.another_resource.link)
67
84
  end
85
+
86
+ # Client.<resource>.<link> finds the appropriate link and invokes it, even if
87
+ # the resource has an underscore in its name
88
+ def test_resource_with_underscored_name
89
+ schema = Heroics::Schema.new(SAMPLE_SCHEMA)
90
+ link = Heroics::Link.new('https://username:secret@example.com',
91
+ schema.resource('underscored_resource').link('list'))
92
+ resource = Heroics::Resource.new({'link' => link})
93
+ client = Heroics::Client.new({'underscored_resource' => resource},
94
+ 'http://example.com')
95
+ Excon.stub(method: :get) do |request|
96
+ assert_equal('Basic dXNlcm5hbWU6c2VjcmV0',
97
+ request[:headers]['Authorization'])
98
+ assert_equal('example.com', request[:host])
99
+ assert_equal(443, request[:port])
100
+ assert_equal('/underscored_resource', request[:path])
101
+ Excon.stubs.pop
102
+ {status: 200, body: 'Hello, world!'}
103
+ end
104
+ assert_equal('Hello, world!', client.underscored_resource.link)
105
+ end
68
106
  end
69
107
 
70
108
  class ClientFromSchemaTest < MiniTest::Unit::TestCase
@@ -192,9 +192,30 @@ SAMPLE_SCHEMA = {
192
192
  'title' => 'List'}
193
193
  ]
194
194
  },
195
+
196
+ 'underscored_resource' => {
197
+ 'description' => 'Underscores the importance of supporting underscored resources',
198
+ 'id' => 'schema/underscored_resource',
199
+ '$schema' => 'http://json-schema.org/draft-04/hyper-schema',
200
+ 'title' => 'Another underscored resource to use in tests',
201
+ 'type' => ['object'],
202
+
203
+ 'definitions' => {},
204
+
205
+ 'properties' => {},
206
+
207
+ 'links' => [
208
+ {'description' => 'Show all underscored resources',
209
+ 'href' => '/underscored_resource',
210
+ 'method' => 'GET',
211
+ 'rel' => 'instances',
212
+ 'title' => 'List'}
213
+ ]
214
+ },
195
215
  },
196
216
  'properties' => {
197
217
  'resource' => { '$ref' => '#/definitions/resource' },
198
- 'another-resource' => { '$ref' => '#/definitions/another-resource' }
218
+ 'another-resource' => { '$ref' => '#/definitions/another-resource' },
219
+ 'underscored_resource' => { '$ref' => '#/definitions/underscored_resource' }
199
220
  }
200
221
  }
@@ -29,7 +29,7 @@ class SchemaTest < MiniTest::Unit::TestCase
29
29
  # Schema.resources returns a sequence of ResourceSchema children.
30
30
  def test_resources
31
31
  schema = Heroics::Schema.new(SAMPLE_SCHEMA)
32
- assert_equal(['resource', 'another-resource'],
32
+ assert_equal(['resource', 'another-resource', 'underscored_resource'],
33
33
  schema.resources.map(&:name))
34
34
  end
35
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heroics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - geemus
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-08 00:00:00.000000000 Z
12
+ date: 2016-06-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler