Class: Flickr::Object

Inherits:
Object
  • Object
show all
Extended by:
Attributes, AutoloadHelper
Defined in:
lib/flickr/object.rb,
lib/flickr/object/set.rb,
lib/flickr/object/list.rb,
lib/flickr/object/photo.rb,
lib/flickr/object/person.rb,
lib/flickr/object/license.rb,
lib/flickr/object/location.rb,
lib/flickr/object/photo/tag.rb,
lib/flickr/object/photo/exif.rb,
lib/flickr/object/photo/note.rb,
lib/flickr/object/photo/size.rb,
lib/flickr/object/visibility.rb,
lib/flickr/object/list/normal.rb,
lib/flickr/object/permissions.rb,
lib/flickr/object/list/kaminari.rb,
lib/flickr/object/upload_ticket.rb,
lib/flickr/object/list/will_paginate.rb,
lib/flickr/object/person/upload_status.rb,
lib/flickr/object/attribute_locations/set.rb,
lib/flickr/object/attribute_locations/list.rb,
lib/flickr/object/attribute_locations/photo.rb,
lib/flickr/object/attribute_locations/person.rb,
lib/flickr/object/attribute_locations/location.rb,
lib/flickr/object/attribute_locations/photo/tag.rb,
lib/flickr/object/attribute_locations/photo/exif.rb,
lib/flickr/object/attribute_locations/photo/note.rb,
lib/flickr/object/attribute_locations/visibility.rb,
lib/flickr/object/attribute_locations/permissions.rb,
lib/flickr/object/attribute_locations/upload_ticket.rb,
lib/flickr/object/attribute_locations/person/upload_status.rb

Overview

Every Flickr object inherits from this class. It provides interface for defining attributes and some helper methods.

Defined Under Namespace

Classes: License, List, Location, Permissions, Person, Photo, Set, UploadTicket, Visibility

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

attribute

Methods included from AutoloadHelper

autoload_dir, autoload_names

Constructor Details

#initialize(attributes, access_token = []) ⇒ Object

Returns a new instance of Object.

Raises:



37
38
39
40
41
42
# File 'lib/flickr/object.rb', line 37

def initialize(attributes, access_token = [])
  raise ArgumentError, "attributes should not be nil" if attributes.nil?

  @attributes = attributes
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject (readonly)



47
48
49
# File 'lib/flickr/object.rb', line 47

def access_token
  @access_token
end

#attributesHash (readonly)

The raw hash of attributes returned from Flickr.

Returns:

See Also:



54
55
56
# File 'lib/flickr/object.rb', line 54

def attributes
  @attributes
end

Class Method Details

.attribute(name, type) ⇒ Object

Overriding Flickr::Attributes#attribute to add a default location. This means that ‘:<attribute>` will always first be searched in `@attributes`.



29
30
31
32
# File 'lib/flickr/object.rb', line 29

def self.attribute(name, type)
  new_attribute = super
  new_attribute.add_locations([-> { @attributes[name.to_s] }])
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Compares by ID (if that object has an ID), otherwise compares by attributes.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flickr/object.rb', line 69

def ==(other)
  if not other.is_a?(Flickr::Object)
    raise ArgumentError "can't compare Flickr::Object with #{other.class}"
  end

  if [self, other].all? { |object| object.respond_to?(:id) && object.id }
    self.id == other.id
  else
    self.attributes == other.attributes
  end
end

#[](key) ⇒ Object

Shorthand for accessing the raw hash of attributes returned from Flickr.

See Also:



62
63
64
# File 'lib/flickr/object.rb', line 62

def [](key)
  @attributes[key]
end

#inspectObject

Displays all the attributes and their values.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/flickr/object.rb', line 85

def inspect
  attribute_values = self.class.attributes
    .inject({}) { |hash, attribute| hash.update(attribute.name => send(attribute.name)) }
    .reject { |name, value| value.nil? or (value.respond_to?(:empty?) and value.empty?) }
  attributes = attribute_values
    .map { |name, value| "#{name}=#{value.inspect}" }
    .join(" ")

  class_name = self.class.name
  hex_code = "0x#{(object_id >> 1).to_s(16)}"

  "#<#{class_name}:#{hex_code} #{attributes}>"
end

#matches?(attributes) ⇒ Boolean

Tests if the object matches a hash of attributes. Supports nesting (see the example).

Examples:

photo.matches?(owner: {username: "janko"})

Parameters:

Returns:



108
109
110
111
112
113
114
115
116
# File 'lib/flickr/object.rb', line 108

def matches?(attributes)
  attributes.all? do |name, value|
    if send(name).is_a?(Flickr::Object) and value.is_a?(Hash)
      send(name).matches?(value)
    else
      send(name) == value
    end
  end
end

#update(attributes) ⇒ Object



121
122
123
124
# File 'lib/flickr/object.rb', line 121

def update(attributes)
  @attributes.update(attributes)
  self
end