Class: Giddy::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/giddy/utils.rb

Class Method Summary collapse

Class Method Details

.get_lightbox_ids(username, password) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/giddy/utils.rb', line 26

def self.get_lightbox_ids(username, password)
  a = Mechanize.new
  a.get('https://secure.gettyimages.com/sign-in') do |page|
    page = page.form_with(:id => 'new_new_session') { |f|
      f["new_session[username]"] = username
      f["new_session[password]"] = password
    }.click_button

    return nil if page.filename != "index.html"
  end

  a.get('http://www.gettyimages.com/account/MediaBin/Default.aspx?ViewBy=0') do |page|
    links = page.links.map(&:href).select { |href| href.start_with? "/Account/MediaBin/LightboxDetail.aspx" }
    return links.map { |h| CGI::parse(h.split('?')[1])['Id'][0] }
  end
end

.rubified_hash(h) ⇒ Object

covert camel case keys to underscore



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/giddy/utils.rb', line 7

def self.rubified_hash(h)
  if h.is_a?(Array)
    return h.map { |v| rubified_hash(v) }
  elsif not h.is_a?(Hash)
    return h
  end

  fixed = {}
  h.each { |key, value|
    key = key.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
    fixed[key.intern] = rubified_hash(value)
  }
  fixed
end