quick_api 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/Gemfile.lock +1 -1
 - data/lib/quick_api/mongoid.rb +151 -0
 - data/lib/quick_api/version.rb +1 -1
 - data/lib/quick_api.rb +9 -2
 - metadata +3 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: ab3a1a650429f8912473e4591a6467a2262fe3ec
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 1fdc64d1cfdd956ee63381aad946021033e781a0
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: aaf87fa52085b5394e51fcd877940ebd47da0d300dc1e5790fbd38fcec3714d15293d213cfc2c539f413b5e16e06aae79368c28162dc3a5ac5619d56bd3c6f94
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: fc54eae113d4a63837c6b77bb8031f482396296a61b54ca7c7ce93b75b4f29e56c1127bb1093d94de37e596654567402339e05cdef18464f356612cf10b95e48
         
     | 
    
        data/Gemfile.lock
    CHANGED
    
    
| 
         @@ -0,0 +1,151 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'quick_api'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module QuickApi
         
     | 
| 
      
 4 
     | 
    
         
            +
              module Mongoid
         
     | 
| 
      
 5 
     | 
    
         
            +
                extend ActiveSupport::Concern
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                included do
         
     | 
| 
      
 8 
     | 
    
         
            +
                  class_attribute :quick_api_attributes
         
     | 
| 
      
 9 
     | 
    
         
            +
                  # class_attribute :quick_api_methods
         
     | 
| 
      
 10 
     | 
    
         
            +
                  class_attribute :quick_api_has_many
         
     | 
| 
      
 11 
     | 
    
         
            +
                  class_attribute :quick_api_belongs_to
         
     | 
| 
      
 12 
     | 
    
         
            +
                  class_attribute :quick_api_has_one
         
     | 
| 
      
 13 
     | 
    
         
            +
                  class_attribute :quick_api_has_and_belongs_to_many
         
     | 
| 
      
 14 
     | 
    
         
            +
                  class_attribute :quick_api_embeds_many
         
     | 
| 
      
 15 
     | 
    
         
            +
                  class_attribute :quick_api_embedded_in
         
     | 
| 
      
 16 
     | 
    
         
            +
                  class_attribute :quick_api_embeds_one
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                module ClassMethods
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                  def quick_api_attrributes(*names)
         
     | 
| 
      
 22 
     | 
    
         
            +
                    self.quick_api_attributes = names
         
     | 
| 
      
 23 
     | 
    
         
            +
                  end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                  def quick_api_has_many(*relations)
         
     | 
| 
      
 26 
     | 
    
         
            +
                    self.quick_api_has_many = relations
         
     | 
| 
      
 27 
     | 
    
         
            +
                  end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                  def quick_api_belongs_to(*relations)
         
     | 
| 
      
 30 
     | 
    
         
            +
                    self.quick_api_belongs_to = relations
         
     | 
| 
      
 31 
     | 
    
         
            +
                  end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                  def quick_api_has_one(*relations)
         
     | 
| 
      
 34 
     | 
    
         
            +
                    self.quick_api_has_one = relations
         
     | 
| 
      
 35 
     | 
    
         
            +
                  end
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                  def quick_api_has_and_belongs_to_many(*relations)
         
     | 
| 
      
 38 
     | 
    
         
            +
                    self.quick_api_has_and_belongs_to_many = relations
         
     | 
| 
      
 39 
     | 
    
         
            +
                  end
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                  def quick_api_embeds_many(*relations)
         
     | 
| 
      
 42 
     | 
    
         
            +
                    self.quick_api_embeds_many = relations
         
     | 
| 
      
 43 
     | 
    
         
            +
                  end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                  def quick_api_embedded_in(*relations)
         
     | 
| 
      
 46 
     | 
    
         
            +
                    self.quick_api_embedded_in = relations
         
     | 
| 
      
 47 
     | 
    
         
            +
                  end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                  def quick_api_embeds_one(*relations)
         
     | 
| 
      
 50 
     | 
    
         
            +
                    self.quick_api_embeds_one = relations
         
     | 
| 
      
 51 
     | 
    
         
            +
                  end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                  # def quick_api_methods(*methods)
         
     | 
| 
      
 54 
     | 
    
         
            +
                  #   self.quick_api_methods = methods
         
     | 
| 
      
 55 
     | 
    
         
            +
                  # end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                end
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                def to_api(options = {relations: true}, result = {})
         
     | 
| 
      
 60 
     | 
    
         
            +
                  if options[:fields]
         
     | 
| 
      
 61 
     | 
    
         
            +
                    api_fields = options[:fields]
         
     | 
| 
      
 62 
     | 
    
         
            +
                  else
         
     | 
| 
      
 63 
     | 
    
         
            +
                    api_fields = self.quick_api_attributes.nil? ? [] : self.quick_api_attributes
         
     | 
