Class: MotionSteward::AppStoreSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-steward/app_store_search.rb

Class Method Summary collapse

Class Method Details

.construct_uri(path) ⇒ Object



40
41
42
# File 'lib/motion-steward/app_store_search.rb', line 40

def self.construct_uri(path)
  URI.parse('https://itunes.apple.com/' + path)
end

.convert_to_date_maybe(k, v) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/motion-steward/app_store_search.rb', line 44

def self.convert_to_date_maybe k, v
  return v if k !~ /date/i

  begin
    Date.parse(v).to_date
  rescue
    v
  end
end

.get(path, querystring) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/motion-steward/app_store_search.rb', line 62

def self.get path, querystring
  first = true
  querystring.each do |k, v|
    if first
      path += '?'
      first = false
    else
      path += '&'
    end
    path += "#{k}=#{v}"
  end

  uri = construct_uri path
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  req = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(req)
  JSON.parse(response.body)
end

.pluck(json, values) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/motion-steward/app_store_search.rb', line 30

def self.pluck json, values
  if json.is_a? Array
    json.map do |o|
      to_hash(o).select { |k, _| values.include? k.to_s }
    end
  else
    to_hash(json).select { |k, _| values.include? k }
  end
end

.search_for_app(term) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/motion-steward/app_store_search.rb', line 18

def self.search_for_app term
  results = get 'search',
                media: 'software',
                entity: 'software',
                term: URI.encode(term),
                country: 'us',
                limit: 15

  pluck results['results'],
        %w(genres price track_name track_id average_user_rating user_rating_count release_date current_version_release_date price)
end

.to_hash(h) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/motion-steward/app_store_search.rb', line 54

def self.to_hash h
  h.inject({}) do |memo, (k, v)|
    new_v = convert_to_date_maybe(k, v)
    memo[k.underscore.to_sym] = new_v
    memo
  end
end