From 630fbea2638fe5e5e14aafea14707969e782740b Mon Sep 17 00:00:00 2001 From: Todd Bealmear Date: Sun, 6 Mar 2016 17:34:44 -0800 Subject: [PATCH 1/5] :scissors: Remove Dependency on rest-client. Resolves #13. --- Gemfile.lock | 3 +- lib/opencomponents.rb | 3 +- lib/opencomponents/component.rb | 49 +++++++++++++------ lib/opencomponents/unrendered_component.rb | 2 +- opencomponents.gemspec | 1 - .../opencomponents/rendered_component_spec.rb | 9 +--- spec/lib/opencomponents/renderer_spec.rb | 4 +- .../unrendered_component_spec.rb | 14 +++--- 8 files changed, 47 insertions(+), 38 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index c39b3aa..5874ff6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,6 @@ PATH remote: . specs: opencomponents (0.5.0) - rest-client (~> 1.8) GEM remote: https://rubygems.org/ @@ -128,4 +127,4 @@ DEPENDENCIES webmock (~> 1.21) BUNDLED WITH - 1.10.3 + 1.11.2 diff --git a/lib/opencomponents.rb b/lib/opencomponents.rb index c98fe99..612c478 100644 --- a/lib/opencomponents.rb +++ b/lib/opencomponents.rb @@ -1,4 +1,5 @@ -require 'rest-client' +require 'json' +require 'net/http' require 'opencomponents/component' require 'opencomponents/rendered_component' diff --git a/lib/opencomponents/component.rb b/lib/opencomponents/component.rb index b7c9d51..c61f307 100644 --- a/lib/opencomponents/component.rb +++ b/lib/opencomponents/component.rb @@ -1,6 +1,8 @@ module OpenComponents # Wrapper object for a component fetched from an OC registry. class Component + REGISTRY_TIMEOUT_MESSAGE = 'The registry took too long to respond.'.freeze + # Public: Gets/sets the String name of the component. attr_accessor :name @@ -102,8 +104,10 @@ def load # Internal: Builds the URL to send a component request to. # # Returns the String URL to request a component from. - def url - File.join(OpenComponents.config.registry, name, version) + def uri + URI(File.join(OpenComponents.config.registry, name, version)).tap do |u| + u.query = URI.encode_www_form(params) + end end # Internal: Executes a component request against the configured registry. @@ -113,18 +117,29 @@ def url # 404. # Raises OpenComponents::RegistryTimeout if the request times out. def response - request_headers = headers.merge(params: params) - - RestClient::Request.execute( - method: :get, - url: url, - timeout: OpenComponents.config.timeout, - headers: request_headers - ) - rescue RestClient::ResourceNotFound => e - fail ComponentNotFound, e.message - rescue RestClient::RequestTimeout => e - fail RegistryTimeout, e.message + _response = Net::HTTP.start( + uri.host, + uri.port, + read_timeout: OpenComponents.config.timeout, + open_timeout: OpenComponents.config.timeout + ) { |http| http.request request } + + case _response + when Net::HTTPNotFound + fail ComponentNotFound, "Component #{name} not found." + when Net::HTTPRequestTimeOut + fail_with_timeout + end + + _response + rescue Timeout::Error + fail_with_timeout + end + + def request + Net::HTTP::Get.new(uri).tap do |r| + headers.each { |k, v| r[k] = v } + end end # Internal: Helper method for converting and memoizing registry response @@ -132,7 +147,11 @@ def response # # Returns a Hash of registry response data. def response_data - @_response_data ||= JSON.parse(response) + @_response_data ||= JSON.parse(response.body) + end + + def fail_with_timeout + fail RegistryTimeout, REGISTRY_TIMEOUT_MESSAGE end private diff --git a/lib/opencomponents/unrendered_component.rb b/lib/opencomponents/unrendered_component.rb index 4b2600f..386c4c9 100644 --- a/lib/opencomponents/unrendered_component.rb +++ b/lib/opencomponents/unrendered_component.rb @@ -2,7 +2,7 @@ module OpenComponents # Wrapper object for components using the `unrendered` rendering mode. class UnrenderedComponent < Component # Internal: Default HTTP headers to send when requesting a component. - DEFAULT_HEADERS = {accept: 'application/vnd.oc.unrendered+json'} + DEFAULT_HEADERS = {'Accept' => 'application/vnd.oc.unrendered+json'} # Public: Returns a Hash of data to use when rendering the component. attr_reader :data diff --git a/opencomponents.gemspec b/opencomponents.gemspec index 3808170..643d451 100644 --- a/opencomponents.gemspec +++ b/opencomponents.gemspec @@ -6,7 +6,6 @@ require 'opencomponents/version' Gem::Specification.new do |spec| spec.required_ruby_version = '>= 1.9.3' - spec.add_dependency 'rest-client', '~> 1.8' spec.add_development_dependency 'bundler', '~> 1.10' spec.authors = ['Todd Bealmear'] diff --git a/spec/lib/opencomponents/rendered_component_spec.rb b/spec/lib/opencomponents/rendered_component_spec.rb index 17d41bb..fd71a0c 100644 --- a/spec/lib/opencomponents/rendered_component_spec.rb +++ b/spec/lib/opencomponents/rendered_component_spec.rb @@ -7,7 +7,6 @@ before do stub_request(:get, "http://localhost:3030/foobar/"). with( - headers: {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}, query: {name: 'foobar'} ). to_return(status: 200, body: '{"href":"http://localhost:3030/foobar?name=foobar","type":"oc-component-local","version":"1.0.0","requestVersion":"","html":"

