Class: FlurryHarvest::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/flurry_harvest/client.rb

Direct Known Subclasses

MockClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flurry_harvest/client.rb', line 12

def initialize(options = {})
  @api_access_code = options[:api_access_code] || (raise FlurryHarvest::ApiAccessCodeNotSet)
  @api_key = options[:api_key] || (raise FlurryHarvest::ApiKeyNotSet)
  @api_host = options[:api_host] || 'api.flurry.com'
  @api_url = options[:api_url] || '/appCircle/v2/getRecommendations'
  @api_port = options[:api_port] || 80
  @agent = options[:agent]
  @debug_mode = options[:debug_mode] || false

  log "Initialize:"
  log "-----------"
  log "api_access_code: #{api_access_code}"
  log "api_key: #{api_key}"
  log "api_host: #{api_host}"
  log "api_url: #{api_url}"
  log "api_port: #{api_port}"
  log "agent: #{agent}"
  log "full options: #{options}"
end

Instance Attribute Details

#agentObject (readonly)

custom agent string



9
10
11
# File 'lib/flurry_harvest/client.rb', line 9

def agent
  @agent
end

#api_access_codeObject (readonly)

Returns the value of attribute api_access_code.



2
3
4
# File 'lib/flurry_harvest/client.rb', line 2

def api_access_code
  @api_access_code
end

#api_hostObject (readonly)

api.flurry.com



4
5
6
# File 'lib/flurry_harvest/client.rb', line 4

def api_host
  @api_host
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'lib/flurry_harvest/client.rb', line 3

def api_key
  @api_key
end

#api_portObject (readonly)

80 || 443



6
7
8
# File 'lib/flurry_harvest/client.rb', line 6

def api_port
  @api_port
end

#api_urlObject (readonly)

appCircle/v2/getRecommendations



5
6
7
# File 'lib/flurry_harvest/client.rb', line 5

def api_url
  @api_url
end

#debug_modeObject (readonly)

Returns the value of attribute debug_mode.



10
11
12
# File 'lib/flurry_harvest/client.rb', line 10

def debug_mode
  @debug_mode
end

#queryObject (readonly)

Returns the value of attribute query.



8
9
10
# File 'lib/flurry_harvest/client.rb', line 8

def query
  @query
end

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



7
8
9
# File 'lib/flurry_harvest/client.rb', line 7

def raw_data
  @raw_data
end

Instance Method Details

#encode_mac(mac_address) ⇒ Object

SHA1 encode of mac address D9:2A:1A:0C:FD:0B should become CF41A96B9A6E4FE2942B4A51F350D7FD722E38B2



81
82
83
# File 'lib/flurry_harvest/client.rb', line 81

def encode_mac(mac_address)
  Digest::SHA1.hexdigest(mac_address).upcase
end

#fetch_offers(options = {}) ⇒ Object

mandatory params iosUdid - :udid sha1Mac - :mac platform - :platform (IPHONE, IPAD, AND) ipAddress - :ip

Raises:

  • (Exception)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/flurry_harvest/client.rb', line 37

def fetch_offers(options = {})
  log "fetch_offers.options: #{options}"

  raise Exception, "UDID is Required" if !options.has_key?(:udid)
  raise Exception, "IP is Required" if !options.has_key?(:ip)

  query_options = {
    :apiAccessCode => @api_access_code,
    :apiKey => @api_key,
    :iosUdid => options[:udid],
    :sha1Mac => options[:mac] ? encode_mac(options[:mac]) : nil,
    :platform => (options[:platform] || 'IPHONE').upcase,
    :ipAddress => options[:ip]
  }

  @query = to_query(query_options)

  http = Net::HTTP.new(@api_host, @api_port)
  http.use_ssl = (@api_port.to_i == 443)
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  path = "#{@api_url}?#{@query}"
  log "Request: http://#{api_host}:#{api_port}#{path}"

  response = http.get(path, headers)
  log "Response.body: #{response.body}"

  @raw_data = JSON.parse(response.body)
  log "Response.raw_data: #{@raw_data}"

  if response.code.to_i == 200
    result = decode
    log "Result: #{result.inspect}"

    return result unless @raw_data.empty?
  else
    raise FlurryHarvest::ApiError, message: "#{@raw_data['code']} - #{@raw_data['message']}"
  end

  false
end

#log(message) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/flurry_harvest/client.rb', line 85

def log(message)
  return unless debug_mode

  final_message = "FlurryHarvest [#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}]: #{message}"

  if defined? ::Rails
    ::Rails.logger.info final_message
  else
    Kernel.puts final_message
  end
end