Class: SmugMug::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/smugmug/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client

Creates a new client instance that can be used to access the SMugMug API

Parameters:

  • args (Hash)

Options Hash (args):

  • :api_key (String)

    Your SmugMug API key

  • :oauth_secret (String)

    Your SmugMug OAuth secret key

  • :user (Hash)

    Configuration for the user you are requesting/updating data for

    • :token [String] OAuth token for the user

    • :secret [String] OAuth secret token for the user

  • String, (String, Optional :user_agent Helps SmugMug identify API calls)

    Optional :user_agent Helps SmugMug identify API calls

  • :http (Hash, Optional)

    Additional configuration for the HTTP requests

    • :verify_mode [Integer, Optional] How to verify the SSL certificate when connecting through HTTPS, either OpenSSL::SSL::VERIFY_PEER or OpenSSL::SSL::VERIFY_NONE, defaults to OpenSSL::SSL::VERIFY_NONE

    • :ca_file [String, Optional] Path to the CA certification file in PEM format

    • :ca_path [String, Optional] Path to the directory containing CA certifications in PEM format

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
# File 'lib/smugmug/client.rb', line 18

def initialize(args)
  raise ArgumentError, "API Key required" unless args.has_key?(:api_key)
  raise ArgumentError, "API OAuth secret required" unless args.has_key?(:oauth_secret)
  raise ArgumentError, "Must specify the users OAuth datA" unless args[:user].is_a?(Hash)
  raise ArgumentError, "Users OAuth token required" unless args[:user][:token]
  raise ArgumentError, "Users OAuth secret token required" unless args[:user][:secret]

  @http = HTTP.new(args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Direct mapping of SmugMug 1.3.0 API, either see wiki.smugmug.net/display/API/API+1.3.0 or the README for examples



74
75
76
77
78
79
80
81
82
83
# File 'lib/smugmug/client.rb', line 74

def method_missing(method, *args)
  api_cat = method.to_s
  return super unless SmugMug::API_METHODS[api_cat]

  if klass = self.instance_variable_get("@#{api_cat}_wrapper")
    klass
  else
    self.instance_variable_set("@#{api_cat}_wrapper", SmugMug::ApiCategory.new(@http, api_cat))
  end
end

Instance Method Details

#upload_media(args) ⇒ Object

Uploading media files to SmugMug, see wiki.smugmug.net/display/API/Uploading for more information

Parameters:

  • args (Hash)

Options Hash (args):

  • :file (File)

    File or stream that can have the content read to upload

  • :file (String)

    Binary contents of the file to upload

  • :FileName (String)

    What the file name is, only required when passing :file as a string

  • :AlbumID (Integer)

    SmugMug Album ID to upload the media to

  • :ImageID (Integer, Optional)

    Image ID to replace if reuploading media rather than adding new

  • :Caption (String, Optional)

    The caption for the media

  • :Hidden (Boolean, Optional)

    Whether the media should be visible

  • :Keywords (String, Optional)

    Keywords to tag the media as

  • :Altitude (Integer, Optional)

    Altitude the media was taken at

  • :Latitude (Float, Optional)

    Latitude the media was taken at

  • :Longitude (Float, Optional)

    Latitude the media was taken at

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/smugmug/client.rb', line 48

def upload_media(args)
  raise ArgumentError, "File is required" unless args.has_key?(:file)
  raise ArgumentError, "AlbumID is required" unless args.has_key?(:AlbumID)

  if args[:file].is_a?(String)
    args[:FileName] ||= File.basename(args[:file])
    args[:content] = File.read(args[:file])
  elsif args[:file].is_a?(File)
    args[:FileName] ||= File.basename(args[:file].path)
    args[:content] = args[:file].read
  else
    raise ArgumentError, "File must be a String or File"
  end

  args.delete(:file)
  @http.request(:uploading, args)
end