ohai, my name is foobar

","renderMode":"rendered"}', headers: {}) @@ -43,7 +42,6 @@ context 'without request params' do before do stub_request(:get, "http://localhost:3030/foobar/"). - with(headers: {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}). to_return(status: 200, body: '{"href":"http://localhost:3030/foobar","type":"oc-component-local","version":"1.0.0","requestVersion":"","html":"

ohai, my name is Todd

","renderMode":"rendered"}', headers: {}) end @@ -80,7 +78,6 @@ before do stub_request(:get, "http://localhost:3030/foobar/1.0.0"). with( - headers: {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}, query: {name: 'foobar'} ). to_return(status: 200, body: '{"href":"http://localhost:3030/foobar/1.0.0?name=foobar","type":"oc-component-local","version":"1.0.0","requestVersion":"1.0.0","html":"

ohai, my name is foobar

","renderMode":"rendered"}', headers: {}) @@ -116,8 +113,6 @@ context 'without request params' do before do stub_request(:get, "http://localhost:3030/foobar/1.0.0"). - with( - headers: {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}). to_return(status: 200, body: '{"href":"http://localhost:3030/foobar/1.0.0","type":"oc-component-local","version":"1.0.0","requestVersion":"1.0.0","html":"

ohai, my name is Todd

","renderMode":"rendered"}', headers: {}) end @@ -152,7 +147,6 @@ context 'for a missing component' do before do stub_request(:get, "http://localhost:3030/foo/"). - with(headers: {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}). to_return(status: 404, body: "", headers: {}) end @@ -178,7 +172,7 @@ context 'with custom HTTP headers' do let!(:stub) do stub_request(:get, "http://localhost:3030/foobar/"). - with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Accept-Language'=>'emoji', 'User-Agent'=>'Ruby'}). + with(:headers => {'Accept-Language'=>'emoji'}). to_return(:status => 200, :body => '{"href":"http://localhost:3030/foobar","type":"oc-component-local","version":"1.0.0","requestVersion":"","html":"

ohai, my name is Todd

","renderMode":"rendered"}', :headers => {}) end @@ -240,7 +234,6 @@ before do stub_request(:get, "http://localhost:3030/foobar/"). with( - headers: {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}, query: {name: 'foobar'} ). to_return(:status => 200, :body => '{"href":"http://foo.com/bar?name=foobar","type":"some-oc-type","version":"1.0.2","requestVersion":"","html":"
WE
","renderMode":"rendered"}', :headers => {}) diff --git a/spec/lib/opencomponents/renderer_spec.rb b/spec/lib/opencomponents/renderer_spec.rb index a976632..24cbcb0 100644 --- a/spec/lib/opencomponents/renderer_spec.rb +++ b/spec/lib/opencomponents/renderer_spec.rb @@ -8,12 +8,10 @@ class TestRenderer # :nodoc: describe '#render_component' do before do stub_request(:get, "http://localhost:3030/foobar/1.0.1"). - with(headers: {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}, - query: {name: 'foobar'}). + with(query: {name: 'foobar'}). to_return(status: 200, body: '{"href":"http://localhost:3030/foobar/1.0.1?name=foobar","type":"oc-component-local","version":"1.0.1","requestVersion":"1.0.1","html":"

