Class: UserVoice::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/uservoice/client.rb', line 4

def initialize(*args)
  case args.size
  when 1,2
    if args[1].is_a?(String)
      init_subdomain_and_api_keys(args[0], args[1])
    else
      init_consumer_and_access_token(*args)
    end
  when 3
    if args[2].is_a?(String)
      init_subdomain_and_api_keys(args[0], args[1], args[2])
    else
      init_subdomain_and_api_keys(args[0], args[1], nil, args[2])
    end
  when 4
    init_subdomain_and_api_keys(*args)
  end
end

Instance Method Details

#authorize_urlObject



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

def authorize_url
  request_token.authorize_url
end

#get_collection(uri, opts = {}) ⇒ Object



138
139
140
# File 'lib/uservoice/client.rb', line 138

def get_collection(uri, opts={})
  UserVoice::Collection.new(self, uri, opts)
end

#init_consumer_and_access_token(consumer, attrs = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/uservoice/client.rb', line 30

def init_consumer_and_access_token(consumer, attrs={})
  @consumer = consumer
  @token = OAuth::AccessToken.new(@consumer, attrs[:oauth_token] || '', attrs[:oauth_token_secret] || '')
  @response_format = attrs[:response_format] || :hash
  @callback = attrs[:callback]
end

#init_subdomain_and_api_keys(subdomain_name, api_key, api_secret, attrs = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/uservoice/client.rb', line 23

def init_subdomain_and_api_keys(subdomain_name, api_key, api_secret, attrs={})
  consumer = OAuth::Consumer.new(api_key, api_secret, { 
    :site => "#{attrs[:protocol] || 'https'}://#{subdomain_name}.#{attrs[:uservoice_domain] || 'uservoice.com'}"
  })
  init_consumer_and_access_token(consumer, attrs)
end

#login_as(email, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/uservoice/client.rb', line 79

def (email, &block)
  unless email.to_s.match(EMAIL_FORMAT)
    raise Unauthorized.new("'#{email}' is not a valid email address")
  end
  token = post('/api/v1/users/login_as.json', {
    :user => { :email => email },
    :request_token => request_token.token
  })['token']

  if token
    (token['oauth_token'], token['oauth_token_secret'], &block)
  else
    raise Unauthorized.new("Could not get Access Token")
  end
end

#login_as_owner(&block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/uservoice/client.rb', line 68

def (&block)
  token = post('/api/v1/users/login_as_owner.json', {
    'request_token' => request_token.token
  })['token']
  if token
    (token['oauth_token'], token['oauth_token_secret'], &block)
  else
    raise Unauthorized.new("Could not get Access Token")
  end
end

#login_with_access_token(oauth_token, oauth_token_secret, &block) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/uservoice/client.rb', line 47

def (oauth_token, oauth_token_secret, &block)
  token = Client.new(@consumer, :oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret)
  if block_given?
    yield token
  else
    return token
  end
end

#login_with_verifier(oauth_verifier) ⇒ Object

Raises:



41
42
43
44
45
# File 'lib/uservoice/client.rb', line 41

def (oauth_verifier)
  raise Unauthorized.new('Call request token first') if @request_token.nil?
  token = @request_token.get_access_token({:oauth_verifier => oauth_verifier}, {}, DEFAULT_HEADERS)
  Client.new(@consumer, :oauth_token => token.token, :oauth_token_secret => token.secret)
end

#request(method, uri, request_body = {}, headers = {}) ⇒ Object



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
# File 'lib/uservoice/client.rb', line 95

def request(method, uri, request_body={}, headers={})
  headers = DEFAULT_HEADERS.merge(headers)

  if headers['Content-Type'] == 'application/json' && request_body.is_a?(Hash)
    request_body = request_body.to_json
  end

  response = case method.to_sym
             when :post, :put
               @token.request(method, uri, request_body, headers)
             when :head, :delete, :get
               @token.request(method, uri, headers)
             else
               raise RuntimeError.new("Invalid HTTP method #{method}")
             end

  return case @response_format.to_s
         when 'raw'
           response
         else
           attrs = JSON.parse(response.body)
           if attrs && attrs['errors']
             case attrs['errors']['type']
             when 'unauthorized'
               raise Unauthorized.new(attrs)
             when 'record_not_found'
               raise NotFound.new(attrs)
             when 'application_error'
               raise ApplicationError.new(attrs)
             else
               raise APIError.new(attrs)
             end
           end
           attrs
         end
end

#request_tokenObject



64
65
66
# File 'lib/uservoice/client.rb', line 64

def request_token
  @request_token = @consumer.get_request_token({:oauth_callback => @callback}, {}, DEFAULT_HEADERS)
end

#secretObject



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

def secret
  @token.secret
end

#tokenObject



56
57
58
# File 'lib/uservoice/client.rb', line 56

def token
  @token.token
end