Class: Infosimples::Data::Client

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

Overview

Infosimples::Data::Client is a client for the Infosimples Data API.

Constant Summary collapse

BASE_URL =
'https://data.infosimples.com/api/v1/:service.json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ Infosimples::Data::Client

Create a Infosimples::Data API client.

Parameters:

  • token (String)

    Your access token.

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :timeout (Integer) — default: 120

    Seconds before giving up of an automation being completed.

  • :max_age (Integer) — default: 86400

    Duration in seconds for a cached automation to be allowed.



19
20
21
22
23
# File 'lib/infosimples/data/client.rb', line 19

def initialize(token, options = {})
  self.token    = token
  self.timeout  = options[:timeout] || 120   # 120 seconds
  self.max_age  = options[:max_age] || 86400 # 24 hours in seconds
end

Instance Attribute Details

#max_ageObject

Returns the value of attribute max_age.



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

def max_age
  @max_age
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#automate(service, args = {}) ⇒ Hash

Automate a service.

Parameters:

  • service (String)

    Service you want to automate.

  • args (Hash) (defaults to: {})

    Arguments to be used in the service automation.

Returns:



31
32
33
# File 'lib/infosimples/data/client.rb', line 31

def automate(service, args = {})
  request(service, :multipart, args)
end

#billingArray

Get billing statistics from your account.

Returns:

  • (Array)

    Billing statistics per token.



38
39
40
# File 'lib/infosimples/data/client.rb', line 38

def billing
  request('billing', :get)
end

#download_sites_urls(response) ⇒ Array

Download sites_urls from response.

Parameters:

  • response (Hash)

    Response returned by #automate.

Returns:

  • (Array)

    HTML bodies from sites_urls.



54
55
56
57
58
59
60
# File 'lib/infosimples/data/client.rb', line 54

def download_sites_urls(response)
  return [] if !response.is_a?(Hash) ||
               (sites_urls = response.dig('receipt', 'sites_urls')).nil?
  sites_urls.map do |url|
    Infosimples::Data::HTTP.request(url: url, http_timeout: 30)
  end
end

#pricingArray

Get prices for each service.

Returns:

  • (Array)

    Service with price.



45
46
47
# File 'lib/infosimples/data/client.rb', line 45

def pricing
  request('pricing', :get)
end