ohai, my name is foobar

","renderMode":"rendered"}', headers: {}) stub_request(:get, "http://localhost:3030/barfoo/"). - with(headers: {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}). to_return(status: 404, body: "", headers: {}) end diff --git a/spec/lib/opencomponents/unrendered_component_spec.rb b/spec/lib/opencomponents/unrendered_component_spec.rb index cc3afd6..61d1403 100644 --- a/spec/lib/opencomponents/unrendered_component_spec.rb +++ b/spec/lib/opencomponents/unrendered_component_spec.rb @@ -7,7 +7,7 @@ before do stub_request(:get, "http://localhost:3030/foobar/"). with( - headers: {'Accept'=>'application/vnd.oc.unrendered+json', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}, + headers: {'Accept'=>'application/vnd.oc.unrendered+json'}, query: {name: 'foobar'} ). to_return(status: 200, body: '{"href":"http://localhost:3030/foobar?name=foobar","type":"oc-component-local","version":"1.0.0","requestVersion":"","data":{"name":"foobar"},"template":{"src":"http://localhost:3030/foobar/1.0.0/static/template.js","type":"jade","key":"0fe4b3fb2d6c0810f0d97a222a7e61eb91243bea"},"renderMode":"unrendered"}', headers: {}) @@ -52,7 +52,7 @@ before do stub_request(:get, "http://localhost:3030/foobar/"). with( - headers: {'Accept'=>'application/vnd.oc.unrendered+json', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}). + headers: {'Accept'=>'application/vnd.oc.unrendered+json'}). to_return(status: 200, body: '{"href":"http://localhost:3030/foobar","type":"oc-component-local","version":"1.0.0","requestVersion":"","data":{"name":"Todd"},"template":{"src":"http://localhost:3030/foobar/1.0.0/static/template.js","type":"jade","key":"0fe4b3fb2d6c0810f0d97a222a7e61eb91243bea"},"renderMode":"unrendered"}', headers: {}) end @@ -97,7 +97,7 @@ before do stub_request(:get, "http://localhost:3030/foobar/1.0.0"). with( - headers: {'Accept'=>'application/vnd.oc.unrendered+json', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}, + headers: {'Accept'=>'application/vnd.oc.unrendered+json'}, query: {name: 'foobar'} ). to_return(status: 200, body: '{"href":"http://localhost:3030/foobar/1.0.0?name=foobar","type":"oc-component-local","version":"1.0.0","requestVersion":"1.0.0","data":{"name":"foobar"},"template":{"src":"http://localhost:3030/foobar/1.0.0/static/template.js","type":"jade","key":"0fe4b3fb2d6c0810f0d97a222a7e61eb91243bea"},"renderMode":"unrendered"}', headers: {}) @@ -142,7 +142,7 @@ before do stub_request(:get, "http://localhost:3030/foobar/1.0.0"). with( - headers: {'Accept'=>'application/vnd.oc.unrendered+json', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}). + headers: {'Accept'=>'application/vnd.oc.unrendered+json'}). to_return(status: 200, body: '{"href":"http://localhost:3030/foobar/1.0.0","type":"oc-component-local","version":"1.0.0","requestVersion":"1.0.0","data":{"name":"Todd"},"template":{"src":"http://localhost:3030/foobar/1.0.0/static/template.js","type":"jade","key":"0fe4b3fb2d6c0810f0d97a222a7e61eb91243bea"},"renderMode":"unrendered"}', headers: {}) end @@ -185,7 +185,7 @@ context 'for a missing component' do before do stub_request(:get, "http://localhost:3030/foo/"). - with(headers: {'Accept'=>'application/vnd.oc.unrendered+json', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}). + with(headers: {'Accept'=>'application/vnd.oc.unrendered+json'}). to_return(status: 404, body: "", headers: {}) end @@ -211,7 +211,7 @@ context 'with custom HTTP headers' do let!(:stub) do stub_request(:get, "http://localhost:3030/foobar/"). - with(:headers => {'Accept'=>'application/vnd.oc.unrendered+json', 'Accept-Encoding'=>'gzip, deflate', 'Accept-Language'=>'emoji', 'User-Agent'=>'Ruby'}). + with(:headers => {'Accept'=>'application/vnd.oc.unrendered+json'}). to_return(:status => 200, :body => '{"href":"http://localhost:3030/foobar/1.0.0","type":"oc-component-local","version":"1.0.0","requestVersion":"","data":{"name":"Todd"},"template":{"src":"http://localhost:3030/foobar/1.0.0/static/template.js","type":"jade","key":"0fe4b3fb2d6c0810f0d97a222a7e61eb91243bea"},"renderMode":"unrendered"}', :headers => {}) end @@ -276,7 +276,7 @@ before do stub_request(:get, "http://localhost:3030/foobar/"). with( - headers: {'Accept'=>'application/vnd.oc.unrendered+json', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}, + headers: {'Accept'=>'application/vnd.oc.unrendered+json'}, query: {name: 'foobar'} ). to_return(status: 200, body: '{"href":"http://foo.com/bar?name=foobar","type":"some-oc-type","version":"1.0.2","requestVersion":"","data":{"name":"foobar"},"template":{"src":"http://foo.com/bar/1.0.2/static/template.js","type":"jade","key":"0fe4b3fb2d6c0810f0d97a222a7e61eb91243bea"},"renderMode":"unrendered"}', headers: {}) From 7abe26d71dde535dd7067ca18d2544cb6c62968c Mon Sep 17 00:00:00 2001 From: Todd Bealmear Date: Sun, 6 Mar 2016 18:47:47 -0800 Subject: [PATCH 2/5] :wrench: Build for Ruby 2.3.0 --- .travis.yml | 1 + lib/opencomponents/component.rb | 9 ++++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1172c1e..b62b3c5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,5 +4,6 @@ rvm: - 2.0.0 - 2.1.0 - 2.2.2 + - 2.3.0 bundler_args: --without development before_install: gem install bundler diff --git a/lib/opencomponents/component.rb b/lib/opencomponents/component.rb index c61f307..ed4f6d2 100644 --- a/lib/opencomponents/component.rb +++ b/lib/opencomponents/component.rb @@ -2,6 +2,7 @@ module OpenComponents # Wrapper object for a component fetched from an OC registry. class Component REGISTRY_TIMEOUT_MESSAGE = 'The registry took too long to respond.'.freeze + BLANK = ''.freeze # Public: Gets/sets the String name of the component. attr_accessor :name @@ -44,14 +45,14 @@ class Component def initialize(name, opts = {}) @name = name @params = opts[:params] || {} - @version = opts[:version] || '' + @version = opts[:version] || BLANK @headers = opts[:headers] || {} end # Public: Returns the String value of `requestVersion` from a component # response, `nil` if not present. def request_version - @request_version == '' ? nil : @request_version + @request_version == BLANK ? nil : @request_version end # Public: Resets all component attributes from a registry response to `nil`. @@ -84,7 +85,7 @@ def reload! load end - protected + private # Internal: Executes a component request and sets attributes common to all # component render modes. Public-facing `#load` methods exist on Component @@ -154,8 +155,6 @@ def fail_with_timeout fail RegistryTimeout, REGISTRY_TIMEOUT_MESSAGE end - private - # Internal: Whitelists instance variable names allowed to be reset when # calling `#flush!`. # From 23dd2dd4a74a5850915ed7f64b9b673a7b3a0138 Mon Sep 17 00:00:00 2001 From: Todd Bealmear Date: Sun, 6 Mar 2016 18:49:05 -0800 Subject: [PATCH 3/5] :memo: Doc Updates --- CHANGELOG.md | 12 ++++++ README.md | 6 +-- lib/opencomponents.rb | 2 +- lib/opencomponents/component.rb | 45 +++++++++++----------- lib/opencomponents/rendered_component.rb | 2 +- lib/opencomponents/renderer.rb | 13 +++---- lib/opencomponents/unrendered_component.rb | 11 +++--- 7 files changed, 49 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bd7f9d..4647345 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +## 1.0.0.pre + * Changes: + * Dropped `rest-client` dependency + * Request headers now use string keys instead of symbols. + ```ruby + # Old + { accept_language: 'en_us' } + + # New + { 'Accept-Language' => 'en_us' } + ``` + ## 0.5.0 * Changes: * OpenComponents::PrerenderedComponent is now OpenComponents::UnrenderedComponent diff --git a/README.md b/README.md index dc77d2e..f5e0a67 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,6 @@ [6]:https://github.com/opentable/oc -## Important -This gem is still under heavy development and the API is likely to change at any -time. - ## Getting Started Add the gem to your Gemfile and run `bundle install`: ```ruby @@ -48,7 +44,7 @@ component = OpenComponents::RenderedComponent.new( 'my-awesome-component', params: {name: 'Kate'}, version: '1.0.2', - headers: {accept_language: 'emoji'} + headers: {'Accept-Language' => 'emoji'} ) ``` diff --git a/lib/opencomponents.rb b/lib/opencomponents.rb index 612c478..550ebb0 100644 --- a/lib/opencomponents.rb +++ b/lib/opencomponents.rb @@ -15,7 +15,7 @@ module OpenComponents DEFAULT_TIMEOUT = 5 # Internal: Custom exception class to raise in the event a component cannot be - # found in the registry. + # found in the registry. ComponentNotFound = Class.new(RuntimeError) # Internal: Custom exception class to raise for response timeouts. diff --git a/lib/opencomponents/component.rb b/lib/opencomponents/component.rb index ed4f6d2..4e6e0e6 100644 --- a/lib/opencomponents/component.rb +++ b/lib/opencomponents/component.rb @@ -1,8 +1,8 @@ module OpenComponents # Wrapper object for a component fetched from an OC registry. class Component - REGISTRY_TIMEOUT_MESSAGE = 'The registry took too long to respond.'.freeze - BLANK = ''.freeze + REGISTRY_TIMEOUT_MESSAGE = 'The registry took too long to respond.'.freeze #:nodoc: + BLANK = ''.freeze #:nodoc: # Public: Gets/sets the String name of the component. attr_accessor :name @@ -16,32 +16,31 @@ class Component # Public: Gets/sets a Hash of headers to send in the component request. attr_accessor :headers - # Public: Returns the String URL of the requested component. + # Public: Gets the String URL of the requested component. attr_reader :href - # Public: Returns the String version of the component as served by the - # registry. + # Public: Gets the String version of the component as served by the + # registry. attr_reader :registry_version - # Public: Returns the String type of the component as served by the - # registry. + # Public: Gets the String type of the component as served by the + # registry. attr_reader :type - # Public: Returns the String render mode of the component as served by the - # registry. Generally either `rendered` or `unrendered`. + # Public: Gets the String render mode of the component as served by the + # registry. Generally either `rendered` or `unrendered`. attr_reader :render_mode # Public: Initializes a new Component subclass. # # name - The String name of the component to request. - # opts - A Hash of options to use when requesting the component - # (default: {}). + # opts - A Hash of options to use when requesting the component (default: {}): # :params - A Hash of parameters to send in the component request - # (optional, default: {}). + # (optional, default: {}). # :version - The String version of the component to request - # (optional, default: ''). - # :headers - A Hash of HTTP request headers to include in the - # component request (optional, default: {}). + # (optional, default: ''). + # :headers - A Hash of HTTP request headers to include in the component + # request (optional, default: {}). def initialize(name, opts = {}) @name = name @params = opts[:params] || {} @@ -50,7 +49,7 @@ def initialize(name, opts = {}) end # Public: Returns the String value of `requestVersion` from a component - # response, `nil` if not present. + # response, `nil` if not present. def request_version @request_version == BLANK ? nil : @request_version end @@ -72,7 +71,7 @@ def flush! end # Public: Resets all component attributes and reloads them from a registry - # response. + # response. # # Examples # @@ -88,8 +87,8 @@ def reload! private # Internal: Executes a component request and sets attributes common to all - # component render modes. Public-facing `#load` methods exist on Component - # subclasses. + # component render modes. Public-facing `#load` methods exist on Component + # subclasses. # # Returns the Component. def load @@ -115,7 +114,7 @@ def uri # # Returns a response body String. # Raises OpenComponents::ComponentNotFound if the registry responds with a - # 404. + # 404. # Raises OpenComponents::RegistryTimeout if the request times out. def response _response = Net::HTTP.start( @@ -137,6 +136,8 @@ def response fail_with_timeout end + # Internal: Helper method for building the component request. Includes + # all params and headers. def request Net::HTTP::Get.new(uri).tap do |r| headers.each { |k, v| r[k] = v } @@ -144,7 +145,7 @@ def request end # Internal: Helper method for converting and memoizing registry response - # data. + # data. # # Returns a Hash of registry response data. def response_data @@ -156,7 +157,7 @@ def fail_with_timeout end # Internal: Whitelists instance variable names allowed to be reset when - # calling `#flush!`. + # calling `#flush!`. # # Returns an Array of instance variable Symbols allowed to be reset. def flush_variables_whitelist diff --git a/lib/opencomponents/rendered_component.rb b/lib/opencomponents/rendered_component.rb index f0b0ad3..23c66b2 100644 --- a/lib/opencomponents/rendered_component.rb +++ b/lib/opencomponents/rendered_component.rb @@ -5,7 +5,7 @@ class RenderedComponent < Component attr_reader :html # Public: Executes a request for the Component against the configured - # registry and sets the component attributes. + # registry and sets the component attributes. # # Examples # diff --git a/lib/opencomponents/renderer.rb b/lib/opencomponents/renderer.rb index 8cc6bee..4eee22e 100644 --- a/lib/opencomponents/renderer.rb +++ b/lib/opencomponents/renderer.rb @@ -2,17 +2,16 @@ module OpenComponents # Provides rendering helper methods for components. module Renderer # Public: Builds a new RenderedComponent, executes a request for it against - # the configured registry, and returns the rendered component HTML. + # the configured registry, and returns the rendered component HTML. # # component - The String name of the component to render. - # opts - A Hash of options to use when requesting the component - # (default: {}). + # opts - A Hash of options to use when requesting the component (default: {}): # :params - A Hash of parameters to send in the component request - # (optional, default: {}). + # (optional, default: {}). # :version - The String version of the component to request - # (optional, default: nil). - # :headers - A Hash of HTTP request headers to include in the - # component request (optional, default: {}). + # (optional, default: nil). + # :headers - A Hash of HTTP request headers to include in the component + # request (optional, default: {}). def render_component(component, opts = {}) OpenComponents::RenderedComponent.new(component, opts) .load diff --git a/lib/opencomponents/unrendered_component.rb b/lib/opencomponents/unrendered_component.rb index 386c4c9..0ba93ad 100644 --- a/lib/opencomponents/unrendered_component.rb +++ b/lib/opencomponents/unrendered_component.rb @@ -13,14 +13,13 @@ class UnrenderedComponent < Component # Public: Initializes a new UnrenderedComponent. # # name - The String name of the component to request. - # opts - A Hash of options to use when requesting the component - # (default: {}). + # opts - A Hash of options to use when requesting the component (default: {}): # :params - A Hash of parameters to send in the component request - # (optional, default: {}). + # (optional, default: {}). # :version - The String version of the component to request - # (optional, default: nil). + # (optional, default: nil). # :headers - A Hash of HTTP request headers to include in the - # component request (optional, default: DEFAULT_HEADERS). + # component request (optional, default: DEFAULT_HEADERS). def initialize(name, opts = {}) super(name, opts) @@ -28,7 +27,7 @@ def initialize(name, opts = {}) end # Public: Executes a request for the Component against the configured - # registry and sets the component attributes. + # registry and sets the component attributes. # # Examples # From d40cc8bf56bb58cb42eae933c68475414a8dcf9e Mon Sep 17 00:00:00 2001 From: Todd Bealmear Date: Sun, 6 Mar 2016 18:58:32 -0800 Subject: [PATCH 4/5] :hocho: Drop Support for Ruby 1.9.3 --- .travis.yml | 1 - CHANGELOG.md | 3 ++- Gemfile.lock | 2 +- lib/opencomponents/component.rb | 20 +++++++++---------- lib/opencomponents/renderer.rb | 23 +++++++++++----------- lib/opencomponents/unrendered_component.rb | 16 +++++++-------- lib/opencomponents/version.rb | 2 +- opencomponents.gemspec | 2 +- 8 files changed, 33 insertions(+), 36 deletions(-) diff --git a/.travis.yml b/.travis.yml index b62b3c5..335a18d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: ruby rvm: - - 1.9.3 - 2.0.0 - 2.1.0 - 2.2.2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 4647345..c1b42f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 1.0.0.pre * Changes: - * Dropped `rest-client` dependency + * Dropped support for Ruby 1.9.3. + * Dropped `rest-client` dependency. * Request headers now use string keys instead of symbols. ```ruby # Old diff --git a/Gemfile.lock b/Gemfile.lock index 5874ff6..326ae56 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - opencomponents (0.5.0) + opencomponents (1.0.0.pre) GEM remote: https://rubygems.org/ diff --git a/lib/opencomponents/component.rb b/lib/opencomponents/component.rb index 4e6e0e6..fcffc53 100644 --- a/lib/opencomponents/component.rb +++ b/lib/opencomponents/component.rb @@ -34,18 +34,16 @@ class Component # Public: Initializes a new Component subclass. # # name - The String name of the component to request. - # opts - A Hash of options to use when requesting the component (default: {}): - # :params - A Hash of parameters to send in the component request - # (optional, default: {}). - # :version - The String version of the component to request - # (optional, default: ''). - # :headers - A Hash of HTTP request headers to include in the component - # request (optional, default: {}). - def initialize(name, opts = {}) + # + # Options + # params - A Hash of parameters to send in the component request (default: {}). + # version - The String version of the component to request (default: ''). + # headers - A Hash of HTTP request headers to include in the request (default: {}). + def initialize(name, params: {}, version: nil, headers: {}) @name = name - @params = opts[:params] || {} - @version = opts[:version] || BLANK - @headers = opts[:headers] || {} + @params = params + @version = version || BLANK + @headers = headers end # Public: Returns the String value of `requestVersion` from a component diff --git a/lib/opencomponents/renderer.rb b/lib/opencomponents/renderer.rb index 4eee22e..aeef5f4 100644 --- a/lib/opencomponents/renderer.rb +++ b/lib/opencomponents/renderer.rb @@ -5,17 +5,18 @@ module Renderer # the configured registry, and returns the rendered component HTML. # # component - The String name of the component to render. - # opts - A Hash of options to use when requesting the component (default: {}): - # :params - A Hash of parameters to send in the component request - # (optional, default: {}). - # :version - The String version of the component to request - # (optional, default: nil). - # :headers - A Hash of HTTP request headers to include in the component - # request (optional, default: {}). - def render_component(component, opts = {}) - OpenComponents::RenderedComponent.new(component, opts) - .load - .html + # + # Options + # params - A Hash of parameters to send in the component request (default: {}). + # version - The String version of the component to request (default: nil). + # headers - A Hash of HTTP request headers to include in the request (default: {}). + def render_component(component, params: {}, version: nil, headers: {}) + OpenComponents::RenderedComponent.new( + component, + params: params, + version: version, + headers: headers + ).load.html end end end diff --git a/lib/opencomponents/unrendered_component.rb b/lib/opencomponents/unrendered_component.rb index 0ba93ad..4e20466 100644 --- a/lib/opencomponents/unrendered_component.rb +++ b/lib/opencomponents/unrendered_component.rb @@ -13,15 +13,13 @@ class UnrenderedComponent < Component # Public: Initializes a new UnrenderedComponent. # # name - The String name of the component to request. - # opts - A Hash of options to use when requesting the component (default: {}): - # :params - A Hash of parameters to send in the component request - # (optional, default: {}). - # :version - The String version of the component to request - # (optional, default: nil). - # :headers - A Hash of HTTP request headers to include in the - # component request (optional, default: DEFAULT_HEADERS). - def initialize(name, opts = {}) - super(name, opts) + # + # Options + # params - A Hash of parameters to send in the component request (default: {}). + # version - The String version of the component to request (default: nil). + # headers - A Hash of HTTP request headers to include in the request (default: DEFAULT_HEADERS). + def initialize(name, params: {}, version: nil, headers: {}) + super @headers.merge!(DEFAULT_HEADERS) end diff --git a/lib/opencomponents/version.rb b/lib/opencomponents/version.rb index 5cb5591..76bf50b 100644 --- a/lib/opencomponents/version.rb +++ b/lib/opencomponents/version.rb @@ -1,3 +1,3 @@ module OpenComponents - VERSION = '0.5.0' # :nodoc: + VERSION = '1.0.0.pre'.freeze # :nodoc: end diff --git a/opencomponents.gemspec b/opencomponents.gemspec index 643d451..b2acac1 100644 --- a/opencomponents.gemspec +++ b/opencomponents.gemspec @@ -4,7 +4,7 @@ $:.unshift File.expand_path('../lib', __FILE__) require 'opencomponents/version' Gem::Specification.new do |spec| - spec.required_ruby_version = '>= 1.9.3' + spec.required_ruby_version = '>= 2.0.0' spec.add_development_dependency 'bundler', '~> 1.10' From dea55787489eb66e6272d89e38ce701d7f310c83 Mon Sep 17 00:00:00 2001 From: Todd Bealmear Date: Sun, 6 Mar 2016 19:37:15 -0800 Subject: [PATCH 5/5] :muscle: Fix Coverage Regression --- .../opencomponents/rendered_component_spec.rb | 23 +++++++++++++++---- .../unrendered_component_spec.rb | 23 +++++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/spec/lib/opencomponents/rendered_component_spec.rb b/spec/lib/opencomponents/rendered_component_spec.rb index fd71a0c..9fcd6d8 100644 --- a/spec/lib/opencomponents/rendered_component_spec.rb +++ b/spec/lib/opencomponents/rendered_component_spec.rb @@ -158,14 +158,27 @@ end context 'for a registry timeout' do - before do - stub_request(:get, "http://localhost:3030/foo/").to_timeout + subject { described_class.new('foo').load } + + context 'HTTP 408' do + before do + stub_request(:get, "http://localhost:3030/foo/"). + to_return(status: 408, body: "", headers: {}) + end + + it 'raises an exception' do + expect { subject }.to raise_exception(OpenComponents::RegistryTimeout) + end end - subject { described_class.new('foo').load } + context 'Connection Timeout' do + before do + stub_request(:get, "http://localhost:3030/foo/").to_timeout + end - it 'raises an exception' do - expect { subject }.to raise_exception(OpenComponents::RegistryTimeout) + it 'raises an exception' do + expect { subject }.to raise_exception(OpenComponents::RegistryTimeout) + end end end diff --git a/spec/lib/opencomponents/unrendered_component_spec.rb b/spec/lib/opencomponents/unrendered_component_spec.rb index 61d1403..ef0552c 100644 --- a/spec/lib/opencomponents/unrendered_component_spec.rb +++ b/spec/lib/opencomponents/unrendered_component_spec.rb @@ -197,14 +197,27 @@ end context 'for a registry timeout' do - before do - stub_request(:get, "http://localhost:3030/foo/").to_timeout + subject { described_class.new('foo').load } + + context 'HTTP 408' do + before do + stub_request(:get, "http://localhost:3030/foo/"). + to_return(status: 408, body: "", headers: {}) + end + + it 'raises an exception' do + expect { subject }.to raise_exception(OpenComponents::RegistryTimeout) + end end - subject { described_class.new('foo').load } + context 'Connection Timeout' do + before do + stub_request(:get, "http://localhost:3030/foo/").to_timeout + end - it 'raises an exception' do - expect { subject }.to raise_exception(OpenComponents::RegistryTimeout) + it 'raises an exception' do + expect { subject }.to raise_exception(OpenComponents::RegistryTimeout) + end end end