Class: RbSplash::WrapSplashApi

Inherits:
Object
  • Object
show all
Defined in:
lib/rb_splash/wrap_splash_api.rb

Constant Summary collapse

AVAILABLE_ORDERS =
%w[latest oldest popular].freeze
AVAILABLE_ORIENTATIONS =
%w[landscape portrait squarish].freeze

Instance Method Summary collapse

Constructor Details

#initializeWrapSplashApi

Returns a new instance of WrapSplashApi.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rb_splash/wrap_splash_api.rb', line 11

def initialize
  @api_location = UrlConfig::API_LOCATION
  @bearer_token_url = UrlConfig::BEARER_TOKEN_URL
  @access_key = ''
  @secret_key = ''
  @redirect_uri = ''
  @code = ''
  @grant_type = 'authorization_code'
  @bearer_token = ''
  @timeout = 10_000
  @retries = 2
  @retry_delay_ms = 100
  @headers = {
    'Content-type' => 'application/json',
    'X-Requested-With' => 'WrapSplash'
  }
end

Instance Method Details

#add_photo_to_collection(collection_id, photo_id) ⇒ Object



309
310
311
312
313
314
315
# File 'lib/rb_splash/wrap_splash_api.rb', line 309

def add_photo_to_collection(collection_id, photo_id)
  validate_required(collection_id, 'collection_id')
  validate_required(photo_id, 'photo_id')
  fetch_url(@api_location + UrlConfig::ADD_PHOTO_TO_COLLECTION.gsub(':collection_id', collection_id), 'POST', {
              photo_id: photo_id
            })
end

#create_new_collection(title, description: nil, private_collection: false) ⇒ Object Also known as: create_collection



285
286
287
288
289
290
# File 'lib/rb_splash/wrap_splash_api.rb', line 285

def create_new_collection(title, description: nil, private_collection: false)
  validate_required(title, 'title')
  fetch_url(@api_location + UrlConfig::CREATE_NEW_COLLECTION, 'POST', {
              title: title, description: description, private: private_collection
            })
end

#delete_collection(id) ⇒ Object



304
305
306
307
# File 'lib/rb_splash/wrap_splash_api.rb', line 304

def delete_collection(id)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::DELETE_COLLECTION.gsub(':id', id), 'DELETE')
end

#generate_bearer_tokenObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rb_splash/wrap_splash_api.rb', line 59

def generate_bearer_token
  validate_required(@access_key, 'access_key')
  validate_required(@secret_key, 'secret_key')
  validate_required(@redirect_uri, 'redirect_uri')
  validate_required(@code, 'code')

  result = fetch_url(@bearer_token_url, 'POST', {}, {
                       client_id: @access_key,
                       client_secret: @secret_key,
                       redirect_uri: @redirect_uri,
                       code: @code,
                       grant_type: @grant_type
                     })

  clear_credentials!

  result
end

#get_a_photo(id, width: nil, height: nil, rect: nil) ⇒ Object Also known as: get_photo



147
148
149
150
151
152
# File 'lib/rb_splash/wrap_splash_api.rb', line 147

def get_a_photo(id, width: nil, height: nil, rect: nil)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::GET_A_PHOTO.gsub(':id', id), 'GET', {
              w: width, h: height, rect: rect
            })
end

#get_a_random_photo(collections: nil, featured: false, username: nil, query: nil, width: nil, height: nil, orientation: 'landscape', count: 1) ⇒ Object Also known as: get_random_photo



156
157
158
159
160
161
162
163
# File 'lib/rb_splash/wrap_splash_api.rb', line 156

def get_a_random_photo(collections: nil, featured: false, username: nil, query: nil, width: nil, height: nil,
                       orientation: 'landscape', count: 1)
  validate_supported_value(orientation, AVAILABLE_ORIENTATIONS, 'orientation')
  fetch_url(@api_location + UrlConfig::GET_A_RANDOM_PHOTO, 'GET', {
              collections: collections&.to_s, featured: featured, username: username,
              query: query, width: width, height: height, orientation: orientation, count: count
            })
end

#get_collection(id) ⇒ Object



256
257
258
259
# File 'lib/rb_splash/wrap_splash_api.rb', line 256

def get_collection(id)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::GET_COLLECTION.gsub(':id', id), 'GET')
end

#get_collection_photos(id, page: 1, per_page: 10) ⇒ Object



266
267
268
269
270
271
# File 'lib/rb_splash/wrap_splash_api.rb', line 266

def get_collection_photos(id, page: 1, per_page: 10)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::GET_COLLECTION_PHOTOS.gsub(':id', id), 'GET', {
              page: page, per_page: per_page
            })