| 
      
 64 
     | 
    
         
            +
                  end
         
     | 
| 
      
 65 
     | 
    
         
            +
                  api_fields.each do |api_field|
         
     | 
| 
      
 66 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 67 
     | 
    
         
            +
                      if (self.send(api_field)).class == Paperclip::Attachment
         
     | 
| 
      
 68 
     | 
    
         
            +
                        picture_styles = []
         
     | 
| 
      
 69 
     | 
    
         
            +
                        self.send(api_field).styles.each {|style| picture_styles << style[0]}
         
     | 
| 
      
 70 
     | 
    
         
            +
                        result[api_field] = {original: "http://#{ActionMailer::Base.default_url_options[:host]}#{self.send(api_field).url}",
         
     | 
| 
      
 71 
     | 
    
         
            +
                                             styles: picture_styles}
         
     | 
| 
      
 72 
     | 
    
         
            +
                      else
         
     | 
| 
      
 73 
     | 
    
         
            +
                        begin
         
     | 
| 
      
 74 
     | 
    
         
            +
                          result[api_field] = self.send(api_field)
         
     | 
| 
      
 75 
     | 
    
         
            +
                        rescue
         
     | 
| 
      
 76 
     | 
    
         
            +
                          raise "The field #{api_field} don't exist in this Model"
         
     | 
| 
      
 77 
     | 
    
         
            +
                        end
         
     | 
| 
      
 78 
     | 
    
         
            +
                      end
         
     | 
| 
      
 79 
     | 
    
         
            +
                    rescue
         
     | 
| 
      
 80 
     | 
    
         
            +
                      begin
         
     | 
| 
      
 81 
     | 
    
         
            +
                        result[api_field] = self.send(api_field)
         
     | 
| 
      
 82 
     | 
    
         
            +
                      rescue
         
     | 
| 
      
 83 
     | 
    
         
            +
                        raise "The field #{api_field} don't exist in this Model"
         
     | 
| 
      
 84 
     | 
    
         
            +
                      end
         
     | 
| 
      
 85 
     | 
    
         
            +
                    end
         
     | 
| 
      
 86 
     | 
    
         
            +
                  end
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
      
 88 
     | 
    
         
            +
                  # if self.quick_api_methods
         
     | 
| 
      
 89 
     | 
    
         
            +
                  #   result = api_method_options(result, self.quick_api_methods)
         
     | 
| 
      
 90 
     | 
    
         
            +
                  # end 
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
                  if options[:relations] == true
         
     | 
| 
      
 93 
     | 
    
         
            +
                      result = api_many_options(result, self.quick_api_has_many)                if self.quick_api_has_many
         
     | 
| 
      
 94 
     | 
    
         
            +
                      result = api_belongs_or_one_options(result, self.quick_api_belongs_to)    if self.quick_api_belongs_to
         
     | 
| 
      
 95 
     | 
    
         
            +
                      result = api_belongs_or_one_options(result, self.quick_api_has_one)       if self.quick_api_has_one
         
     | 
| 
      
 96 
     | 
    
         
            +
                      result = api_many_options(result, self.quick_api_has_and_belongs_to_many) if self.quick_api_has_and_belongs_to_many
         
     | 
| 
      
 97 
     | 
    
         
            +
                      result = api_many_options(result, self.quick_api_embeds_many)             if self.quick_api_embeds_many
         
     | 
| 
      
 98 
     | 
    
         
            +
                      result = api_belongs_or_one_options(result, self.quick_api_embedded_in)   if self.quick_api_embedded_in
         
     | 
| 
      
 99 
     | 
    
         
            +
                      result = api_belongs_or_one_options(result, self.quick_api_embeds_one)    if self.quick_api_embeds_one
         
     | 
| 
      
 100 
     | 
    
         
            +
                  end
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                  result = api_set_options(result, options)
         
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
      
 104 
     | 
    
         
            +
                  return result
         
     | 
| 
      
 105 
     | 
    
         
            +
                end
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
                private
         
     | 
| 
      
 108 
     | 
    
         
            +
             
     | 
| 
      
 109 
     | 
    
         
            +
                # def api_method_options(result, options)
         
     | 
| 
      
 110 
     | 
    
         
            +
                #   options.each do |method|
         
     | 
| 
      
 111 
     | 
    
         
            +
                #     #TODO doc
         
     | 
| 
      
 112 
     | 
    
         
            +
                #     result.merge! self.send(method)
         
     | 
| 
      
 113 
     | 
    
         
            +
                #   end
         
     | 
| 
      
 114 
     | 
    
         
            +
                #   return result 
         
     | 
| 
      
 115 
     | 
    
         
            +
                # end
         
     | 
| 
      
 116 
     | 
    
         
            +
             
     | 
