diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7e82330..27c2f7c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,11 +1,24 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2018-02-27 16:14:20 +1300 using RuboCop version 0.52.1. +# on 2019-01-30 09:43:47 +1300 using RuboCop version 0.52.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. +# Offense count: 2 +# Cop supports --auto-correct. +Layout/ElseAlignment: + Exclude: + - 'lib/supplejack_common/request.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: Width, IgnoredPatterns. +Layout/IndentationWidth: + Exclude: + - 'lib/supplejack_common/request.rb' + # Offense count: 3 Lint/AmbiguousOperator: Exclude: @@ -21,6 +34,14 @@ Lint/AssignmentInCondition: - 'lib/supplejack_common/paginated_collection.rb' - 'lib/supplejack_common/validations/size.rb' +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleAlignWith, AutoCorrect. +# SupportedStylesAlignWith: keyword, variable, start_of_line +Lint/EndAlignment: + Exclude: + - 'lib/supplejack_common/request.rb' + # Offense count: 2 Lint/ShadowingOuterLocalVariable: Exclude: @@ -40,32 +61,37 @@ Lint/UselessAssignment: - 'spec/supplejack_common/resources/file_resource_spec.rb' - 'spec/supplejack_common/sitemap/paginated_collection_spec.rb' -# Offense count: 14 +# Offense count: 21 Metrics/AbcSize: - Max: 33 + Max: 40 -# Offense count: 37 +# Offense count: 46 # Configuration parameters: CountComments, ExcludedMethods. Metrics/BlockLength: Max: 226 -# Offense count: 2 +# Offense count: 3 # Configuration parameters: CountComments. Metrics/ClassLength: Max: 135 -# Offense count: 1 +# Offense count: 2 Metrics/CyclomaticComplexity: Max: 7 -# Offense count: 16 +# Offense count: 20 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 32 + Max: 33 # Offense count: 2 +# Configuration parameters: CountKeywordArgs. +Metrics/ParameterLists: + Max: 6 + +# Offense count: 4 Metrics/PerceivedComplexity: - Max: 8 + Max: 9 # Offense count: 3 Naming/AccessorMethodName: @@ -85,26 +111,18 @@ Naming/VariableNumber: Exclude: - 'spec/supplejack_common/sitemap/paginated_collection_spec.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AutoCorrect. -Performance/HashEachMethods: - Exclude: - - 'lib/supplejack_common/enrichments/enrichment.rb' - # Offense count: 1 Security/Eval: Exclude: - '.pryrc' -# Offense count: 2 +# Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: AutoCorrect, EnforcedStyle. # SupportedStyles: nested, compact Style/ClassAndModuleChildren: Exclude: - 'lib/supplejack_common/parser/supplejack_common.rb' - - 'spec/supplejack_common/tmp/json/europeana.rb' # Offense count: 2 Style/DateTime: @@ -112,7 +130,7 @@ Style/DateTime: - 'lib/supplejack_common/modifiers/date_parser.rb' - 'spec/supplejack_common/modifiers/date_parser_spec.rb' -# Offense count: 42 +# Offense count: 44 Style/Documentation: Enabled: false @@ -128,7 +146,7 @@ Style/DoubleNegation: Style/FormatStringToken: EnforcedStyle: unannotated -# Offense count: 7 +# Offense count: 6 # Configuration parameters: MinBodyLength. Style/GuardClause: Exclude: @@ -136,7 +154,6 @@ Style/GuardClause: - 'lib/supplejack_common/loader.rb' - 'lib/supplejack_common/modifiers/date_parser.rb' - 'lib/supplejack_common/oai/paginated_collection.rb' - - 'lib/supplejack_common/paginated_collection.rb' - 'lib/supplejack_common/validations/exclusion.rb' - 'lib/supplejack_common/validations/inclusion.rb' @@ -172,6 +189,11 @@ Style/ModuleFunction: Exclude: - 'lib/supplejack_common/utils.rb' +# Offense count: 2 +Style/OptionalArguments: + Exclude: + - 'lib/supplejack_common/oai/client.rb' + # Offense count: 2 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, AllowInnerSlashes. @@ -181,7 +203,7 @@ Style/RegexpLiteral: - 'lib/supplejack_common/json/base.rb' - 'spec/supplejack_common/integrations/parsers/json_parser.rb' -# Offense count: 196 +# Offense count: 293 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https Metrics/LineLength: diff --git a/Gemfile.lock b/Gemfile.lock index 2f7cab6..ff9bb6c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,9 +2,11 @@ PATH remote: . specs: supplejack_common (0.0.2) + actioncable actionpack activesupport chronic (<= 0.10.2) + coderay dimensions htmlentities json (~> 1.8.3) @@ -22,44 +24,48 @@ PATH GEM remote: https://rubygems.org/ specs: - actionpack (5.1.5) - actionview (= 5.1.5) - activesupport (= 5.1.5) + actioncable (5.2.2) + actionpack (= 5.2.2) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionpack (5.2.2) + actionview (= 5.2.2) + activesupport (= 5.2.2) rack (~> 2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.1.5) - activesupport (= 5.1.5) + actionview (5.2.2) + activesupport (= 5.2.2) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - activemodel (5.1.5) - activesupport (= 5.1.5) - activesupport (5.1.5) + activemodel (5.2.2) + activesupport (= 5.2.2) + activesupport (5.2.2) concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (~> 0.7) + i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) addressable (2.5.2) public_suffix (>= 2.0.2, < 4.0) ast (2.4.0) - bson (4.3.0) + bson (4.4.2) builder (3.2.3) byebug (10.0.2) chronic (0.10.2) coderay (1.1.2) - concurrent-ruby (1.0.5) + concurrent-ruby (1.1.4) crack (0.4.3) safe_yaml (~> 1.0.0) crass (1.0.3) diff-lcs (1.1.3) dimensions (1.3.0) docile (1.1.5) - domain_name (0.5.20170404) + domain_name (0.5.20180417) unf (>= 0.0.5, < 1.0.0) - erubi (1.7.0) + erubi (1.8.0) faraday (0.14.0) multipart-post (>= 1.2, < 3) faraday_middleware (0.12.2) @@ -68,7 +74,7 @@ GEM htmlentities (4.3.4) http-cookie (1.0.3) domain_name (~> 0.5) - i18n (0.9.5) + i18n (1.5.3) concurrent-ruby (~> 1.0) json (1.8.6) jsonpath (0.5.8) @@ -77,21 +83,22 @@ GEM crass (~> 1.0.2) nokogiri (>= 1.5.9) method_source (0.9.0) - mime-types (3.1) + mime-types (3.2.2) mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) - mimemagic (0.3.2) + mime-types-data (3.2018.0812) + mimemagic (0.3.3) mini_portile2 (2.3.0) minitest (5.11.3) mock_redis (0.17.3) - mongo (2.5.1) + mongo (2.6.4) bson (>= 4.3.0, < 5.0.0) - mongoid (6.3.0) - activemodel (~> 5.1) - mongo (>= 2.5.0, < 3.0.0) + mongoid (7.0.2) + activemodel (>= 5.1, < 6.0.0) + mongo (>= 2.5.1, < 3.0.0) multi_json (1.13.1) multipart-post (2.0.0) netrc (0.11.0) + nio4r (2.3.1) nokogiri (1.8.2) mini_portile2 (~> 2.3.0) nokogumbo (1.5.0) @@ -111,8 +118,8 @@ GEM byebug (~> 10.0) pry (~> 0.10) public_suffix (3.0.2) - rack (2.0.4) - rack-test (0.8.3) + rack (2.0.6) + rack-test (1.1.0) rack (>= 1.0, < 3) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) @@ -121,12 +128,12 @@ GEM loofah (~> 2.0) rainbow (3.0.0) rake (10.5.0) - redis (4.0.1) + redis (4.1.0) rest-client (2.0.2) http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 4.0) netrc (~> 0.8) - retriable (3.1.1) + retriable (3.1.2) rspec (2.11.0) rspec-core (~> 2.11.0) rspec-expectations (~> 2.11.0) @@ -144,7 +151,7 @@ GEM unicode-display_width (~> 1.0, >= 1.0.1) ruby-progressbar (1.9.0) safe_yaml (1.0.4) - sanitize (4.6.0) + sanitize (4.6.6) crass (~> 1.0.2) nokogiri (>= 1.4.4) nokogumbo (~> 1.4) @@ -164,6 +171,9 @@ GEM addressable (>= 2.3.6) crack (>= 0.3.2) hashdiff + websocket-driver (0.7.0) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.3) PLATFORMS ruby diff --git a/lib/supplejack_common/base.rb b/lib/supplejack_common/base.rb index 0d20186..994aa0b 100644 --- a/lib/supplejack_common/base.rb +++ b/lib/supplejack_common/base.rb @@ -138,6 +138,7 @@ def deletable? def rejected? return false if self.class.rejection_rules.nil? + self.class.rejection_rules.any? do |r| instance_eval(&r) end diff --git a/lib/supplejack_common/enrichments/enrichment.rb b/lib/supplejack_common/enrichments/enrichment.rb index 5a13edd..cba0833 100644 --- a/lib/supplejack_common/enrichments/enrichment.rb +++ b/lib/supplejack_common/enrichments/enrichment.rb @@ -66,7 +66,6 @@ def attribute(name, options = {}, &block) _attribute_definitions[name][:block] = block if block_given? end - # rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/CyclomaticComplexity def resource resource_class = "SupplejackCommon::#{_format.to_s.capitalize}Resource".constantize @@ -80,7 +79,6 @@ def resource options[:proxy] = _proxy if _proxy.present? @resource ||= resource_class.new(_url, options) end - # rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/CyclomaticComplexity def set_attribute_values @@ -97,7 +95,7 @@ def set_attribute_values end def enrichable? - _required_attributes.each do |_attribute, value| + _required_attributes.each_value do |value| return false if value.nil? end diff --git a/lib/supplejack_common/json/base.rb b/lib/supplejack_common/json/base.rb index 46cd8c1..aebd7a6 100644 --- a/lib/supplejack_common/json/base.rb +++ b/lib/supplejack_common/json/base.rb @@ -16,12 +16,12 @@ def record_selector(path) self._record_selector = path end - def document(url) + def document(url, channel_options) if url.include?('scroll') - self._document = SupplejackCommon::Request.scroll(url, _request_timeout, _throttle, _http_headers) + self._document = SupplejackCommon::Request.scroll(url, _request_timeout, _throttle, _http_headers, channel_options) _document elsif url =~ /^https?/ - self._document = SupplejackCommon::Request.get(url, _request_timeout, _throttle, _http_headers, _proxy) + self._document = SupplejackCommon::Request.get(url, _request_timeout, _throttle, _http_headers, _proxy, channel_options) _document elsif url =~ /^file/ File.read(url.gsub(/file:\/\//, '')) @@ -36,8 +36,8 @@ def total_results(total_selector) JsonPath.on(_document, total_selector).try(:first).to_f end - def records_json(url) - new_document = document(url) + def records_json(url, channel_options = {}) + new_document = document(url, channel_options) return [] if new_document.code == 204 records = JsonPath.on(new_document, _record_selector).try(:first) @@ -45,8 +45,8 @@ def records_json(url) records end - def fetch_records(url) - records_json(url).map { |attributes| new(attributes) } + def fetch_records(url, channel_options = {}) + records_json(url, channel_options).map { |attributes| new(attributes) } end def records(options = {}) diff --git a/lib/supplejack_common/oai/base.rb b/lib/supplejack_common/oai/base.rb index 71a9246..98caf69 100644 --- a/lib/supplejack_common/oai/base.rb +++ b/lib/supplejack_common/oai/base.rb @@ -18,14 +18,23 @@ class Base < SupplejackCommon::Base class_attribute :_set self._set = {} + class_attribute :_channel_options + self._channel_options = {} + class << self attr_reader :response def client - @client ||= OAI::Client.new(base_urls.first, {}, _proxy) + @client ||= OAI::Client.new(base_urls.first, {}, _proxy, _channel_options) end def records(options = {}) + self._channel_options = { + user_id: options[:user_id], + parser_id: options[:parser_id], + environment: options[:environment] + } + options = options.keep_if { |key| VALID_RECORDS_OPTIONS.include?(key) } options[:metadata_prefix] = get_metadata_prefix if get_metadata_prefix.present? options[:set] = get_set if get_set.present? diff --git a/lib/supplejack_common/oai/client.rb b/lib/supplejack_common/oai/client.rb index 5c5b6df..cbde9a0 100644 --- a/lib/supplejack_common/oai/client.rb +++ b/lib/supplejack_common/oai/client.rb @@ -6,10 +6,13 @@ module OAI # lib/supplejack_common/client.rb class Client - def initialize(base_url, options = {}, proxy = nil) + attr_reader :channel_options + + def initialize(base_url, options = {}, proxy = nil, channel_options) @base = URI.parse base_url @debug = options.fetch(:debug, false) @parser = options.fetch(:parser, 'rexml') + @channel_options = channel_options @http_client = options.fetch(:http) do Faraday.new(url: @base.clone, proxy: proxy) do |builder| @@ -46,7 +49,22 @@ def initialize(base_url, options = {}, proxy = nil) end def get(uri) + if defined?(ActionCable) + ActionCable.server.broadcast( + "#{channel_options[:environment]}_channel_#{channel_options[:parser_id]}_#{channel_options[:user_id]}", + status_log: "Requesting URL: #{uri}" + ) + end + response = @http_client.get("?#{uri.query}") + + if defined?(ActionCable) + ActionCable.server.broadcast( + "#{channel_options[:environment]}_channel_#{channel_options[:parser_id]}_#{channel_options[:user_id]}", + status_log: ::CodeRay.scan(response.body, :xml).html(line_numbers: :table).html_safe + ) + end + response.body end end diff --git a/lib/supplejack_common/oai/paginated_collection.rb b/lib/supplejack_common/oai/paginated_collection.rb index f373178..3965972 100644 --- a/lib/supplejack_common/oai/paginated_collection.rb +++ b/lib/supplejack_common/oai/paginated_collection.rb @@ -5,7 +5,7 @@ module Oai class PaginatedCollection < SupplejackCommon::PaginatedCollection include Enumerable - attr_reader :client, :options, :klass, :limit + attr_reader :client, :options, :klass, :limit, :channel_options def initialize(client, options, klass) @client = client @@ -13,6 +13,12 @@ def initialize(client, options, klass) @options = options @klass = klass @counter = 0 + + @channel_options = { + user_id: options[:user_id], + parser_id: options[:parser_id], + environment: options[:environment] + } end def each diff --git a/lib/supplejack_common/paginated_collection.rb b/lib/supplejack_common/paginated_collection.rb index 3c40b19..92ced76 100644 --- a/lib/supplejack_common/paginated_collection.rb +++ b/lib/supplejack_common/paginated_collection.rb @@ -7,7 +7,7 @@ class PaginatedCollection attr_reader :klass, :options - attr_reader :page_parameter, :per_page_parameter, :per_page, :page, :counter + attr_reader :page_parameter, :per_page_parameter, :per_page, :page, :counter, :channel_options def initialize(klass, pagination_options = {}, options = {}) @klass = klass @@ -22,13 +22,19 @@ def initialize(klass, pagination_options = {}, options = {}) @total_selector = pagination_options[:total_selector] @initial_param = pagination_options[:initial_param] + @channel_options = { + user_id: options[:user_id], + parser_id: options[:parser_id], + environment: options[:environment] + } + @options = options @counter = 0 end def each(&block) klass.base_urls.each do |base_url| - @records = klass.fetch_records(next_url(base_url)) + @records = klass.fetch_records(next_url(base_url), channel_options) return nil unless yield_from_records(&block) @@ -36,7 +42,7 @@ def each(&block) while more_results? @records.clear - @records = klass.fetch_records(next_url(base_url)) + @records = klass.fetch_records(next_url(base_url), channel_options) return nil unless yield_from_records(&block) end @@ -137,6 +143,11 @@ def yield_from_records record.set_attribute_values if record.rejected? + ActionCable.server.broadcast( + "#{channel_options[:environment]}_channel_#{channel_options[:parser_id]}_#{channel_options[:user_id]}", + status_log: "Record with title #{record.title.first} has been rejected due to failing reject_if conditions in the parser." + ) + next else @counter += 1 diff --git a/lib/supplejack_common/request.rb b/lib/supplejack_common/request.rb index 18a77e6..42cb9cd 100644 --- a/lib/supplejack_common/request.rb +++ b/lib/supplejack_common/request.rb @@ -7,8 +7,8 @@ module SupplejackCommon # SJ Request class class Request class << self - def get(url, request_timeout, options = [], headers = {}, proxy = nil) - new(url, request_timeout, options, headers, proxy).get + def get(url, request_timeout, options = [], headers = {}, proxy = nil, channel_options = {}) + new(url, request_timeout, options, headers, proxy, channel_options).get end def scroll(url, request_timeout, options = [], headers = {}) @@ -16,9 +16,9 @@ def scroll(url, request_timeout, options = [], headers = {}) end end - attr_accessor :url, :throttling_options, :request_timeout, :headers, :proxy + attr_accessor :url, :throttling_options, :request_timeout, :headers, :proxy, :channel_options - def initialize(url, request_timeout, options = [], headers = {}, proxy = nil) + def initialize(url, request_timeout, options = [], headers = {}, proxy = nil, channel_options = {}) @url = URI.escape(URI.unescape(url)) options ||= [] @@ -28,6 +28,7 @@ def initialize(url, request_timeout, options = [], headers = {}, proxy = nil) @request_timeout = request_timeout || 60_000 @headers = headers @proxy = proxy + @channel_options = channel_options end def uri @@ -93,6 +94,29 @@ def delay def request_url ::Retriable.retriable(tries: 5, base_interval: 1, multiplier: 2) do ::Sidekiq.logger.info "Retrying RestClient request #{url}" if defined?(Sidekiq) + if defined?(ActionCable) + ::ActionCable.server.broadcast( + "#{channel_options[:environment]}_channel_#{channel_options[:parser_id]}_#{channel_options[:user_id]}", + status_log: "Requesting URL: #{url}" + ) + end + + ::Sidekiq.logger.info "Requesting URL: #{url}, #{channel_options[:environment]}_channel_#{channel_options[:parser_id]}_#{channel_options[:user_id]}" if defined?(Sidekiq) + + if headers.present? && defined?(ActionCable) + ActionCable.server.broadcast( + "#{channel_options[:environment]}_channel_#{channel_options[:parser_id]}_#{channel_options[:user_id]}", + status_log: "This URL is being requested with the following headers: #{headers}" + ) + end + + if proxy && defined?(ActionCable) + ActionCable.server.broadcast( + "#{channel_options[:environment]}_channel_#{channel_options[:parser_id]}_#{channel_options[:user_id]}", + status_log: "This url is being requested through the following proxy: #{proxy}" + ) + end + RestClient::Request.execute( method: :get, url: url, @@ -111,6 +135,7 @@ def request_resource response = if defined?(Rails) && ::SupplejackCommon.caching_enabled Rails.cache.fetch(url, expires_in: 10.minutes) { request_url } else + request_url end end @@ -120,6 +145,21 @@ def request_resource Sidekiq.logger.info "GET (#{real_time}): #{url}, started #{start_time.utc.iso8601}" end + content_type = if response.headers[:content_type].include? 'xml' + :xml + elsif response.headers[:content_type].include? 'html' + :html + else + :json + end + + if defined?(ActionCable) + ActionCable.server.broadcast( + "#{channel_options[:environment]}_channel_#{channel_options[:parser_id]}_#{channel_options[:user_id]}", + status_log: CodeRay.scan(response.body, content_type).html(line_numbers: :table).html_safe + ) + end + response end end diff --git a/lib/supplejack_common/rss/base.rb b/lib/supplejack_common/rss/base.rb index e01422b..b43b0d1 100644 --- a/lib/supplejack_common/rss/base.rb +++ b/lib/supplejack_common/rss/base.rb @@ -19,14 +19,14 @@ def records(options = {}) SupplejackCommon::PaginatedCollection.new(self, {}, options) end - def fetch_records(url) - document = index_document(url) + def fetch_records(url, channel_options = {}) + document = index_document(url, channel_options) xml_nodes = document.xpath(_record_selector, _namespaces) xml_nodes.map { |node| new(node) } end - def index_document(url) - xml = SupplejackCommon::Request.get(url, _request_timeout, _throttle, _http_headers, _proxy) + def index_document(url, channel_options) + xml = SupplejackCommon::Request.get(url, _request_timeout, _throttle, _http_headers, _proxy, channel_options) Nokogiri::XML.parse(xml) end end diff --git a/lib/supplejack_common/xml/base.rb b/lib/supplejack_common/xml/base.rb index b18e5e6..4e61e4e 100644 --- a/lib/supplejack_common/xml/base.rb +++ b/lib/supplejack_common/xml/base.rb @@ -30,8 +30,8 @@ def records(options = {}) klass.new(self, pagination_options, options) end - def fetch_records(url = nil) - xml_records(url) + def fetch_records(url = nil, channel_options = {}) + xml_records(url, channel_options) end def record_format(format) diff --git a/lib/supplejack_common/xml_helpers/xml_document_methods.rb b/lib/supplejack_common/xml_helpers/xml_document_methods.rb index 2def58a..f9f9608 100644 --- a/lib/supplejack_common/xml_helpers/xml_document_methods.rb +++ b/lib/supplejack_common/xml_helpers/xml_document_methods.rb @@ -11,9 +11,9 @@ module XmlDocumentMethods end module ClassMethods - def xml_records(url) + def xml_records(url, channel_options = {}) xml_nodes = [] - with_each_file(url) do |file| + with_each_file(url, channel_options) do |file| document = parse_document(file) self._document = document xml_nodes += document.xpath(_record_selector, _namespaces).map { |node| new(node, url) } @@ -34,9 +34,9 @@ def xml_records(url) # For single xml files and external urls, this will only be one file # For tar.gz this will yield once for each file in the tar # - def with_each_file(url) + def with_each_file(url, channel_options = {}) if url =~ /^https?/ - yield SupplejackCommon::Request.get(url, _request_timeout, _throttle, _http_headers, _proxy) + yield SupplejackCommon::Request.get(url, _request_timeout, _throttle, _http_headers, _proxy, channel_options) elsif url =~ /^file/ url = url.gsub(%r{file:\/\/}, '') diff --git a/spec/supplejack_common/integrations/json_base_spec.rb b/spec/supplejack_common/integrations/json_base_spec.rb index 0166431..0fcdfe8 100644 --- a/spec/supplejack_common/integrations/json_base_spec.rb +++ b/spec/supplejack_common/integrations/json_base_spec.rb @@ -7,7 +7,7 @@ describe SupplejackCommon::Json::Base do before do json = File.read('spec/supplejack_common/integrations/source_data/json_records.json') - stub_request(:get, 'http://api.europeana.eu/records.json').to_return(status: 200, body: json) + stub_request(:get, 'http://api.europeana.eu/records.json').to_return(status: 200, body: json, headers: { content_type: 'json' }) end let!(:record) { JsonParser.records.first } diff --git a/spec/supplejack_common/integrations/xml_base_spec.rb b/spec/supplejack_common/integrations/xml_base_spec.rb index 3c2a0da..53b7c50 100644 --- a/spec/supplejack_common/integrations/xml_base_spec.rb +++ b/spec/supplejack_common/integrations/xml_base_spec.rb @@ -7,7 +7,7 @@ describe SupplejackCommon::Xml::Base do before do xml = File.read('spec/supplejack_common/integrations/source_data/xml_parser_records.xml') - stub_request(:get, 'http://digitalnz.org/xml').to_return(status: 200, body: xml) + stub_request(:get, 'http://digitalnz.org/xml').to_return(status: 200, body: xml, headers: { content_type: 'xml' }) end let!(:record) { XmlParser.records.first } diff --git a/spec/supplejack_common/integrations/xml_sitemap_spec.rb b/spec/supplejack_common/integrations/xml_sitemap_spec.rb index 4b1a277..164aa44 100644 --- a/spec/supplejack_common/integrations/xml_sitemap_spec.rb +++ b/spec/supplejack_common/integrations/xml_sitemap_spec.rb @@ -7,10 +7,10 @@ describe SupplejackCommon::Xml::Base do before do urls_xml = File.read('spec/supplejack_common/integrations/source_data/xml_sitemap_parser_urls.xml') - stub_request(:get, 'http://www.nzonscreen.com/api/title/').to_return(status: 200, body: urls_xml) + stub_request(:get, 'http://www.nzonscreen.com/api/title/').to_return(status: 200, body: urls_xml, headers: { content_type: 'xml' }) record_xml = File.read('spec/supplejack_common/integrations/source_data/xml_sitemap_parser_record.xml') - stub_request(:get, 'http://www.nzonscreen.com/api/title/weekly-review-no-395-1949').to_return(status: 200, body: record_xml) + stub_request(:get, 'http://www.nzonscreen.com/api/title/weekly-review-no-395-1949').to_return(status: 200, body: record_xml, headers: { content_type: 'xml' }) end let!(:record) { XmlSitemapParser.records.first } diff --git a/spec/supplejack_common/json/base_spec.rb b/spec/supplejack_common/json/base_spec.rb index 3d82d60..77e7873 100644 --- a/spec/supplejack_common/json/base_spec.rb +++ b/spec/supplejack_common/json/base_spec.rb @@ -54,16 +54,16 @@ klass._throttle = {} klass.http_headers('Authorization': 'Token token="token"', 'x-api-key': 'gus') klass._request_timeout = 60_000 - SupplejackCommon::Request.should_receive(:get).with('http://google.com', 60_000, {}, { 'Authorization': 'Token token="token"', 'x-api-key': 'gus' }, nil) { json } - klass.document('http://google.com').should eq json + SupplejackCommon::Request.should_receive(:get).with('http://google.com', 60_000, {}, { 'Authorization': 'Token token="token"', 'x-api-key': 'gus' }, nil, {}) { json } + klass.document('http://google.com', {}).should eq json end it 'stores json document at _document class attribute' do klass._throttle = {} klass.http_headers('Authorization': 'Token token="token"', 'x-api-key': 'gus') klass._request_timeout = 60_000 - SupplejackCommon::Request.should_receive(:get).with('http://google.com', 60_000, {}, { 'Authorization': 'Token token="token"', 'x-api-key': 'gus' }, nil) { json } - klass.document('http://google.com') + SupplejackCommon::Request.should_receive(:get).with('http://google.com', 60_000, {}, { 'Authorization': 'Token token="token"', 'x-api-key': 'gus' }, nil, {}) { json } + klass.document('http://google.com', {}) expect(klass._document).to equal json end end @@ -71,7 +71,7 @@ context 'json files' do it 'stores the raw json' do File.should_receive(:read).with('file:///data/sites/data.json'.gsub(%r{file:\/\/}, '')) { json } - klass.document('file:///data/sites/data.json').should eq json + klass.document('file:///data/sites/data.json', {}).should eq json end end @@ -80,8 +80,8 @@ klass._throttle = {} klass.http_headers('x-api-key': 'key') klass._request_timeout = 60_000 - SupplejackCommon::Request.should_receive(:scroll).with('http://google.com/_scroll', 60_000, {}, 'x-api-key': 'key') { json } - klass.document('http://google.com/_scroll') + SupplejackCommon::Request.should_receive(:scroll).with('http://google.com/_scroll', 60_000, {}, { 'x-api-key': 'key' }, {}) { json } + klass.document('http://google.com/_scroll', {}) expect(klass._document).to eq json end @@ -89,8 +89,8 @@ klass._throttle = {} klass.http_headers('x-api-key': 'key') klass._request_timeout = 60_000 - SupplejackCommon::Request.should_receive(:scroll).with('http://google.com/scroll', 60_000, {}, 'x-api-key': 'key') { json } - klass.document('http://google.com/scroll') + SupplejackCommon::Request.should_receive(:scroll).with('http://google.com/scroll', 60_000, {}, { 'x-api-key': 'key' }, {}) { json } + klass.document('http://google.com/scroll', {}) expect(klass._document).to eq json end end @@ -102,8 +102,8 @@ klass._throttle = {} klass.http_headers('Authorization' => 'Token token="token"', 'x-api-key' => 'gus') klass._request_timeout = 60_000 - SupplejackCommon::Request.should_receive(:get).with('http://google.com', 60_000, {}, { 'Authorization' => 'Token token="token"', 'x-api-key' => 'gus' }, nil).and_return { json } - klass.document('http://google.com') + SupplejackCommon::Request.should_receive(:get).with('http://google.com', 60_000, {}, { 'Authorization' => 'Token token="token"', 'x-api-key' => 'gus' }, nil, {}).and_return { json } + klass.document('http://google.com', {}) expect(klass.total_results('$.total_results_selector')).to eq 500.0 end end @@ -114,8 +114,8 @@ klass._throttle = {} klass.http_headers('Authorization' => 'Token token="token"', 'x-api-key' => 'gus') klass._request_timeout = 60_000 - SupplejackCommon::Request.should_receive(:get).with('http://google.com', 60_000, {}, { 'Authorization' => 'Token token="token"', 'x-api-key' => 'gus' }, nil).and_return { json } - klass.document('http://google.com') + SupplejackCommon::Request.should_receive(:get).with('http://google.com', 60_000, {}, { 'Authorization' => 'Token token="token"', 'x-api-key' => 'gus' }, nil, {}).and_return { json } + klass.document('http://google.com', {}) expect(klass.next_page_token('$.your_next_page')).to eq '1234' end end diff --git a/spec/supplejack_common/oai/base_spec.rb b/spec/supplejack_common/oai/base_spec.rb index fc643ea..ff9af08 100644 --- a/spec/supplejack_common/oai/base_spec.rb +++ b/spec/supplejack_common/oai/base_spec.rb @@ -19,7 +19,7 @@ describe '.client' do it 'initializes a new OAI client' do klass.base_url 'http://google.com' - OAI::Client.should_receive(:new).with('http://google.com', {}, nil) + OAI::Client.should_receive(:new).with('http://google.com', {}, nil, {}) klass.client end end diff --git a/spec/supplejack_common/paginated_collection_spec.rb b/spec/supplejack_common/paginated_collection_spec.rb index c43633b..1e96044 100644 --- a/spec/supplejack_common/paginated_collection_spec.rb +++ b/spec/supplejack_common/paginated_collection_spec.rb @@ -32,8 +32,8 @@ end it 'should process all base_urls' do - SupplejackCommon::Base.should_receive(:fetch_records).with('http://go.gle/') - SupplejackCommon::Base.should_receive(:fetch_records).with('http://dnz.harvest/1') + SupplejackCommon::Base.should_receive(:fetch_records).with('http://go.gle/', user_id: nil, parser_id: nil, environment: nil) + SupplejackCommon::Base.should_receive(:fetch_records).with('http://dnz.harvest/1', user_id: nil, parser_id: nil, environment: nil) collection.each { ; } end @@ -46,8 +46,8 @@ end it 'should call fetch records with a paginated url' do - SupplejackCommon::Base.should_receive(:fetch_records).with('http://go.gle/?page=1&per_page=10') - SupplejackCommon::Base.should_receive(:fetch_records).with('http://dnz.harvest/1?page=1&per_page=10') + SupplejackCommon::Base.should_receive(:fetch_records).with('http://go.gle/?page=1&per_page=10', user_id: nil, parser_id: nil, environment: nil) + SupplejackCommon::Base.should_receive(:fetch_records).with('http://dnz.harvest/1?page=1&per_page=10', user_id: nil, parser_id: nil, environment: nil) collection.each { ; } end end diff --git a/spec/supplejack_common/request_spec.rb b/spec/supplejack_common/request_spec.rb index c0a1206..9283aa6 100644 --- a/spec/supplejack_common/request_spec.rb +++ b/spec/supplejack_common/request_spec.rb @@ -4,10 +4,10 @@ describe SupplejackCommon::Request do let!(:klass) { SupplejackCommon::Request } - let!(:request) { klass.new('http://google.com/titles/1', 10_000) } + let!(:request) { klass.new('http://google.com/titles/1', 10_000, {}, content_type: 'hello') } before(:each) do - RestClient::Request.stub(:execute) { 'body' } + RestClient::Request.stub(:execute) { request } end describe '.get' do @@ -16,7 +16,7 @@ end it 'initializes a request object' do - klass.should_receive(:new).with('google.com', nil, [{ delay: 1 }], {}, nil) { request } + klass.should_receive(:new).with('google.com', nil, [{ delay: 1 }], {}, nil, {}) { request } klass.get('google.com', nil, [{ delay: 1 }]) end @@ -171,11 +171,4 @@ request_obj.request_url end end - - describe 'request_resource' do - it 'should request the resource and return it' do - request.should_receive(:request_url) { 'body' } - request.request_resource.should eq 'body' - end - end end diff --git a/spec/supplejack_common/xml/base_spec.rb b/spec/supplejack_common/xml/base_spec.rb index bc62fb3..fba9c90 100644 --- a/spec/supplejack_common/xml/base_spec.rb +++ b/spec/supplejack_common/xml/base_spec.rb @@ -52,7 +52,7 @@ describe '.fetch_records' do it 'initializes a set of xml records' do - klass.should_receive(:xml_records).with(nil) { [] } + klass.should_receive(:xml_records).with(nil, {}) { [] } klass.fetch_records end end diff --git a/spec/supplejack_common/xml_helpers/xml_document_methods_spec.rb b/spec/supplejack_common/xml_helpers/xml_document_methods_spec.rb index 409079c..ac2bc8e 100644 --- a/spec/supplejack_common/xml_helpers/xml_document_methods_spec.rb +++ b/spec/supplejack_common/xml_helpers/xml_document_methods_spec.rb @@ -45,7 +45,7 @@ context 'url is a url' do it 'gets the url and yields it' do - SupplejackCommon::Request.should_receive(:get).with('http://google.co.nz', 60_000, anything, anything, anything) { file } + SupplejackCommon::Request.should_receive(:get).with('http://google.co.nz', 60_000, anything, anything, anything, anything) { file } expect { |b| klass.send(:with_each_file, 'http://google.co.nz', &b) }.to yield_with_args(file) end end diff --git a/supplejack_common.gemspec b/supplejack_common.gemspec index 3b0881b..517d29a 100644 --- a/supplejack_common.gemspec +++ b/supplejack_common.gemspec @@ -19,6 +19,7 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency 'actionpack' gem.add_runtime_dependency 'activesupport' gem.add_runtime_dependency 'chronic', '<= 0.10.2' + gem.add_runtime_dependency 'coderay' gem.add_runtime_dependency 'dimensions' gem.add_runtime_dependency 'htmlentities' gem.add_runtime_dependency 'json', '~> 1.8.3' @@ -36,4 +37,5 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency 'sanitize' gem.add_runtime_dependency 'tzinfo' gem.add_development_dependency 'webmock', '~> 1.8' + gem.add_runtime_dependency 'actioncable' end