class AppStoreInfo::API

Constants

URL_TEMPLATE

The URL to lookup an app. The first variable is the region (us, pt, kor, etc..) and the second one is the application id (i.e. 599015198).

Attributes

id[RW]
url[RW]

Public Class Methods

new(id, region = AppStoreInfo::DEFAULT_REGION) click to toggle source
# File lib/app_store_info/api.rb, line 12
def initialize(id, region = AppStoreInfo::DEFAULT_REGION)
  @id = id
  @region = region
  @url = format(URL_TEMPLATE, region, id)
end

Public Instance Methods

parse() click to toggle source
# File lib/app_store_info/api.rb, line 18
def parse
  response = Faraday.get(url)

  unless response.status == 200 && response.body
    raise ConnectionError, "Cound't connect to app store API"
  end

  parse_json(response.body)
end

Private Instance Methods

parse_json(body) click to toggle source
# File lib/app_store_info/api.rb, line 30
def parse_json(body)
  json = JSON.parse(body)

  raise EntryNotFound, 'No results' unless json.key?('results')

  json = json['results'].first

  # If the JSON exists and it's a mobile app (to avoid OS X apps)
  unless json && json['supportedDevices']
    raise EntryNotFound, "App not found or unavailable on '#{@region}' region"
  end

  json
rescue JSON::ParserError => error
  raise ParseError, error.message
end