[SCM] ci-tooling packaging branch, master, updated. 4de4d8bdaa6227172c82f77030cd65c24550005a

Harald Sitter apachelogger-guest at moszumanska.debian.org
Mon Dec 7 13:09:17 UTC 2015


Gitweb-URL: http://git.debian.org/?p=pkg-kde/ci-tooling.git;a=commitdiff;h=4de4d8b

The following commit has been merged in the master branch:
commit 4de4d8bdaa6227172c82f77030cd65c24550005a
Author: Harald Sitter <sitter at kde.org>
Date:   Mon Dec 7 14:09:15 2015 +0100

    refactor method_missing into multiple partial methods
---
 lib/lp.rb | 94 +++++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 56 insertions(+), 38 deletions(-)

diff --git a/lib/lp.rb b/lib/lp.rb
index 4ccff25..d31f8b9 100644
--- a/lib/lp.rb
+++ b/lib/lp.rb
@@ -176,59 +176,77 @@ module Launchpad
   #    accessor.
   class Rubber < OpenStruct
     # @!visibility private
+    def method_missing_link(name)
+      data = Launchpad.get(URI(send("#{name}_link")))
+      Rubber.from_json(data)
+    end
+
+    # @!visibility private
+    def method_missing_collection_link(name)
+      ret = []
+      uri = URI(send("#{name}_collection_link"))
+      loop do
+        obj = Rubber.from_json(Launchpad.get(uri))
+        ret += obj.entries
+        return ret unless obj['next_collection_link']
+        uri = URI(obj.next_collection_link)
+      end
+    end
+
+    # @!visibility private
+    def build_params(name, params = {})
+      params ||= {}
+      params['ws.op'] = name.to_s.chomp('!')
+      params.each do |key, value|
+        next unless value.respond_to?(:fetch) && value.fetch('self_link', false)
+        params[key] = value['self_link']
+      end
+      params
+    end
+
+    # @!visibility private
+    def http_get(uri)
+      ret = []
+      loop do
+        obj = Rubber.from_json(Launchpad.get(uri))
+        return obj unless obj['entries']
+        ret += obj.entries
+        return ret unless obj['next_collection_link']
+        uri = URI(obj.next_collection_link)
+      end
+      nil
+    end
+
+    def method_missing_valid?(*args)
+      return true if args.empty?
+      args.size == 1 && args[0].is_a?(Hash)
+    end
+
+    # @!visibility private
     def method_missing(name, *args, &_block)
+      super unless method_missing_valid?(*args)
+
       # Pointer to different URL reflecting a different object.
-      if self["#{name}_link"]
-        data = Launchpad.get(URI(send("#{name}_link")))
-        return Rubber.from_json(data)
-      end
+      return method_missing_link(name) if self["#{name}_link"]
 
       # Pointer to a different URL reflecting a collection of things.
       if self["#{name}_collection_link"]
-        ret = []
-        uri = URI(send("#{name}_collection_link"))
-        loop do
-          obj = Rubber.from_json(Launchpad.get(uri))
-          ret += obj.entries
-          return ret unless obj['next_collection_link']
-          uri = URI(obj.next_collection_link)
-        end
+        return method_missing_collection_link(name)
       end
 
       # Build parameters.
       # For convenience reasons passing a parameter object that itself
       # responds to self_link will result in its self_link being passed.
-      params = args[0]
-      params ||= {}
-      params['ws.op'] = name.to_s.chomp('!')
-      params.each do |key, value|
-        begin
-          next unless value['self_link']
-        rescue
-          next
-        end
-        params[key] = value['self_link']
-      end
+      params = build_params(name, args[0])
 
       uri = URI(self['self_link'])
       uri.query = URI.encode_www_form(params)
 
       # Try to call as a 'function' on the API
-      if name.to_s.end_with?('!') # foo! causes a post
-        return Launchpad.post(uri)
-      else # foo causes a get
-        ret = []
-        loop do
-          obj = Rubber.from_json(Launchpad.get(uri))
-          if obj['entries']
-            ret += obj.entries
-          else
-            return obj
-          end
-          return ret unless obj['next_collection_link']
-          uri = URI(obj.next_collection_link)
-        end
-      end
+      #   foo! causes a POST
+      #   else we GET
+      return Launchpad.post(uri) if name.to_s.end_with?('!')
+      http_get(uri)
     end
 
     # Construct a {Rubber} from a JSON string.

-- 
ci-tooling packaging



More information about the pkg-kde-commits mailing list