end

#get_curated_collection(id) ⇒ Object



261
262
263
264
# File 'lib/rb_splash/wrap_splash_api.rb', line 261

def get_curated_collection(id)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::GET_CURATED_COLLECTION.gsub(':id', id), 'GET')
end

#get_curated_collection_photos(id, page: 1, per_page: 10) ⇒ Object



273
274
275
276
277
278
# File 'lib/rb_splash/wrap_splash_api.rb', line 273

def get_curated_collection_photos(id, page: 1, per_page: 10)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::GET_CURATED_COLLECTION_PHOTOS.gsub(':id', id), 'GET', {
              page: page, per_page: per_page
            })
end

#get_current_user_profileObject



78
79
80
# File 'lib/rb_splash/wrap_splash_api.rb', line 78

def 
  fetch_url(@api_location + UrlConfig::CURRENT_USER_PROFILE, 'GET')
end


174
175
176
177
# File 'lib/rb_splash/wrap_splash_api.rb', line 174

def get_photo_link(id)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::GET_A_PHOTO_DOWNLOAD_LINK.gsub(':id', id), 'GET')
end

#get_photo_statistics(id, resolution: 'days', quantity: 30) ⇒ Object



167
168
169
170
171
172
# File 'lib/rb_splash/wrap_splash_api.rb', line 167

def get_photo_statistics(id, resolution: 'days', quantity: 30)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::GET_A_PHOTO_STATISTICS.gsub(':id', id), 'GET', {
              resolution: resolution, quantity: quantity
            })
end

#get_public_profile(username, width: nil, height: nil) ⇒ Object



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

def get_public_profile(username, width: nil, height: nil)
  validate_required(username, 'username')
  fetch_url(@api_location + UrlConfig::USERS_PUBLIC_PROFILE + username, 'GET', { w: width, h: height })
end

#get_stats_monthObject



234
235
236
# File 'lib/rb_splash/wrap_splash_api.rb', line 234

def get_stats_month
  fetch_url(@api_location + UrlConfig::STATS_MONTH, 'GET')
end

#get_stats_totalsObject



230
231
232
# File 'lib/rb_splash/wrap_splash_api.rb', line 230

def get_stats_totals
  fetch_url(@api_location + UrlConfig::STATS_TOTALS, 'GET')
end

#get_user_collections(username, page: 1, per_page: 10) ⇒ Object



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

def get_user_collections(username, page: 1, per_page: 10)
  validate_required(username, 'username')
  fetch_url(@api_location + UrlConfig::USERS_COLLECTIONS.gsub(':username', username), 'GET', {
              page: page, per_page: per_page
            })
end

#get_user_liked_photos(username, page: 1, per_page: 10, order_by: 'latest') ⇒ Object



111
112
113
114
115
116
117
# File 'lib/rb_splash/wrap_splash_api.rb', line 111

def get_user_liked_photos(username, page: 1, per_page: 10, order_by: 'latest')
  validate_required(username, 'username')
  validate_supported_value(order_by, AVAILABLE_ORDERS, 'order_by')
  fetch_url(@api_location + UrlConfig::USERS_LIKED_PHOTOS.gsub(':username', username), 'GET', {
              page: page, per_page: per_page, order_by: order_by
            })
end

#get_user_photos(username, page: 1, per_page: 10, stats: false, resolution: 'days', quantity: 30, order_by: 'latest') ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/rb_splash/wrap_splash_api.rb', line 101

def get_user_photos(username, page: 1, per_page: 10, stats: false, resolution: 'days', quantity: 30,
                    order_by: 'latest')
  validate_required(username, 'username')
  validate_supported_value(order_by, AVAILABLE_ORDERS, 'order_by')
  fetch_url(@api_location + UrlConfig::USERS_PHOTOS.gsub(':username', username), 'GET', {
              page: page, per_page: per_page, order_by: order_by,
              stats: stats, resolution: resolution, quantity: quantity
            })
end

#get_user_portfolio(username) ⇒ Object



96
97
98
99
# File 'lib/rb_splash/wrap_splash_api.rb', line 96

def get_user_portfolio(username)
  validate_required(username, 'username')
  fetch_url(@api_location + UrlConfig::USERS_PORTFOLIO.gsub(':username', username), 'GET')
end

#get_user_statistics(username, resolution: 'days', quantity: 30) ⇒ Object



126
127
128
129
130
131
# File 'lib/rb_splash/wrap_splash_api.rb', line 126

