Class: Partigirb::Client

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

Defined Under Namespace

Classes: Request

Constant Summary collapse

VALID_METHODS =
[:get,:post,:put,:delete]
VALID_FORMATS =
[:atom,:xml,:json]
PARTIGI_API_HOST =
"www.partigi.com"
TIMESTAMP_FORMAT =
'%Y-%m-%dT%H:%M:%SZ'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer_key, consumer_secret, options = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(consumer_key, consumer_secret, options={})
  @consumer = ::OAuth::Consumer.new(consumer_key, consumer_secret, {:site => "http://#{PARTIGI_API_HOST}"})
  @transport = Transport.new(@consumer)
  @api_host = PARTIGI_API_HOST.clone
  @api_version = options[:api_version] || Partigirb::CURRENT_API_VERSION
  @headers = {"User-Agent"=>"Partigirb/#{Partigirb::VERSION}"}.merge!(options[:headers]||{})
  @default_format = options[:default_format] || :atom
  @handlers = {
    :json => Partigirb::Handlers::JSONHandler.new,
    :xml => Partigirb::Handlers::XMLHandler.new,
    :atom => Partigirb::Handlers::AtomHandler.new,
    :unknown => Partigirb::Handlers::StringHandler.new
  }
  @handlers.merge!(options[:handlers]) if options[:handlers]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/partigirb/client.rb', line 67

def method_missing(name,*args)
  # If method is a format name, execute using that format
  if format_invocation?(name)
    return call_with_format(name,*args)
  end
  # If method ends in ! or ? use that to determine post or get
  if name.to_s =~ /^(.*)(!|\?)$/
    name = $1.to_sym
    # ! is a post, ? is a get
    self.request.method = ($2 == '!' ? :post : :get)          
    if format_invocation?(name)
      return call_with_format(name,*args)
    else
      self.request << "/#{$1}"
      return call_with_format(self.default_format,*args)
    end
  end
  # Else add to the request path
  self.request << "/#{name}"
  self
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def access_token
  @access_token
end

#api_hostObject

Returns the value of attribute api_host.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def api_host
  @api_host
end

#api_versionObject

Returns the value of attribute api_version.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def api_version
  @api_version
end

#authObject

Returns the value of attribute auth.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def auth
  @auth
end

#consumer_keyObject

Returns the value of attribute consumer_key.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def consumer_secret
  @consumer_secret
end

#default_formatObject

Returns the value of attribute default_format.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def default_format
  @default_format
end

#handlersObject

Returns the value of attribute handlers.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def handlers
  @handlers
end

#headersObject

Returns the value of attribute headers.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def headers
  @headers
end

#requestObject

Returns the value of attribute request.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def request
  @request
end

#transportObject

Returns the value of attribute transport.



49
50
51
# File 'lib/partigirb/client.rb', line 49

def transport
  @transport
end

Instance Method Details

#authorize_from_access(token, secret) ⇒ Object



125
126
127
# File 'lib/partigirb/client.rb', line 125

def authorize_from_access(token, secret)
  @access_token = OAuth::AccessToken.new(consumer, token, secret)
end

#authorize_from_request(request_token, request_secret, verifier_or_pin) ⇒ Object

For web apps use params, for desktop apps, use the verifier is the pin that twitter gives users.



112
113
114
115
# File 'lib/partigirb/client.rb', line 112

def authorize_from_request(request_token, request_secret, verifier_or_pin)
  request_token = OAuth::RequestToken.new(consumer, request_token, request_secret)
  @access_token = request_token.get_access_token(:oauth_verifier => verifier_or_pin)
end

#clearObject

Clears any pending request built up by chained methods but not executed



90
91
92
# File 'lib/partigirb/client.rb', line 90

def clear
  self.request = nil
end

#consumerObject



117
118
119
# File 'lib/partigirb/client.rb', line 117

def consumer
  @consumer
end

#request_token(options = {}) ⇒ Object

Note: If using oauth with a web app, be sure to provide :oauth_callback.



101
102
103
# File 'lib/partigirb/client.rb', line 101

def request_token(options={})
  @request_token ||= consumer.get_request_token(options)
end

#set_callback_url(url) ⇒ Object



105
106
107
108
# File 'lib/partigirb/client.rb', line 105

def set_callback_url(url)
  clear_request_token
  request_token(:oauth_callback => url)
end

#verify_credentialsObject

Shortcut methods



131
132
133
# File 'lib/partigirb/client.rb', line 131

def verify_credentials
  .verify_credentials?
end