| 
      
 117 
     | 
    
         
            +
                def api_set_options(result, options)
         
     | 
| 
      
 118 
     | 
    
         
            +
                  result = api_added(result, options[:added]) if options[:added]
         
     | 
| 
      
 119 
     | 
    
         
            +
                  return result
         
     | 
| 
      
 120 
     | 
    
         
            +
                end
         
     | 
| 
      
 121 
     | 
    
         
            +
             
     | 
| 
      
 122 
     | 
    
         
            +
                def api_many_options(result, many)
         
     | 
| 
      
 123 
     | 
    
         
            +
                  many.each do |model|
         
     | 
| 
      
 124 
     | 
    
         
            +
                    resources = eval(model.to_s.underscore.pluralize)
         
     | 
| 
      
 125 
     | 
    
         
            +
                    result_api_resources = []
         
     | 
| 
      
 126 
     | 
    
         
            +
                    resources.each do |resource|
         
     | 
| 
      
 127 
     | 
    
         
            +
                      result_api_resources << resource.to_api
         
     | 
| 
      
 128 
     | 
    
         
            +
                    end
         
     | 
| 
      
 129 
     | 
    
         
            +
                    result[model.to_s.underscore.pluralize] = result_api_resources
         
     | 
| 
      
 130 
     | 
    
         
            +
                  end
         
     | 
| 
      
 131 
     | 
    
         
            +
                  return result
         
     | 
| 
      
 132 
     | 
    
         
            +
                end
         
     | 
| 
      
 133 
     | 
    
         
            +
             
     | 
| 
      
 134 
     | 
    
         
            +
                def api_belongs_or_one_options(result, one)
         
     | 
| 
      
 135 
     | 
    
         
            +
                  one.each do |model|
         
     | 
| 
      
 136 
     | 
    
         
            +
                    resource = eval(model.to_s.singularize.camelize.underscore)
         
     | 
| 
      
 137 
     | 
    
         
            +
                    result[model.to_s.singularize.camelize.underscore] = resource.try(:to_api)
         
     | 
| 
      
 138 
     | 
    
         
            +
                  end
         
     | 
| 
      
 139 
     | 
    
         
            +
                  return result
         
     | 
| 
      
 140 
     | 
    
         
            +
                end
         
     | 
| 
      
 141 
     | 
    
         
            +
             
     | 
| 
      
 142 
     | 
    
         
            +
                def api_added(result, added)
         
     | 
| 
      
 143 
     | 
    
         
            +
                  added.each do |field|
         
     | 
| 
      
 144 
     | 
    
         
            +
                    field.each do |sub_field|
         
     | 
| 
      
 145 
     | 
    
         
            +
                      result[sub_field[0]] = sub_field[1] unless sub_field.empty?
         
     | 
| 
      
 146 
     | 
    
         
            +
                    end
         
     | 
| 
      
 147 
     | 
    
         
            +
                  end
         
     | 
| 
      
 148 
     | 
    
         
            +
                  return result
         
     | 
| 
      
 149 
     | 
    
         
            +
                end
         
     | 
| 
      
 150 
     | 
    
         
            +
              end
         
     | 
| 
      
 151 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/quick_api/version.rb
    CHANGED
    
    
    
        data/lib/quick_api.rb
    CHANGED
    
    | 
         @@ -1,5 +1,12 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            require  
     | 
| 
      
 1 
     | 
    
         
            +
            require 'active_support'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'quick_api/version'
         
     | 
| 
       2 
4 
     | 
    
         | 
| 
       3 
5 
     | 
    
         
             
            module QuickApi
         
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            # include in AR
         
     | 
| 
      
 10 
     | 
    
         
            +
            ActiveSupport.on_load(:active_record) do
         
     | 
| 
      
 11 
     | 
    
         
            +
              ActiveRecord::Base.send(:include, QuickApi)
         
     | 
| 
       5 
12 
     | 
    
         
             
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: quick_api
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.2
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - SternCode
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2014- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2014-05-16 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: bundler
         
     | 
| 
         @@ -53,6 +53,7 @@ files: 
     | 
|
| 
       53 
53 
     | 
    
         
             
            - Rakefile
         
     | 
| 
       54 
54 
     | 
    
         
             
            - lib/quick_api.rb
         
     | 
| 
       55 
55 
     | 
    
         
             
            - lib/quick_api/.DS_Store
         
     | 
| 
      
 56 
     | 
    
         
            +
            - lib/quick_api/mongoid.rb
         
     | 
| 
       56 
57 
     | 
    
         
             
            - lib/quick_api/version.rb
         
     | 
| 
       57 
58 
     | 
    
         
             
            - quick_api.gemspec
         
     | 
| 
       58 
59 
     | 
    
         
             
            homepage: ''
         
     |