def get_user_statistics(username, resolution: 'days', quantity: 30)
  validate_required(username, 'username')
  fetch_url(@api_location + UrlConfig::USERS_STATISTICS.gsub(':username', username), 'GET', {
              resolution: resolution, quantity: quantity
            })
end

#init(options = {}) ⇒ Object

Raises:



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
# File 'lib/rb_splash/wrap_splash_api.rb', line 29

def init(options = {})
  raise WrapSplashError, 'Initialisation parameters required!' unless options.is_a?(Hash) && !options.empty?

  @timeout = options[:timeout].is_a?(Numeric) && options[:timeout].positive? ? options[:timeout] : 10_000
  @retries = options[:retries].is_a?(Numeric) && options[:retries] >= 0 ? options[:retries] : 2
  raw_timeout = options[:retry_delay_ms]
  valid_timeout = raw_timeout.is_a?(Numeric) && raw_timeout >= 0
  @retry_delay_ms = valid_timeout ? raw_timeout : 100
  @bearer_token = options[:bearer_token] || ''

  @headers = {
    'Content-type' => 'application/json',
    'X-Requested-With' => 'WrapSplash'
  }

  if options[:bearer_token]
    @headers['Authorization'] = "Bearer #{options[:bearer_token]}"
    @headers['X-WrapSplash-Header'] = compute_hash(options[:bearer_token])
    return
  end

  @access_key = options[:access_key] || raise(WrapSplashError, 'Access Key missing!')
  @secret_key = options[:secret_key] || raise(WrapSplashError, 'Secret Key missing!')
  @redirect_uri = options[:redirect_uri] || raise(WrapSplashError, 'Redirect URI missing!')
  @code = options[:code] || raise(WrapSplashError, 'Authorization Code missing!')

  @headers['Authorization'] = "Client-ID #{options[:access_key]}"
  @headers['X-WrapSplash-Header'] = compute_hash(options[:access_key])
end

#like_photo(id) ⇒ Object



197
198
199
200
# File 'lib/rb_splash/wrap_splash_api.rb', line 197

def like_photo(id)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::LIKE_A_PHOTO.gsub(':id', id), 'POST')
end

#list_collections(page: 1, per_page: 10) ⇒ Object



238
239
240
241
242
# File 'lib/rb_splash/wrap_splash_api.rb', line 238

def list_collections(page: 1, per_page: 10)
  fetch_url(@api_location + UrlConfig::LIST_COLLECTIONS, 'GET', {
              page: page, per_page: per_page
            })
end

#list_curated_collections(page: 1, per_page: 10) ⇒ Object



250
251
252
253
254
# File 'lib/rb_splash/wrap_splash_api.rb', line 250

def list_curated_collections(page: 1, per_page: 10)
  fetch_url(@api_location + UrlConfig::LIST_CURATED_COLLECTIONS, 'GET', {
              page: page, per_page: per_page
            })
end

#list_curated_photos(page: 1, per_page: 10, order_by: 'latest') ⇒ Object



140
141
142
143
144
145
# File 'lib/rb_splash/wrap_splash_api.rb', line 140

def list_curated_photos(page: 1, per_page: 10, order_by: 'latest')
  validate_supported_value(order_by, AVAILABLE_ORDERS, 'order_by')
  fetch_url(@api_location + UrlConfig::LIST_CURATED_PHOTOS, 'GET', {
              page: page, per_page: per_page, order_by: order_by
            })
end


244
245
246
247
248
# File 'lib/rb_splash/wrap_splash_api.rb', line 244

def list_featured_collections(page: 1, per_page: 10)
  fetch_url(@api_location + UrlConfig::LIST_FEATURED_COLLECTIONS, 'GET', {
              page: page, per_page: per_page
            })
end

#list_photos(page: 1, per_page: 10, order_by: 'latest') ⇒ Object



133
134
135
136
137
138
# File 'lib/rb_splash/wrap_splash_api.rb', line 133

def list_photos(page: 1, per_page: 10, order_by: 'latest')
  validate_supported_value(order_by, AVAILABLE_ORDERS, 'order_by')
  fetch_url(@api_location + UrlConfig::LIST_PHOTOS, 'GET', {
              page: page, per_page: per_page, order_by: order_by
            })
end


280
281
282
283
# File 'lib/rb_splash/wrap_splash_api.rb', line 280

def list_related_collections(id)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::LIST_RELATED_COLLECTION.gsub(':id', id), 'GET')
end

#remove_photo_from_collection(collection_id, photo_id) ⇒ Object



317
318
319
320
321
322
# File 'lib/rb_splash/wrap_splash_api.rb', line 317

