Class: Sitegraph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Sitegraph

Returns a new instance of Sitegraph.



11
12
13
# File 'lib/sitegraph.rb', line 11

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

Instance Method Details

#create_event(description) ⇒ Object

Creates a new event associated with your account in the Sitegraph system

  • Args :

    • description -> A description of the event, such as “Launched v2.0”

  • Returns :

    • The event’s unique identifier

  • Raises :

    • RuntimeException -> If there is a problem with your request, a RuntimeException will be raised with the

      information
      


64
65
66
67
68
69
70
# File 'lib/sitegraph.rb', line 64

def create_event(description)
  response = self.post_request('event',{
    'authKey' => self.api_key,
    'event' => description
  })
  return response[description]
end

#decrement(identifier, amount) ⇒ Object

Decrements a given statistic by the amount specified

  • Args :

    • identifier -> The statistic’s identifier, set when you created the statistic

    • amount -> An integer specifying the amount by which to decrease the statistic

  • Returns :

    • True if the statistic was decremented.

  • Raises :

    • RuntimeException -> If there is a problem with your request, a RuntimeException will be raised with the

      information
      


45
46
47
48
49
50
51
52
53
# File 'lib/sitegraph.rb', line 45

def decrement(identifier, amount)
  response = self.post_request('decrement',{
    'authKey' => self.api_key,
    'statistic' => identifier,
    'amount' => amount,
    'api_info' => API_INFORMATION
  })
  return True
end

#increment(identifier, amount) ⇒ Object

Increments a given statistic by the amount specified

  • Args :

    • identifier -> The statistic’s identifier, set when you created the statistic

    • amount -> An integer specifying the amount by which to increment the statistic

  • Returns :

    • True if the statistic was incremented.

  • Raises :

    • RuntimeException -> If there is a problem with your request, a RuntimeException will be raised with the

      information
      


25
26
27
28
29
30
31
32
33
# File 'lib/sitegraph.rb', line 25

def increment(identifier, amount)
  response = self.post_request('increment',{
    'authKey' => self.api_key,
    'statistic' => identifier,
    'amount' => amount,
    'api_info' => API_INFORMATION
  })
  return True
end

#post_request(url, data) ⇒ Object

Helper function for POSTing to a Sitegraph URL

  • Args :

    • url -> The URL to post to, without the trailing slash.

    • data -> A hash of the data to post

  • Returns :

    • A hash of the parsed JSON response

  • Raises :

    • RuntimeException -> If there is a problem with your request, a RuntimeException will be raised with the

      information
      


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sitegraph.rb', line 82

def post_request(url, data)
  uri = URI.parse(API_SERVER + url + "/")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new(uri.path)
  request.body = URI.encode_www_form(data)
  
  response = http.request(request)
  
  json = JSON.parse(response.body)
  
  error = json['error']
  if error != nil
    raise error
  end
  
  return json
end