Sha256: de6975e5cc9f7955c7429fb9fb1e1c426aa07ced12222f8692cc1dd0e40eac8f

Contents?: true

Size: 1.92 KB

Versions: 20

Compression:

Stored size: 1.92 KB

Contents

module Adparlor
  module Facebook
    module GraphApi
      module Fields
        module FieldDecorator
          GLOBAL_FIELDS = [:account_id, :conn, :files, :message, :source]

          def self.included(base)
            base.extend(ClassMethods)
          end

          module ClassMethods
            def fields(*args)
              if args.first == :all
                @fields.map(&:to_s).join(',')
              else
                invalid_fields = args.map(&:to_s) - @fields.map(&:to_s)
                invalid_fields_error(invalid_fields) if invalid_fields.any?
                (@fields & args).map(&:to_s).join(',')
              end
            end

            def validate_initialize_fields(*args)
              create_fields = self.const_defined?('CREATE_FIELDS') ? const_get('CREATE_FIELDS') : []
              invalid_fields = args.map(&:to_s) - ((@fields || []) + GLOBAL_FIELDS + create_fields).map(&:to_s)
              invalid_fields_error(invalid_fields) if invalid_fields.any?
            end

            def validate_post_fields(*args, method)
              valid_fields = self.const_defined?('CREATE_FIELDS') ? const_get('CREATE_FIELDS') : [] if method == 'CREATE'
              valid_fields = self.const_defined?('UPDATE_FIELDS') ? const_get('UPDATE_FIELDS') : [] if method == 'UPDATE' || method == 'DELETE'
              invalid_fields = args.map(&:to_s) - (@fields + GLOBAL_FIELDS + valid_fields).map(&:to_s)
              invalid_fields_error(invalid_fields) if invalid_fields.any?
            end

            def field_attrs(fields)
              @fields = fields
              @fields.each { |field| attr_accessor field }
            end

            private

            def invalid_fields_error(invalid_fields)
              raise Adparlor::Facebook::Error.new("Invalid field#{invalid_fields.length > 1 ? 's' : ''} #{invalid_fields.join(', ')} passed", 500)
            end
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
adparlor-facebook-0.8.2 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.8.0 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.9 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.8 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.7 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.6 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.5 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.4 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.3 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.2 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.1 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.7.0 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.6.6 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.6.5 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.6.4 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.6.3 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.6.2 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.6.1 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.6.0 lib/adparlor/facebook/graph_api/fields/field_decorator.rb
adparlor-facebook-0.5.9 lib/adparlor/facebook/graph_api/fields/field_decorator.rb