/
satisfaction.rb
180 lines (152 loc) · 4.59 KB
/
satisfaction.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require 'satisfaction/external_dependencies'
# Suppress the 'warning: peer certificate won't be verified in this SSL session'
class Net::HTTP
alias_method :old_initialize, :initialize
def initialize(*args)
old_initialize(*args)
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end
module Sfn
end
class Satisfaction
# ==============
# = Exceptions =
# ==============
require 'satisfaction/exceptions'
# ==================
# = Core Utilities =
# ==================
require 'satisfaction/util'
require 'satisfaction/has_satisfaction'
require 'satisfaction/associations'
require 'satisfaction/resource'
require 'satisfaction/loader'
require 'satisfaction/identity_map'
# =============
# = Resources =
# =============
require 'satisfaction/company'
require 'satisfaction/person'
require 'satisfaction/topic'
require 'satisfaction/tag'
require 'satisfaction/product'
require 'satisfaction/reply'
# =============
include Associations
attr_reader :options
attr_reader :loader
attr_reader :consumer
attr_reader :token
attr_reader :identity_map
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
def satisfaction
self
end
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
def autoload?
options[:autoload]
end
def set_basic_auth(user, password)
identity_map.expire_record(Me, 'me')
@user = user
@password = password
end
def set_consumer(key, secret)
identity_map.expire_record(Me, 'me')
@consumer = OAuth::Consumer.new(key, secret)
end
def set_token(token, secret)
identity_map.expire_record(Me, 'me')
@token = OAuth::Token.new(token, secret)
end
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
def authorize_url(token)
uri = URI.parse(options[:authorize_url])
if uri.query.present?
uri.query += "&oauth_token=#{token.token}"
else
uri.query = "oauth_token=#{token.token}"
end
uri.to_s
end
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
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
def get(path, query_string={})
url = self.url(path, query_string)
@loader.get(url, :consumer => @consumer, :token => @token, :user => @user, :password => @password)
end
def post(path, form={})
url = self.url(path)
@loader.post(url,
:consumer => @consumer,
:token => @token,
:user => @user,
:password => @password,
:form => form)
end
def delete(path)
url = self.url(path)
@loader.post(url,
:consumer => @consumer,
:token => @token,
:user => @user,
:password => @password,
:method => :delete)
end
def put(path, form={})
url = self.url(path)
@loader.post(url,
:consumer => @consumer,
:token => @token,
:user => @user,
:password => @password,
:method => :put,
:form => form)
end
private
def validate_options
raise ArgumentError, "You must specify a location for the API's service root" if options[:root].blank?
end
end