Class: Satisfaction

Inherits:
Object
  • Object
show all
Includes:
Associations
Defined in:
lib/satisfaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Associations

#belongs_to, #has_many

Constructor Details

#initialize(options = {}) ⇒ Satisfaction

Returns a new instance of Satisfaction.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/satisfaction.rb', line 38

def initialize(options={})
  @options = options.reverse_merge({
    :root => "http://api.getsatisfaction.com", 
    :autoload => false,
    :request_token_url => 'http://getsatisfaction.com/api/request_token',
    :access_token_url => 'http://getsatisfaction.com/api/access_token',
    :authorize_url => 'http://getsatisfaction.com/api/authorize',
  })
  @loader = Sfn::Loader.new
  @identity_map = Sfn::IdentityMap.new
  
  has_many :companies, :url => '/companies'
  has_many :people, :url => '/people'
  has_many :topics, :url => '/topics'
  has_many :replies, :url => '/replies'
  has_many :tags, :url => '/tags'
  has_many :products, :url => '/products'
end

Instance Attribute Details

#consumerObject (readonly)

Returns the value of attribute consumer.



33
34
35
# File 'lib/satisfaction.rb', line 33

def consumer
  @consumer
end

#identity_mapObject (readonly)

Returns the value of attribute identity_map.



35
36
37
# File 'lib/satisfaction.rb', line 35

def identity_map
  @identity_map
end

#loaderObject (readonly)

Returns the value of attribute loader.



32
33
34
# File 'lib/satisfaction.rb', line 32

def loader
  @loader
end

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/satisfaction.rb', line 31

def options
  @options
end

#tokenObject (readonly)

Returns the value of attribute token.



34
35
36
# File 'lib/satisfaction.rb', line 34

def token
  @token
end

Instance Method Details

#access_token(token) ⇒ Object



104
105
106
107
108
109
# File 'lib/satisfaction.rb', line 104

def access_token(token)
  result, body = *@loader.get("#{options[:access_token_url]}", :force => true, :consumer => @consumer, :token => token)
  raise "Could not retrieve access token" unless result == :ok
  response = CGI.parse(body)
  OAuth::Token.new(response["oauth_token"], response["oauth_token_secret"])
end

#authorize_url(token) ⇒ Object



100
101
102
# File 'lib/satisfaction.rb', line 100

def authorize_url(token)
  "#{options[:authorize_url]}?oauth_token=#{token.token}"
end

#autoload?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/satisfaction.rb', line 73

def autoload?
  options[:autoload]
end

#delete(path) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/satisfaction.rb', line 136

def delete(path)
  url = self.url(path)
  @loader.post(url,
    :consumer => @consumer, 
    :token => @token, 
    :user => @user, 
    :password => @password,
    :method => :delete)
end

#get(path, query_string = {}) ⇒ Object



119
120
121
122
123
124
# File 'lib/satisfaction.rb', line 119

def get(path, query_string={})
  url = self.url(path, query_string)
  
  @loader.get(url, :consumer => @consumer, :token => @token, :user => @user, :password => @password)
    
end

#meObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/satisfaction.rb', line 61

def me
  me = satisfaction.identity_map.get_record(Me, 'me') do
    Sfn::Me.new('me', satisfaction)
  end
  
  if me.loaded?
    me
  else
    me.load 
  end
end

#post(path, form = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/satisfaction.rb', line 126

def post(path, form={})
  url = self.url(path)
  @loader.post(url, 
    :consumer => @consumer, 
    :token => @token, 
    :user => @user, 
    :password => @password,
    :form => form)
end

#put(path, form = {}) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/satisfaction.rb', line 146

def put(path, form={})
  url = self.url(path)
  @loader.post(url, 
    :consumer => @consumer, 
    :token => @token, 
    :user => @user, 
    :password => @password,
    :method => :put,
    :form => form)
end

#request_tokenObject



93
94
95
96
97
98
# File 'lib/satisfaction.rb', line 93

def request_token
  result, body = *@loader.get("#{options[:request_token_url]}", :force => true, :consumer => @consumer, :token => nil)
  raise "Could not retrieve request token" unless result == :ok
  response = CGI.parse(body)
  OAuth::Token.new(response["oauth_token"], response["oauth_token_secret"])
end

#satisfactionObject



57
58
59
# File 'lib/satisfaction.rb', line 57

def satisfaction
  self
end

#set_basic_auth(user, password) ⇒ Object



77
78
79
80
81
# File 'lib/satisfaction.rb', line 77

def set_basic_auth(user, password)
  identity_map.expire_record(Me, 'me')
  @user = user
  @password = password
end

#set_consumer(key, secret) ⇒ Object



83
84
85
86
# File 'lib/satisfaction.rb', line 83

def set_consumer(key, secret)
  identity_map.expire_record(Me, 'me')
  @consumer = OAuth::Consumer.new(key, secret)
end

#set_token(token, secret) ⇒ Object



88
89
90
91
# File 'lib/satisfaction.rb', line 88

def set_token(token, secret)
  identity_map.expire_record(Me, 'me')
  @token = OAuth::Token.new(token, secret)
end

#url(path, query_string = {}) ⇒ Object



112
113
114
115
116
117
# File 'lib/satisfaction.rb', line 112

def url(path, query_string={})
  qs = query_string.map{|kv| URI.escape(kv.first.to_s) + "=" + URI.escape(kv.last.to_s)}.join("&")
  uri_string = "#{@options[:root]}#{path}"
  uri_string += "?#{qs}" unless qs.blank?
  URI.parse(uri_string)
end