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.



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

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
  @search = Sfn::Search.new(options[:root], @loader)
  
  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.



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

def consumer
  @consumer
end

#identity_mapObject (readonly)

Returns the value of attribute identity_map.



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

def identity_map
  @identity_map
end

#loaderObject (readonly)

Returns the value of attribute loader.



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

def loader
  @loader
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#searchObject (readonly)

Returns the value of attribute search.



37
38
39
# File 'lib/satisfaction.rb', line 37

def search
  @search
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#access_token(token) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/satisfaction.rb', line 107

def access_token(token)
  log = Logger.new(STDOUT)
  log.level = Logger::INFO
  log.info("Trying to retrieve access token ...")
  result, body = *@loader.get("#{options[:access_token_url]}", :force => true, :consumer => @consumer, :token => token)
  unless result == :ok
    log.error("Could not retrieve access token!  Result was #{result} and the body was #{body}")
    log.info("consumer was #{@consumer}")
    raise "Could not retrieve access token" 
  end
  
  response = CGI.parse(body)
  OAuth::Token.new(response["oauth_token"], response["oauth_token_secret"])
end

#authorize_url(token) ⇒ Object



103
104
105
# File 'lib/satisfaction.rb', line 103

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

#autoload?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/satisfaction.rb', line 76

def autoload?
  options[:autoload]
end

#delete(path) ⇒ Object



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

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



130
131
132
133
134
135
# File 'lib/satisfaction.rb', line 130

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

#meObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/satisfaction.rb', line 64

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



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

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



157
158
159
160
161
162
163
164
165
166
# File 'lib/satisfaction.rb', line 157

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



96
97
98
99
100
101
# File 'lib/satisfaction.rb', line 96

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



60
61
62
# File 'lib/satisfaction.rb', line 60

def satisfaction
  self
end

#set_basic_auth(user, password) ⇒ Object



80
81
82
83
84
# File 'lib/satisfaction.rb', line 80

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

#set_consumer(key, secret) ⇒ Object



86
87
88
89
# File 'lib/satisfaction.rb', line 86

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

#set_token(token, secret) ⇒ Object



91
92
93
94
# File 'lib/satisfaction.rb', line 91

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

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



123
124
125
126
127
128
# File 'lib/satisfaction.rb', line 123

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