class MotionSteward::AppStoreSearch

Public Class Methods

construct_uri(path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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