def remove_photo_from_collection(collection_id, photo_id)
  validate_required(collection_id, 'collection_id')
  validate_required(photo_id, 'photo_id')
  url = UrlConfig::REMOVE_PHOTO_FROM_COLLECTION.gsub(':collection_id', collection_id)
  fetch_url(@api_location + url, 'DELETE', { photo_id: photo_id })
end

#search(query, page: 1, per_page: 10, collections: nil, orientation: nil) ⇒ Object



207
208
209
210
211
212
213
214
# File 'lib/rb_splash/wrap_splash_api.rb', line 207

def search(query, page: 1, per_page: 10, collections: nil, orientation: nil)
  validate_required(query, 'query')
  validate_supported_value(orientation, AVAILABLE_ORIENTATIONS, 'orientation')
  fetch_url(@api_location + UrlConfig::SEARCH_PHOTOS, 'GET', {
              query: query, page: page, per_page: per_page,
              collections: collections&.to_s, orientation: orientation
            })
end

#search_collections(query, page: 1, per_page: 10) ⇒ Object



216
217
218
219
220
221
# File 'lib/rb_splash/wrap_splash_api.rb', line 216

def search_collections(query, page: 1, per_page: 10)
  validate_required(query, 'query')
  fetch_url(@api_location + UrlConfig::SEARCH_COLLECTIONS, 'GET', {
              query: query, page: page, per_page: per_page
            })
end

#search_users(query, page: 1, per_page: 10) ⇒ Object



223
224
225
226
227
228
# File 'lib/rb_splash/wrap_splash_api.rb', line 223

def search_users(query, page: 1, per_page: 10)
  validate_required(query, 'query')
  fetch_url(@api_location + UrlConfig::SEARCH_USERS, 'GET', {
              query: query, page: page, per_page: per_page
            })
end

#unlike_photo(id) ⇒ Object



202
203
204
205
# File 'lib/rb_splash/wrap_splash_api.rb', line 202

def unlike_photo(id)
  validate_required(id, 'id')
  fetch_url(@api_location + UrlConfig::UNLIKE_A_PHOTO.gsub(':id', id), 'DELETE')
end

#update_current_user_profile(username: nil, first_name: nil, last_name: nil, email: nil, url: nil, location: nil, bio: nil, instagram_username: nil) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/rb_splash/wrap_splash_api.rb', line 82

def (username: nil, first_name: nil, last_name: nil, email: nil, url: nil,
                                location: nil, bio: nil, instagram_username: nil)
  fetch_url(@api_location + UrlConfig::UPDATE_CURRENT_USER_PROFILE, 'PUT', {
              username: username, first_name: first_name, last_name: last_name,
              email: email, url: url, location: location, bio: bio,
              instagram_username: instagram_username
            })
end

#update_existing_collection(id, title, description: nil, private_collection: false) ⇒ Object Also known as: update_collection



294
295
296
297
298
299
300
# File 'lib/rb_splash/wrap_splash_api.rb', line 294

def update_existing_collection(id, title, description: nil, private_collection: false)
  validate_required(id, 'id')
  validate_required(title, 'title')
  fetch_url(@api_location + UrlConfig::UPDATE_EXISTING_COLLECTION.gsub(':id', id), 'PUT', {
              title: title, description: description, private: private_collection
            })
end

#update_photo(id, location: {}, exif: {}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rb_splash/wrap_splash_api.rb', line 179

def update_photo(id, location: {}, exif: {})
  validate_required(id, 'id')
  params = {}
  params['location[latitude]'] = location[:latitude] if location[:latitude]
  params['location[longitude]'] = location[:longitude] if location[:longitude]
  params['location[name]'] = location[:name] if location[:name]
  params['location[city]'] = location[:city] if location[:city]
  params['location[country]'] = location[:country] if location[:country]
  params['location[confidential]'] = location[:confidential] if location[:confidential]
  params['exif[make]'] = exif[:make] if exif[:make]
  params['exif[model]'] = exif[:model] if exif[:model]
  params['exif[exposure_time]'] = exif[:exposure_time] if exif[:exposure_time]
  params['exif[aperture_value]'] = exif[:aperture_value] if exif[:aperture_value]
  params['exif[focal_length]'] = exif[:focal_length] if exif[:focal_length]
  params['exif[iso_speed_ratings]'] = exif[:iso_speed_ratings] if exif[:iso_speed_ratings]
  fetch_url(@api_location + UrlConfig::UPDATE_A_PHOTO.gsub(':id', id), 'PUT', params)
end