Class: Net::Flickr

### Home Classes Methods

### In Files

- lib/net/flickr/auth.rb
- lib/net/flickr/errors.rb
- lib/net/flickr/list.rb
- lib/net/flickr/people.rb
- lib/net/flickr/person.rb
- lib/net/flickr/photo.rb
- lib/net/flickr/photolist.rb
- lib/net/flickr/photos.rb
- lib/net/flickr/tag.rb
- lib/net/flickr.rb

### Parent

Object

### Namespace

- CLASS Net::Flickr::APIError
- CLASS Net::Flickr::Auth
- CLASS Net::Flickr::InvalidResponse
- CLASS Net::Flickr::List
- CLASS Net::Flickr::ListError
- CLASS Net::Flickr::People
- CLASS Net::Flickr::Person
- CLASS Net::Flickr::Photo
- CLASS Net::Flickr::PhotoList
- CLASS Net::Flickr::Photos
- CLASS Net::Flickr::Tag

### Methods

- ::new
- #auth
- #parse_response
- #people
- #photos
- #request
- #sign_request
- #sign_url

### Class/Module Index

Quicksearch

- Net
- Net::Flickr
- Net::Flickr::APIError
- Net::Flickr::Auth
- Net::Flickr::InvalidResponse
- Net::Flickr::List
- Net::Flickr::ListError
- Net::Flickr::People
- Net::Flickr::Person
- Net::Flickr::Photo
- Net::Flickr::PhotoList
- Net::Flickr::Photos
- Net::Flickr::Tag

No matching classes.

# Net::Flickr

This library implements Flickr's REST API. Its usage should be pretty straightforward. See below for examples.

Author | Ryan Grove (ryan@wonko.com)
Version | 0.0.1
Copyright | Copyright (c) 2007-2008 Ryan Grove. All rights reserved.
License | New BSD License (opensource.org/licenses/bsd-license.php)
Website | code.google.com/p/net-flickr/

## APIs not yet implemented

- activity

- blogs

- contacts

- favorites

- groups

- groups.pools

- interestingness

- photos.comments

- photos.geo

- photos.licenses

- photos.notes

- photos.transform

- photos.upload

- photosets

- photosets.comments

- reflection

- tags

- test

- urls

### Constants

AUTH_URL
REST_ENDPOINT
VERSION

### Attributes

api_key[R]
api_secret[R]
timeout[RW]

### Public Class Methods

new(api_key, api_secret = nil) click to toggle source

Creates a new Net::Flickr object that will use the specified api_key and api_secret to connect to Flickr. If you don't already have a Flickr API key, you can get one at flickr.com/services/api/keys.

If you don't provide an api_secret, you won't be able to make API calls requiring authentication.

    # File lib/net/flickr.rb, line 102
    def initialize(api_key, api_secret = nil)
      @api_key    = api_key
      @api_secret = api_secret

      # Initialize dependent classes.

      @auth   = Auth.new(self)
      @people = People.new(self)
      @photos = Photos.new(self)
    end

### Public Instance Methods

auth() click to toggle source

Returns a Net::Flickr::Auth instance.

    # File lib/net/flickr.rb, line 113
    def auth
      @auth
    end

parse_response(response_xml) click to toggle source

Parses the specified Flickr REST response. If the response indicates a successful request, the response block will be returned as an Hpricot element. Otherwise, an error will be raised.

    # File lib/net/flickr.rb, line 120
    def parse_response(response_xml)
      begin
        xml = Hpricot::XML(response_xml)
      rescue => e
        raise InvalidResponse, 'Invalid Flickr API response'
      end

      unless rsp = xml.at('/rsp')
        raise InvalidResponse, 'Invalid Flickr API response'
      end

      if rsp['stat'] == 'ok'
        return rsp
      elsif rsp['stat'] == 'fail'
        raise APIError, rsp.at('/err')['msg']
      else
        raise InvalidResponse, 'Invalid Flickr API response'
      end
    end

people() click to toggle source

Returns a Net::Flickr::People instance.

    # File lib/net/flickr.rb, line 141
    def people
      @people
    end

photos() click to toggle source

Returns a Net::Flickr::Photos instance.

    # File lib/net/flickr.rb, line 146
    def photos
      @photos
    end

request(method, args = {}) click to toggle source

Calls the specified Flickr REST API method with the supplied arguments and returns a Flickr REST response in XML format. If an API secret is set, the request will be properly signed.

    # File lib/net/flickr.rb, line 153
    def request(method, args = {})
      params  = args.merge({'method' => method, 'api_key' => @api_key})
      url     = URI.parse(REST_ENDPOINT)
      http    = Net::HTTP.new(url.host, url.port)
      request = sign_request(Net::HTTP::Post.new(url.path), params)

      http.start do |http|
        if block_given?
          http.request(request) {|response| yield response }
        else
          response = http.request(request)

          # Raise a Net::HTTP error if the HTTP request failed.

          unless response.is_a?(Net::HTTPSuccess) ||
              response.is_a?(Net::HTTPRedirection)
            response.error!
          end

          # Return the parsed response.

          return parse_response(response.body)
        end
      end
    end

sign_request(request, params) click to toggle source

Signs a Flickr API request with the API secret if set.

    # File lib/net/flickr.rb, line 178
    def sign_request(request, params)
      # If the secret isn't set, we can't sign anything.

      if @api_secret.nil?
        request.set_form_data(params)
        return request
      end

      # Add auth_token to the param list if we're already authenticated.

      params['auth_token'] = @auth.token unless @auth.token.nil?

      # Build a sorted, concatenated parameter list as described at

      # http://flickr.com/services/api/auth.spec.html

      paramlist = ''
      params.keys.sort.each {|key| paramlist << key <<
          URI.escape(params[key].to_s) }

      # Sign the request with a hash of the secret key and the concatenated

      # parameter list.

      params['api_sig'] = Digest::MD5.hexdigest(@api_secret + paramlist)
      request.set_form_data(params)

      return request
    end

sign_url(url) click to toggle source

Signs a Flickr URL with the API secret if set.

    # File lib/net/flickr.rb, line 203
    def sign_url(url)
      return url if @api_secret.nil?

      uri = URI.parse(url)

      params = uri.query.split('&')
      params << 'api_sig=' + Digest::MD5.hexdigest(@api_secret +
          params.sort.join('').gsub('=', ''))

      uri.query = params.join('&')

      return uri.to_s
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.